AWS Cloud formation script to create and execute change set - amazon-web-services

I am using the following AWS Cli Cloud Formation commands to create and then execute and change set:
aws cloudformation create-change-set --change-set-name change-set-1
aws cloudformation execute-change-set --change-set-name change-set-1
However the first command returns before the the change set has been created, the if I execute the second command immediately it fails.
Solutions I have considered:
Adding a delay between the two commands.
Repeating the second command until it succeeds.
Both of these have their problems.
Ideally there would be an option on the create-change-set command to execute immediately, or to run synchronously and not return until the change set has been created.
Has anyone ever tried this and come up with a better solution than me?

I haven't personally tried it, but maybe you could use the command list-change-sets to loop until your change set is with a status CREATE_COMPLETE, and then execute your second command.
Hope this helps.

I solved this issue by using the following sequence :
aws cloudformation create-change-set
aws cloudformation wait change-set-create-complete
aws cloudformation execute-change-set
aws cloudformation wait stack-create-complete
Hope it will help.

If you don't require the intermediate step of creating a change set and then executing it (as we didn't) then use the update-stack sub command.
aws cloudformation update-stack --stack-name myStack --template-url ...

Related

How to show the status of creating or updating a stack with Cloudformation using AWS cli

I'm trying to find a command that allows me to see the latest status of the creation/update of a Cloudformation stack.
I thought I could use wait preceding the command stack-update-complete (in this case for update) but nothing happens.
This is what I'm doing:
I execute the command to update my stack:
aws cloudformation update-stack --stack-name my-stack \
--template-body file://my-stack.yml
Immediately after I run:
aws cloudformation wait stack-update-complete --stack-name my-stack
I want to see the latest status including the result (create, update, delete, and rollback) so I don't have to login into AWS console.
Thanks!

Is it possible to execute deployment of AWS CDK asynchronously?

The command cdk deploy ... is used to deploy one or more CloudFormation stacks. When it executes, it displays messages resulting from the deployment of the various stacks and this can take some time.
The deploy command supports the --notification-arns parameter, which is an array of ARNs of SNS topics that CloudFormation will notify with stack related events.
Is it possible to execute cdk deploy and not have it report to the console its progress (i.e. the command exits immediately after uploading the new CloudFormation assets) and simply rely on the SNS topic as a means of getting feedback on the progress of a deployment?
A quick and dirty way (untested) would be to use nohup
$ nohup cdk ... --require-approval never 1>/dev/null
The --require-approval never simply means it wont stop to ask for permission for sercurity requests and obviously nohup allows the command to run with out terminating.
Its the only solution I can think of that is quick.
Another solution for long term would be to use the CdkToolkit to create your own script for deployment. Take a look at cdk command to get an idea. This is been something Ive wanted from aws-cdk for a while - I want custom deploy scripts rather than using the shell.
I found a solution to asynchronously deploy a cdk app via the --no-execute flag:
cdk deploy StackX --no-execute
change_set_name = aws cloudformation list-change-sets --stack-name StackX --query "Summaries[0].ChangeSetId" --output text
aws cloudformation execute-change-set --change-set-name $change_set_name
For my case this works, because I use this method to deploy new stacks only, so there will ever be only exactly 1 change set for this stack and I can retrieve it with the query for the entry with index 0. If you wish to update existing stacks, you will have to select the correct change set from the list returned by the list-change-sets command.
I had a similar issue - I didn't want to keep a console open while waiting for a long-running init script on an ec2 instance to finish. I've hit Ctrl-C after publishing was completed and the changeset got created. It kept running and I've checked the status in the Cloudformation view. Not perfect and not automation-friendly, but I didn't need to keep the process running on my machine.

cloudformation ecs force new deployement

What is the equivalent of --force-new-deployment flag in cloudformation ?
(aws ecs update-service --cluster --service --force-new-deployment)
In my cloudformation template, my docker image tag is latest. However I want to be able to force the redeployment if I replay the stack.
Thanks you
Think of CloudFormation as just a provisioning service or an orchestrator.
Specifying an Image as repository-url/image:tag will definitely fetch the specified image with specific tag from your repo but only upon a stack operation. Once a stack operation is finished, any changes to service will be depending on the next stack operation.
What you can do here is either having either
Cloudwatch Event based rule targeting a Lambda Function whenever there is an image upload to ECR.
Or in case of using some other repo than ECR, have a hook configured in a way that will invoke update-service --cluster --service --force-new-deployment ... command whenever there is a new image upload.
Considerations:
This can lead your stack being in DRIFTED status.
Just make sure to keep your stack's Image property's value in sync with the latest one running within service, whenever you plan to update the stack.
Here's something I came up with to force a deploy after a stack update if needed. The service name is the same as the stack name, so if yours is different you will need to adjust accordingly.
START_TIME=`date +%s`
aws cloudformation deploy --stack-name $STACK_NAME [other options]
LAST_DEPLOY=`aws ecs describe-services --services $STACK_NAME --query "services[0].deployments[?status=='PRIMARY'].createdAt" --output text`
if [ "$START_TIME" -gt "${LAST_DEPLOY%.*}" ]; then aws ecs update-service --service $STACK_NAME --force-new-deployment; fi
The best I have been able to come up with is to change my healthcheck from curl -f to curl --fail and back to force a new deployment. Not ideal but it works.
I'd be interested in a better way, ideally built in to CloudFormation too.

How to add a wait time before executing a step in Bitbucket Pipeline

I have a Bitbucket pipeline where it creates AWS resources using cloudformation and deploys website to it. But deployment fails even the cloudformation creates the stack correctly. What I think the issue is when the deployment happens cloudformation S3 bucket creation may not have been finished.
I have a Hugo website and I have created a bitbucket pipeline to deploy it to server. What it does is it creates S3 bucket using cloudformation to host the website and then upload the Hugo website to it. When I ran the steps in the pipeline manually in a terminal with a delay between each step, it happens successfully. But when it happens on Bitbucket pipeline it gave error saying the S3 bucket that I'm trying to upload content is not available. When I checked in AWS that bucket is actually there. That means Cloudformation has worked correctly. But when the files start to copy, the bucket may have not been available to upload the file. That's my assumption. Is there a workaround for this one. When doing it locally I can wait between the two commands of cloudformation creation and file copying. But how to handle it in Bitbucket pipeline environment. Following is my pipeline code.
pipelines:
pull-requests:
'**':
- step:
script:
- aws cloudformation create-stack --stack-name demo-web --template-body file://cloudformation.json --parameters ParameterKey=S3BucketName,ParameterValue=demo-web
- hugo
- aws s3 cp public/ s3://demo-web/ --recursive
How to handle this scenario in the correct way. Is there a workaround for this situation. Or is the problem that I have identified is not the actual problem.
First, to wait in bitbucket pipelines you should be able to just use sleep x where x is the number of seconds you want to sleep.
A different note - bear in mind that after the first subsequent run of this, deployment will potentially fail the next time as you are using create-stack which will fail if stack already exists...
Using the AWS CloudFormation API for this is best practice.
There is a wait command just as there is a create-stack command, the wait command and its options allows AWS to halt your processing until stack is in COMPLETE status before continuing.
Option 1:
Put your stack creation command into a shell script and as a second step in your shell script you invoke the wait stack-create-complete command.
Then invoke that shell script in pipeline instead of the direct command.
Option 2:
In your bitbucket pipeline, right after your create command, invoke the aws cloudformation await complete command before the upload/hugo command.
I prefer option one because it allows you to manipulate and trace the creation as you wish.
See documentation: https://docs.aws.amazon.com/cli/latest/reference/cloudformation/wait/stack-create-complete.html
Using the CloudFormation API is great because you don't have to guess as to how long to wait, it is more guaranteed to be available.

How can i execute cloudformation changesets in ansible

I am using ansible in cloudformation
https://docs.ansible.com/ansible/2.4/cloudformation_module.html
But i could not find any way how to execute changesets using ansible
You would probably need to use the AWS CLI in an ansible command.
Something like:
- name: Execute a specific changeset
command: aws cloudformation execute-change-set --change-set-name arn:aws:cloudformation:us-east-1:123456789012:changeSet/SampleChangeSet/1a2345b6-0000-00a0-a123-00abc0abc000
Note that you would need to set the AWS_ACCESS_KEY and AWS_SECRET_KEY appropriately.
Also keep in mind the following, from the CloudFormation documentation:
After you execute a change set, AWS CloudFormation deletes all change
sets that are associated with the stack because they aren't valid for
the updated stack. If an update fails, you need to create a new change
set.
So you may find it more helpful to create a CloudFormation stack via ansible, rather than create a changeset somewhere that would just be run once from ansible and subsequently deleted.