Skip to content

Issue with API Gateway returning 500 server error after renaming function. #930

@imangerah

Description

@imangerah

Description

After renaming a function in the SAM template, my API Gateway calls return 500 errors.

Steps to reproduce

I have a stack that uses a custom domain with AWS::ApiGateway::BasePathMapping and AWS::ApiGateway::DomainName. The Base Path Mapping is as follows:

  APIDomainName:
    Type: AWS::ApiGateway::DomainName
    Properties:
      CertificateArn: !ImportValue FlythroughApiRootDomain-APIDomainCertificate
      DomainName: !FindInMap [EndpointMap, !Ref StageName, url]

  APIBasePathMapping:
    Type: AWS::ApiGateway::BasePathMapping
    Properties:
      DomainName: !Ref APIDomainName
      RestApiId: !Ref ServerlessRestApi
      Stage: Prod

I am unsure if above plays a role in the bug.

I first deployed the stack with a function named HelloWorld-xxx, thereafter I decided to rename it to HelloWorld_xxx. After the stack successfully deployed, I started getting 500 errors from API Gateway.

Enabling logging on API Gateway I was able to determine the cause was API Gateway did not have permission to execute the lambda.

Deleting the function from my template, deploying the stack, adding it back and redeploying the stack fixed the issue.

Observed result

Working Permissions Prior to function rename:

aws lambda get-policy --function-name PaymentResponse-Api-prod 
{
    "Policy": "{\"Version\":\"2012-10-17\",\"Id\":\"default\",\"Statement\":[{\"Sid\":\"Api-prod-PaymentResponseFunctionRegisterPermissionProd-1LBGN1UHYWFWS\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"apigateway.amazonaws.com\"},\"Action\":\"lambda:invokeFunction\",\"Resource\":\"arn:aws:lambda:us-east-1:xxxxxxxxxxxx:function:PaymentResponse-Api-prod\",\"Condition\":{\"ArnLike\":{\"AWS:SourceArn\":\"arn:aws:execute-api:us-east-1:xxxxxxxxxxxx:5x12ry2pj0/Prod/POST/paymentresponse\"}}},{\"Sid\":\"Api-prod-PaymentResponseFunctionRegisterPermissionTest-DMNP3TTVDK7D\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"apigateway.amazonaws.com\"},\"Action\":\"lambda:invokeFunction\",\"Resource\":\"arn:aws:lambda:us-east-1:xxxxxxxxxxxx:function:PaymentResponse-Api-prod\",\"Condition\":{\"ArnLike\":{\"AWS:SourceArn\":\"arn:aws:execute-api:us-east-1:xxxxxxxxxxxx:5x12ry2pj0/*/POST/paymentresponse\"}}}]}", 
    "RevisionId": "85968857-f658-4167-xxxx-a70779bd905b"
}

Permissions after renaming the function (Produces a 500 error)

aws lambda get-policy --function-name PaymentResponse_Api-prod
{
    "Policy": "{\"Version\":\"2012-10-17\",\"Id\":\"default\",\"Statement\":[{\"Sid\":\"Api-prod-PaymentResponseFunctionRegisterPermissionProd-11DFFFOOMFVVP\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"apigateway.amazonaws.com\"},\"Action\":\"lambda:invokeFunction\",\"Resource\":\"arn:aws:lambda:us-east-1:xxxxxxxxxxxx:function:PaymentResponse_Api-prod\",\"Condition\":{\"ArnLike\":{\"AWS:SourceArn\":\"arn:aws:execute-api:us-east-1:xxxxxxxxxxxx:5x12ry2pj0/Prod/POST/paymentresponse\"}}},{\"Sid\":\"Api-prod-PaymentResponseFunctionRegisterPermissionTest-16ULZVEZEWU1W\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"apigateway.amazonaws.com\"},\"Action\":\"lambda:invokeFunction\",\"Resource\":\"arn:aws:lambda:us-east-1:xxxxxxxxxxxx:function:PaymentResponse_Api-prod\",\"Condition\":{\"ArnLike\":{\"AWS:SourceArn\":\"arn:aws:execute-api:us-east-1:xxxxxxxxxxxx:5x12ry2pj0/*/POST/paymentresponse\"}}}]}", 
    "RevisionId": "8786fe68-8650-4df7-xxxx-4ea06f82a439"
}

Permissions after deleting the function and redeploying (Works again - 500 error solved)

aws lambda get-policy --function-name PaymentResponse_Api-prod
{
    "Policy": "{\"Version\":\"2012-10-17\",\"Id\":\"default\",\"Statement\":[{\"Sid\":\"Api-prod-PaymentResponseFunctionRegisterPermissionTest-988DLJH4ECT1\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"apigateway.amazonaws.com\"},\"Action\":\"lambda:invokeFunction\",\"Resource\":\"arn:aws:lambda:us-east-1:xxxxxxxxxxxx:function:PaymentResponse_Api-prod\",\"Condition\":{\"ArnLike\":{\"AWS:SourceArn\":\"arn:aws:execute-api:us-east-1:xxxxxxxxxxxx:5x12ry2pj0/*/POST/paymentresponse\"}}},{\"Sid\":\"Api-prod-PaymentResponseFunctionRegisterPermissionProd-DNS6C540K5SZ\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"apigateway.amazonaws.com\"},\"Action\":\"lambda:invokeFunction\",\"Resource\":\"arn:aws:lambda:us-east-1:xxxxxxxxxxxx:function:PaymentResponse_Api-prod\",\"Condition\":{\"ArnLike\":{\"AWS:SourceArn\":\"arn:aws:execute-api:us-east-1:xxxxxxxxxxxx:5x12ry2pj0/Prod/POST/paymentresponse\"}}}]}", 
    "RevisionId": "05cf3af3-a9d9-48f9-xxxx-c46566fe74b1"
}

Expected result

Renaming a function should not result in API gateway permission errors.

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

  1. OS: Debian 9
  2. sam --version: SAM CLI, version 0.14.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions