Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I cloudform an API gateway resource with a lambda proxy integration

Tags:

I've been trying to work out how to express (in cloudformation) an API Gateway Resource that has a Lambda function integration type using the Lambda Proxy integration.

This is easy to do in the AWS console as there is a check box that you can select: API gateway console showing the Use Lambda Proxy Integration checkbox

However there is no corresponding field in the AWS::ApiGateway::Method CloudFormation resource (it should be in the Integration property).

How can I configure this in cloudformation?

like image 719
sihil Avatar asked Sep 29 '16 13:09

sihil


Video Answer


2 Answers

The Integration type should be set to AWS_PROXY. An example snippet of a method from a working YAML CloudFormation template is below.

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

It's worth saying how a I figured this out...

After scratching my head for a while I examined the output of the aws apigateway get-method CLI command for a method that was configured this way using the console. That gave me the following JSON and I realised that the checkbox might be encoded into the type. I tested my assumption and came up with the CloudFormation above.

{
    "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"
}
like image 177
sihil Avatar answered Oct 18 '22 23:10

sihil


I have solved this same issue by simple changing the

Integration:
Type: AWS_PROXY

To

Integration:
Type: AWS

The cloud formation documentation currently is scarce and the API gateway cloudformation documentation doesn't match up to what can be seen on the console which hinders anyone who is trying to resolve an issue.

Hope this helps!

like image 21
Alex Johnston Avatar answered Oct 19 '22 00:10

Alex Johnston