如何使用lambda代理集成对API网关资源进行云格式化

sih*_*hil 17 aws-cloudformation aws-lambda aws-api-gateway

我一直在努力研究如何使用Lambda代理集成来表达(在cloudformation中)具有Lambda函数集成类型的API网关资源.

这在AWS控制台中很容易实现,因为您可以选择一个复选框: API网关控制台显示

但是,AWS :: ApiGateway :: Method CloudFormation资源中没有相应的字段(它应该在Integration属性中).

如何在cloudformation中配置它?

sih*_*hil 15

Integration类型应设置为AWS_PROXY.下面是工作YAML CloudFormation模板中方法的示例代码段.

ProxyResourceAny:
  Type: AWS::ApiGateway::Method
  Properties:
    AuthorizationType: NONE
    HttpMethod: ANY
    ResourceId:
      Ref: ProxyResource
    RestApiId:
      Ref: API
    Integration:
      Type: AWS_PROXY
      IntegrationHttpMethod: POST
      Uri: !Sub
        - arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${Arn}/invocations
        - Arn:
            Fn::GetAtt:
            - RestorerLambda
            - Arn
Run Code Online (Sandbox Code Playgroud)

值得一说的是我怎么想出来的......

在我挠了一会儿之后,我检查了aws apigateway get-methodCLI命令的输出,以获得使用控制台以这种方式配置的方法.这给了我以下JSON,我意识到复选框可能被编码到类型中.我测试了我的假设并提出了上面的CloudFormation.

{
    "apiKeyRequired": false,
    "httpMethod": "ANY",
    "methodIntegration": {
        "integrationResponses": {
            "200": {
                "responseTemplates": {
                    "application/json": null
                },
                "statusCode": "200"
            }
        },
        "passthroughBehavior": "WHEN_NO_MATCH",
        "cacheKeyParameters": [],
        "uri": "arn:aws:apigateway:eu-west-1:lambda:path/2015-03-31/functions/arn:aws:lambda:eu-west-1:XXXXXXXXX:function:Shildrew-Restorer-Play-Lambda/invocations",
        "httpMethod": "POST",
        "cacheNamespace": "64bl3tgw4g",
        "type": "AWS_PROXY"
    },
    "requestParameters": {},
    "authorizationType": "NONE"
}
Run Code Online (Sandbox Code Playgroud)