简体   繁体   中英

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. The parameter that I want to pass down from API gateway to Lambda function is '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)"

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 . You are using an uppercase I . From the AWS CloudFormation docs :

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.

Also, your template from above contains a RequestTemplates property which is placed in the wrong hierarchy level. It must be placed below Integration as noted in the AWS CloudFormation docs as well. Here is the correct template for AWS::ApiGateway::Method for you:

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM