简体   繁体   English

如何使用aws中的cloudformation在api网关中请求参数并将其传递给lambda函数?

[英]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 , where location is query string, path, or header, and name 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.

相关问题 如何使用 API 网关将事件参数传递给 AWS Lambda function? - How to pass event parameters to AWS Lambda function using API Gateway? 如何使用AWS API Gateway将URL参数传递给Lambda函数? - How do you pass URL Parameters to a Lambda function using AWS API Gateway? 如何使用API​​网关和Lambda函数从AWS Aurora读取数据? - How can I read data from AWS Aurora using API gateway and Lambda function? C#如何配置AWS API Gateway参数以映射到基本的AWS Lambda函数? - C# How do i configure AWS API Gateway parameters to map to a basic AWS Lambda Function? AWS Cloudformation配置API网关指向lambda function版本 - AWS Cloudformation configure API Gateway to point to lambda function version 如何在AWS Lambda函数中从AWS API Gateway URI访问请求和路径变量 - How do I get access to the request and path variables from AWS API Gateway URI in an AWS Lambda function 如何将POST变量传递给运行Lambda函数的AWS API Gateway? - How to pass POST variables to an AWS API Gateway running a lambda function? 我可以使用 CloudFormation 模板更新 AWS Lambda function 吗? - Can I update AWS Lambda function using CloudFormation template? 如何在AWS API网关和Lambda函数中触发ngrok获取请求? - How to trigger a ngrok get request in AWS API gateway and Lambda function? 如何使用API​​网关将参数传递给Lambda函数以从DynamoDB查询项目? - How to pass parameters to a Lambda function using API gateway for querying items from DynamoDB?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM