AWS CloudFormation create-stack vs deploy: Which One to Use?

AWS cloudformation create-stack vs deploy Which One to Use

AWS cloudformation create-stack vs deploy: Which One to Use?

If you have deployed a CloudFormation template in AWS using CLI, I am sure you had the same confusion which I was having a few days back.

Having two options create-stack and deploy, it becomes quite confusing on which one to use. And more accurately which command is better to deploy a CloudFormation template using AWS CLI.

In this post, I will share my learnings on cloudformation create-stack vs deploy. Moreover, I will clarify why should you choose one over the other.

So stay tuned till the end to know more.

Don’t want to miss any posts from us? join us on our Facebook group, and follow us on Facebook, Twitter, LinkedIn, and Instagram. You can also subscribe to our newsletter below to not miss any updates from us.

Overview

On AWS, there are many ways to create and manage your resource. AWS CloudFormation is one of the popular ways these days. You can create your CloudFormation stack using the AWS console also. However, when we are on the journey of automation, I would vouch for making things as automated as possible.

And in order to do so, you can use AWS CLI to create/manage a CloudFormation stack. cloudformation create-stack and cloudformation deploy are two commands that you can use to create a stack. having said that, when you have two different options to do one thing, it’s always confusing which one to choose.

Let’s understand both of them and see the difference.

Looking for the best course to master AWS CloudFormation? Here is what I used to get myself kickstarted: AWS CloudFormation Master Class

What is cloudformation create-stack?

aws cloudformation create-stack is a CLI command that you can use to create a CloudFormation stack. When using this command, you need to provide a stack name, template location and parameters if any.

Once cloudformation create-stack API call completes successfully, the stack creation starts. You can check the status of the stack through the DescribeStacks operation/CLI call.

This is what the syntax looks like –

aws cloudformation create-stack --stack-name demostack --template-body file://sampletemplate.yml --parameters ParameterKey=KeyPairName,ParameterValue=TestKey

The above command creates a CloudFormation stack from a local file. If you want to refer to your template in the s3 or SSM document store. You can use –templateurl like below-

aws cloudformation create-stack \
 --stack-name demostack \
 --template-url "ssm-doc://arn:aws:ssm:us-east-1:123456789012:document/docName"

To sum it up, cloudformation create-stack can create a stack in AWS and that’s it.

What is cloudformation deploy?

cloudformation deploy is a CLI command that you can use to create and manage your CloudFormation stack. Notice the word manage here. Unlike cloudformation create, deploy can create or update your stack based on the changeset that it creates under the hood.

Changeset means the difference between what’s already there and what you trying to add. In other words. Based on the outcome, it decides what to do.

So this is kind of an all-in-one command. And it is capable of creating or updating your stack intelligently.

This is what the syntax looks like –

aws cloudformation deploy --template-file filepath --stack-name my-stack-name --parameter-overrides Key1=Value1 Key2=Value2

The difference in CloudFormation create-stack vs deploy

When you use cloudformation create-stack command to create a stack, you need to know whether a stack with the same name exists or not. Otherwise, the create operation will error out saying stack always exists.

On top of that, if you need to update your stack, you need to run a different command i.e. cloudformation update-stack. This is not ideal, especially in cases where you want to deploy your template from a script or as part of a CICD pipeline.

On the other hand ->

cloudformation deploy takes advantage of various existing operations like create, update, changeset etc. to automatically decide whether to create a stack or update it.

Additionally when you use –no-execute-changeset flag, it provides you with the command to review your changes and apply them only when you are happy.

Which one to Use: cloudformation create-stack vs deploy?

The create-stack and update-stack were the original operations for creating and updating CloudFormation stacks.

When changesets were added to CloudFormation, the cloudformation deploy command came into the picture. It basically combines the older operations into one. However, you still have create and update available to you in case you need it.

I would say, use cloudformation create-stack when you are 100% sure that you want to create a stack and that’s it. If you further want to make any update or create a changeset or see stack status, you are ready to use different commands for it.

But if you want to utilise new and better toolings by AWS, and looking for a one-stop solution to deploy a CloudFormation template to AWS using CLI, use cloudformation deploy. It is almost always a better choice for deploying your CloudFormation template using AWS CLI. And especially comes in handy while using a script to deploy it.

Conclusion

In this post on cloudformation create-stack vs deploy, we learnt about these two CLI commands that you can use to create your CloudFormation stack.

Then we learnt the similarity and differences between cloudformation create-stack vs deploy.

I hope the post was useful to you. Feel free to leave a comment or provide feedback.

Enjoyed the content?

Subscribe to our newsletter below to get awesome AWS learning materials delivered straight to your inbox.

Don’t forget to motivate me by-

Suggested Read:

Leave a Reply

Your email address will not be published. Required fields are marked *