aws-cli icon indicating copy to clipboard operation
aws-cli copied to clipboard

API Events are sometimes missing when deploying Cloudformation resource of type "AWS::Serverless::Function"

Open kskory opened this issue 9 years ago • 3 comments

I've got API Gateway and Lambda in a CF template with 7 API events (code at the bottom). After creating a new CF stack from that template some of the API Events for that lambda are usually missing. Redeploying doesn't fix that, the only way is to remove the Lambda from the CF stack and then add it back, after several attempts you may be lucky enough to have all Api Events created. The behaviour seems to be completely nondeterministic, sometimes 5 events get created, sometimes 6 or 7. It doesn't happen (or maybe it is just much less likely to happen) for lambdas with fewer Api Events.

Missing Lambda triggers are causing permissions errors (api gateway is missing permission to invoke lambda).

Our stack includes several more ApiGateway Lambdas and other resources, if it changes anything.

tested aws-cli versions aws-cli/1.11.78 Python/2.7.12 Linux/4.4.0-83-generic botocore/1.5.41 aws-cli/1.11.121 Python/2.7.6 Linux/3.13.0-92-generic botocore/1.5.84

aws cloudformation deploy --template-file ... --parameter-overrides ... --capabilities CAPABILITY_IAM --stack-name ...

ApiGatewayApi:
  Type: AWS::Serverless::Api
  Properties:
    StageName: v1
    DefinitionBody:
      Fn::Transform:
        Name: AWS::Include
        Parameters:
          Location: !Sub s3://${ArtifactsBucket}/${SwaggerArtifactName}

UserProfileLambdaHandler:
    Type: 'AWS::Serverless::Function'
    ...
      Events:
        Api1:
          Type: Api
          Properties:
            RestApiId: !Ref ApiGatewayApi
            Path: /user-profile
            Method: GET
        Api2:
          Type: Api
          Properties:
            RestApiId: !Ref ApiGatewayApi
            Path: /user-profile
            Method: PUT
        Api3:
          Type: Api
          Properties:
            RestApiId: !Ref ApiGatewayApi
            Path: /user-profile/sns-arns/{id}
            Method: DELETE
        Api4:
          Type: Api
          Properties:
            RestApiId: !Ref ApiGatewayApi
            Path: /user-profile/sns-arns
            Method: GET
        Api5:
          Type: Api
          Properties:
            RestApiId: !Ref ApiGatewayApi
            Path: /user-profile/sns-arns
            Method: POST
        Api6:
          Type: Api
          Properties:
            RestApiId: !Ref ApiGatewayApi
            Path: /user-profile/screen-name
            Method: GET
        Api7:
          Type: Api
          Properties:
            RestApiId: !Ref ApiGatewayApi
            Path: /user-profile/screen-name
            Method: PUT

and corresponding fragment from swagger file:

    "/user-profile" : {
      "get" : {
        "summary" : "user-profile",
        "description" : "",
        "operationId" : "getUserProfile",
        "produces" : [ "application/json" ],
        "responses" : {
          "200" : {
            "description" : "successful operation",
            "schema" : {
              "$ref" : "#/definitions/UserProfile"
            }
          }
        },
        "security" : [ {
          "cognito_userpool" : [ ]
        } ],
        "x-amazon-apigateway-integration" : {
          "passthroughBehavior" : "when_no_match",
          "responses" : {
            "default" : {
              "statusCode" : "200"
            }
          },
          "contentHandling" : "CONVERT_TO_TEXT",
          "httpMethod" : "POST",
          "type" : "aws_proxy",
          "uri" : {
            "Fn::Sub" : "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${EnvironmentName}-smsBackend-UserProfileLambdaHandler/invocations"
          }
        }
      },
      "put" : {
        "summary" : "user-profile",
        "description" : "",
        "operationId" : "updateUserProfile",
        "consumes" : [ "application/json" ],
        "produces" : [ "application/json" ],
        "parameters" : [ {
          "in" : "body",
          "name" : "body",
          "required" : false,
          "schema" : {
            "$ref" : "#/definitions/UserProfile"
          }
        } ],
        "responses" : {
          "200" : {
            "description" : "successful operation",
            "schema" : {
              "$ref" : "#/definitions/UserProfile"
            }
          }
        },
        "security" : [ {
          "cognito_userpool" : [ ]
        } ],
        "x-amazon-apigateway-integration" : {
          "passthroughBehavior" : "when_no_match",
          "responses" : {
            "default" : {
              "statusCode" : "200"
            }
          },
          "contentHandling" : "CONVERT_TO_TEXT",
          "httpMethod" : "POST",
          "type" : "aws_proxy",
          "uri" : {
            "Fn::Sub" : "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${EnvironmentName}-smsBackend-UserProfileLambdaHandler/invocations"
          }
        }
      }
    },
    "/user-profile/sns-arns" : {
      "get" : {
        "summary" : "user-profile/sns-arns",
        "description" : "",
        "operationId" : "getArns",
        "produces" : [ "application/json" ],
        "responses" : {
          "200" : {
            "description" : "successful operation",
            "schema" : {
              "type" : "array",
              "uniqueItems" : true,
              "items" : {
                "$ref" : "#/definitions/SnsArn"
              }
            }
          }
        },
        "security" : [ {
          "cognito_userpool" : [ ]
        } ],
        "x-amazon-apigateway-integration" : {
          "passthroughBehavior" : "when_no_match",
          "responses" : {
            "default" : {
              "statusCode" : "200"
            }
          },
          "contentHandling" : "CONVERT_TO_TEXT",
          "httpMethod" : "POST",
          "type" : "aws_proxy",
          "uri" : {
            "Fn::Sub" : "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${EnvironmentName}-smsBackend-UserProfileLambdaHandler/invocations"
          }
        }
      },
      "post" : {
        "summary" : "user-profile/sns-arns",
        "description" : "",
        "operationId" : "addArn",
        "consumes" : [ "application/json" ],
        "produces" : [ "application/json" ],
        "parameters" : [ {
          "in" : "body",
          "name" : "body",
          "required" : false,
          "schema" : {
            "$ref" : "#/definitions/SnsArn"
          }
        } ],
        "responses" : {
          "200" : {
            "description" : "successful operation",
            "schema" : {
              "type" : "array",
              "uniqueItems" : true,
              "items" : {
                "$ref" : "#/definitions/SnsArn"
              }
            }
          }
        },
        "security" : [ {
          "cognito_userpool" : [ ]
        } ],
        "x-amazon-apigateway-integration" : {
          "passthroughBehavior" : "when_no_match",
          "responses" : {
            "default" : {
              "statusCode" : "200"
            }
          },
          "contentHandling" : "CONVERT_TO_TEXT",
          "httpMethod" : "POST",
          "type" : "aws_proxy",
          "uri" : {
            "Fn::Sub" : "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${EnvironmentName}-smsBackend-UserProfileLambdaHandler/invocations"
          }
        }
      }
    },
    "/user-profile/sns-arns/{id}" : {
      "delete" : {
        "summary" : "user-profile/sns-arns/{id}",
        "description" : "",
        "operationId" : "deleteArn",
        "parameters" : [ {
          "name" : "id",
          "in" : "path",
          "required" : true,
          "type" : "string"
        } ],
        "responses" : {
          "204" : {
            "description" : ""
          }
        },
        "security" : [ {
          "cognito_userpool" : [ ]
        } ],
        "x-amazon-apigateway-integration" : {
          "passthroughBehavior" : "when_no_match",
          "responses" : {
            "default" : {
              "statusCode" : "204"
            }
          },
          "contentHandling" : "CONVERT_TO_TEXT",
          "httpMethod" : "POST",
          "type" : "aws_proxy",
          "uri" : {
            "Fn::Sub" : "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${EnvironmentName}-smsBackend-UserProfileLambdaHandler/invocations"
          }
        }
      }
    },
    "/user-profile/screen-name" : {
      "get" : {
        "summary" : "user-profile/screen-name",
        "description" : "",
        "operationId" : "getScreenName",
        "produces" : [ "application/json" ],
        "responses" : {
          "200" : {
            "description" : "successful operation",
            "schema" : {
              "$ref" : "#/definitions/ScreenName"
            }
          }
        },
        "security" : [ {
          "cognito_userpool" : [ ]
        } ],
        "x-amazon-apigateway-integration" : {
          "passthroughBehavior" : "when_no_match",
          "responses" : {
            "default" : {
              "statusCode" : "200"
            }
          },
          "contentHandling" : "CONVERT_TO_TEXT",
          "httpMethod" : "POST",
          "type" : "aws_proxy",
          "uri" : {
            "Fn::Sub" : "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${EnvironmentName}-smsBackend-UserProfileLambdaHandler/invocations"
          }
        }
      },
      "put" : {
        "summary" : "user-profile/screen-name",
        "description" : "",
        "operationId" : "updateScreenName",
        "consumes" : [ "application/json" ],
        "produces" : [ "application/json" ],
        "parameters" : [ {
          "in" : "body",
          "name" : "body",
          "required" : false,
          "schema" : {
            "$ref" : "#/definitions/ScreenName"
          }
        } ],
        "responses" : {
          "200" : {
            "description" : "successful operation",
            "schema" : {
              "$ref" : "#/definitions/ScreenName"
            }
          }
        },
        "security" : [ {
          "cognito_userpool" : [ ]
        } ],
        "x-amazon-apigateway-integration" : {
          "passthroughBehavior" : "when_no_match",
          "responses" : {
            "default" : {
              "statusCode" : "200"
            }
          },
          "contentHandling" : "CONVERT_TO_TEXT",
          "httpMethod" : "POST",
          "type" : "aws_proxy",
          "uri" : {
            "Fn::Sub" : "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${EnvironmentName}-smsBackend-UserProfileLambdaHandler/invocations"
          }
        }
      }
    }

kskory avatar Jul 21 '17 11:07 kskory

This appears to be related to CloudFormation's server-side logic rather than the logic with in the CLI cloudformation deploy command.

The cloudformation deploy command is essentially a small wrapper around the create-change-set and execute-change-set commands. So the template and swagger document provided to the command should not be getting modified prior to sending it to CloudFormation's API. Also given that it is nondeterministic with respect to the number of event sources that get created, further supports the case that it is related to CloudFormation's server-side logic.

I would recommend either opening up a issue in the developer forums or opening a case with support to get insight on what may be happening.

kyleknap avatar Jul 24 '17 20:07 kyleknap

Thanks for the response, I created a thread if anyone wants to track it: https://forums.aws.amazon.com/thread.jspa?threadID=260457&tstart=0

kskory avatar Jul 26 '17 09:07 kskory

Closing in favor of the forum issue.

JordonPhillips avatar Aug 21 '17 16:08 JordonPhillips