CloudFormation doesn't deploy to API gateway stages on update

后端 未结 8 1241
南笙
南笙 2020-12-25 11:47

When I run CloudFormation deploy using a template with API Gateway resources, the first time I run it, it creates and deploys to stages. The subsequent times I

相关标签:
8条回答
  • 2020-12-25 12:06

    Seems like there is no way to easily create a new Deployment whenever one of your Cloudformation Resources changes.

    One way to work around that would be to use a Lambda-backed Custom Resource (see http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-custom-resources.html).

    The Lambda should create the new Deployment, only if one of your Resources has been updated. To determine if one of your Resources has been updated,
    you will probably have to implement custom logic around this API call: http://docs.aws.amazon.com/AWSCloudFormation/latest/APIReference/API_DescribeStackEvents.html

    In order to trigger updates on your Custom Resource, I suggest you supply a Cloudformation Parameter that will be used to force an update of your Custom Resource (e.g. the current time, or a version number).

    Note that you will have to add a DependsOn clause to your Custom Resource that will include all Resources relevant to your API. Otherwise, your deployment might be created before all your API Resources are updated.

    Hope this helps.

    0 讨论(0)
  • 2020-12-25 12:06

    When your template specifies a deployment, CloudFormation will create that deployment only if it doesn't already exist. When you attempt to run it again, it observes that the deployment still exists so it won't recreate it, thus no deployment. You need a new resource id for the deployment so that it will create a new deployment. Read this for more information: https://currentlyunnamed-theclassic.blogspot.com/2018/12/mastering-cloudformation-for-api.html

    0 讨论(0)
  • 2020-12-25 12:08

    I was using above approach but it looks to complicated to me just to deploy API gateway. If we are changing name of the resources then it takes time to delete and recreate the resources which increases down time for you application.

    I'm following below approach to deploy API gateway to the stage using AWS CLI and it is not affecting the deployment with Cloudformation stack.

    What I'm doing is, running below AWS CLI command after deployment is completed for API Gateway. It will update the existing stage with latest updates.

    aws apigateway create-deployment --rest-api-id tztstixfwj --stage-name stg --description 'Deployed from CLI'
    
    0 讨论(0)
  • 2020-12-25 12:11

    Use SAM

    AWS::Serverless::Api

    This does the deployment for you when it does the Transformation

    0 讨论(0)
  • 2020-12-25 12:19

    The answer here is to use the AutoDeploy property of the Stage:

      Stage:
        Type: AWS::ApiGatewayV2::Stage
        Properties:
          StageName: v1
          Description: 'API Version 1'
          ApiId: !Ref: myApi
          AutoDeploy: true
    

    Note that the 'DeploymentId' property must be unspecified when using 'AutoDeploy'.

    See documentation, here: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigatewayv2-stage.html

    0 讨论(0)
  • 2020-12-25 12:23

    I may be late, but here are the options which which you do a redeployment if a API resources changes, may be helpful to people who still looking for options -

    1. Try AutoDeploy to true. If you are using V2 version of deployment. Note that you need to have APIGW created through V2. V1 and V2 are not compatible to each other. https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigatewayv2-stage.html#cfn-apigatewayv2-stage-autodeploy

    2. Lambda backed custom resource, Lambda inturn call createDeployment API - https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-custom-resources.html

    3. CodePipeline that has an action that calls a Lambda Function much like the Custom Resource would - https://docs.aws.amazon.com/codepipeline/latest/userguide/actions-invoke-lambda-function.html

    4. SAM(Serverless Application Model) follows a similar syntax to CloudFormation which simplifies the resource creation into abstractions and uses those to build and deploy a normal CloudFormation template. https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html

    5. If you are using any abstraction layer to cloudformation like Sceptre, you can have a hook to call createDeployment after any-update to the resource https://sceptre.cloudreach.com/2.3.0/docs/hooks.html

    I gone with third option since I kept using Sceptre for Cloudformation deployment. Implementing hooks in sceptre is easy as well.

    0 讨论(0)
提交回复
热议问题