簡體   English   中英

如何使用aws中的cloudformation在api網關中請求參數並將其傳遞給lambda函數?

[英]How can I request parameters in api gateway using cloudformation in aws and pass it down to lambda function?

我正在嘗試使用 AWS CloudFormation 中的 API Gateway 請求參數。 我想從 API 網關傳遞給 Lambda 函數的參數是“action”。 我已經嘗試了以下代碼,到目前為止我遇到了錯誤,如下所述。 有人可以幫助我確定問題和可能的解決方案嗎?

“指定的映射表達式無效:驗證結果:警告:[],錯誤:[指定的映射表達式無效: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' 

根據文檔, RequestParameters的鍵應該像integration.request.<location>.<name> ,帶有小寫的i用於integration 您正在使用大寫的I 來自AWS CloudFormation 文檔

使用以下模式integration.request.location.name指定目標,其中location是查詢字符串、路徑或標頭, name是有效的唯一參數名稱。

此外,您上面的模板包含一個RequestTemplates屬性,該屬性位於錯誤的層次結構級別。 它也必須放在Integration下方,如AWS CloudFormation 文檔中所述。 以下是適合您的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"

有關定義請求模板的更多信息,參見開發人員參考

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM