[英]How can I request parameters in api gateway using cloudformation in aws and pass it down to lambda function?
I am trying to request parameters using API Gateway in AWS CloudFormation.我正在尝试使用 AWS CloudFormation 中的 API Gateway 请求参数。 The parameter that I want to pass down from API gateway to Lambda function is 'action'.
我想从 API 网关传递给 Lambda 函数的参数是“action”。 I have tried the following code and so far I ran into the error, mention below.
我已经尝试了以下代码,到目前为止我遇到了错误,如下所述。 Can someone please help me with identifying the issue and a possible resolution?
有人可以帮助我确定问题和可能的解决方案吗?
"Invalid mapping expression specified: Validation Result: warnings : [], errors : [Invalid mapping expression specified: Integration.request.path.action] (Service: AmazonApiGateway; Status Code: 400; Error Code: BadRequestException; Request ID: 037f4753-52b5-4276-979a-131a0f903e63)" “指定的映射表达式无效:验证结果:警告:[],错误:[指定的映射表达式无效:Integration.request.path.action](服务:AmazonApiGateway;状态代码:400;错误代码:BadRequestException;请求 ID:037f4753- 52b5-4276-979a-131a0f903e63)"
AWSTemplateFormatVersion: "2010-09-09"
Description: "API Gateway and Lambda function"
Resources:
SampleApi:
Type: "AWS::ApiGateway::RestApi"
Properties:
Name: Sample
SampleApiMethod:
Type: "AWS::ApiGateway::Method"
Properties:
AuthorizationType: "NONE"
HttpMethod: "GET"
RequestParameters:
method.request.path.action: true
RequestTemplates:
application/yaml
Integration:
IntegrationHttpMethod: "POST"
Type: "AWS_PROXY"
RequestParameters:
Integration.request.path.action: method.request.path.action
Uri: !Sub
- "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${lambdaArn}/invocations"
- lambdaArn: !GetAtt "SampleLambda.Arn"
CacheKeyParameters:
- method.request.path.action
ResourceId: !GetAtt "SampleApi.RootResourceId"
RestApiId: !Ref "SampleApi"
SampleApiDeployment:
Type: "AWS::ApiGateway::Deployment"
DependsOn: "SampleApiMethod"
Properties:
RestApiId: !Ref "SampleApi"
StageName: test
SampleLambda:
Type: "AWS::Lambda::Function"
Properties:
Code:
ZipFile: |
import yaml
import boto3
cf_client = boto3.client('cloudformation')
cf_client.create_stack(
StackName='your-stack',
TemplateURL='Some URL',
Parameters=[
{
'ParameterKey':'action',
'ParameterValue': 'kms:*'
},
]
)
Handler: "index.handler"
Role: !GetAtt "SampleLambdaRole.Arn"
Runtime: python3.7
LambdaApiGatewayInvoke:
Type: "AWS::Lambda::Permission"
Properties:
Action: "lambda:InvokeFunction"
FunctionName: !GetAtt "SampleLambda.Arn"
Principal: "apigateway.amazonaws.com"
SourceArn: !Sub "arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${SampleApi}/*/GET/"
SampleLambdaRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Action: ["sts:AssumeRole"]
Effect: "Allow"
Principal:
Service: ["lambda.amazonaws.com"]
Policies:
- PolicyDocument:
Version: "2012-10-17"
Statement:
- Action: ["cloudwatch:*", "logs:*"]
Effect: "Allow"
Resource: "*"
PolicyName: "lambdaLogPolicy"
Outputs:
apiGatewayInvokeURL:
Value: !Sub 'https://Sample.execute-api.${AWS::Region}.amazonaws.com/test'
According to the docs, the key for RequestParameters
should be like integration.request.<location>.<name>
, with a lowercase i
for integration
.根据文档,
RequestParameters
的键应该像integration.request.<location>.<name>
,带有小写的i
用于integration
。 You are using an uppercase I
.您正在使用大写的
I
。 From the AWS CloudFormation docs :来自AWS CloudFormation 文档:
Specify the destination by using the following pattern
integration.request.location.name
, wherelocation
is query string, path, or header, andname
is a valid, unique parameter name.使用以下模式
integration.request.location.name
指定目标,其中location
是查询字符串、路径或标头,name
是有效的唯一参数名称。
Also, your template from above contains a RequestTemplates
property which is placed in the wrong hierarchy level.此外,您上面的模板包含一个
RequestTemplates
属性,该属性位于错误的层次结构级别。 It must be placed below Integration
as noted in the AWS CloudFormation docs as well.它也必须放在
Integration
下方,如AWS CloudFormation 文档中所述。 Here is the correct template for AWS::ApiGateway::Method
for you:以下是适合您的
AWS::ApiGateway::Method
模板:
SampleApiMethod:
Type: "AWS::ApiGateway::Method"
Properties:
AuthorizationType: "NONE"
HttpMethod: "GET"
RequestParameters:
method.request.path.action: true
Integration:
IntegrationHttpMethod: "POST"
Type: "AWS_PROXY"
RequestParameters:
integration.request.path.action: method.request.path.action
RequestTemplates:
"application/yaml": "<define your template here>"
Uri: !Sub
- "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${lambdaArn}/invocations"
- lambdaArn: !GetAtt "SampleLambda.Arn"
CacheKeyParameters:
- method.request.path.action
ResourceId: !GetAtt "SampleApi.RootResourceId"
RestApiId: !Ref "SampleApi"
More information about defining a request template can be found in the developer reference .有关定义请求模板的更多信息,请参见开发人员参考。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.