简体   繁体   中英

AWS API Gateway - Lambda Proxy not turning on via CloudFormation template

I am using CloudFormation to create an API in API Gateway. Each one of my endpoints points to a Lambda function. I need to turn on "Lambda Proxy integration" for each endpoint.

Here's a snippet from my CloudFormation template:

method1:
    Type: "AWS::ApiGateway::Method"
    Properties: 
        ApiKeyRequired: true
        AuthorizationType: None 
        HttpMethod: POST
        Integration:
            Type: AWS_PROXY
            IntegrationHttpMethod: POST
            IntegrationResponses:
                - ResponseTemplates:
                    application/json: Empty
                StatusCode: 200
            Uri: 
                Fn::ImportValue: !Sub '${ProjectName}-${Environment}-method1'
        MethodResponses:
            - ResponseModels:
                application/json: Empty
            StatusCode: 200
        RequestValidatorId: !Ref validateBodyValidator
        ResourceId: !Ref method1Resource
        RestApiId: !Ref RestApi

I have set my integration type as AWS_PROXY . When I run this template, everything seems like. I get the following result:

在此处输入图片说明 在此处输入图片说明

As you see, Use Lambda Proxy integration option is shown as selected. However, when I make the API call, I get the following error.

[
    "Internal Server Error"
]

After a day of trying to find the issue, here's what I found:

If I uncheck the Use Lambda Proxy integration option, re-check it, and deploy - it starts working.

It's almost like - it looks selected but it's not selected. I have to manually uncheck and recheck for EVERY method.

Any thoughts?

I was able to solve this, thanks to congbaoguier for your comment above.

I added the Method1Permission section in the following template where I create my Lambda function:

Method1:
    Type: AWS::Lambda::Function
    DependsOn:
        - Method1Role
        - Method1Policy
    Properties:
        Role: !GetAtt Method1Role.Arn 
        Code:
            S3Bucket: !ImportValue sharedinf-cp-lambdabucketname
            S3Key: Method1.jar
    Handler: com.companyname.projectname.methodname::handleRequest
    Runtime: "java8"
    Timeout: "15"
    MemorySize: "512"

Method1Permission:
    Type: AWS::Lambda::Permission
    Properties:
    FunctionName: !GetAtt 
        - Method1
        - Arn
    Action: 'lambda:InvokeFunction'
    Principal: apigateway.amazonaws.com

This allowed API gateway to access my Lambda function.

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