Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CloudFormation doesn't deploy to API gateway stages on update

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 run it, it updates the resources but doesn't deploy to stages.

Is that behaviour as intended? If yes, how'd I get it to deploy to stages whenever it updates?

(Terraform mentions a similar issue: https://github.com/hashicorp/terraform/issues/6613)

like image 909
bjfletcher Avatar asked Jan 02 '17 08:01

bjfletcher


People also ask

How do I redeploy API gateway?

Sign in to the API Gateway console at https://console.aws.amazon.com/apigateway . In the APIs navigation pane, choose the API you want to deploy. In the Resources navigation pane, choose Actions. From the Actions drop-down menu, choose Deploy API.

How long does API gateway take to update?

You can change an existing API type using the API Gateway console, the AWS CLI, or an AWS SDK for API Gateway. The update operation may take up to 60 seconds to complete.

What happens when you update a CloudFormation stack?

When you directly update a stack, you submit changes and AWS CloudFormation immediately deploys them. Use direct updates when you want to quickly deploy your updates. With change sets, you can preview the changes AWS CloudFormation will make to your stack, and then decide whether to apply those changes.


2 Answers

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.

like image 167
spg Avatar answered Sep 22 '22 09:09

spg


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

like image 36
TheClassic Avatar answered Sep 21 '22 09:09

TheClassic