How to Deploy CloudFormation Template using AWS CLI
When it comes to creating and managing resources on AWS, there are many ways in which you can achieve this. AWS CloudFormation is the IaC(Infrastructure as Code) way of provisioning resources.
AWS CloudFormation lets you deploy your resources on AWS in a better and more automated way. You prepare your template and deploy it on AWS. As usual, you can do that using AWS console or AWS CLI.
In this post, you’ll learn how to deploy CloudFormation template using AWS CLI.
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.
Prerequisite
- An AWS account: See How to Setup Free Tier Account on AWS in Right Way
- AWS CLI
- Basic knowledge of CloudFormation
How to Deploy CloudFormation Template using AWS CLI
When you want to deploy a CloudFormation template using AWS CLI, you can use either of the below two commands-
- cloudformation create-stack
- cloudformation deploy
Both of them can help you provision the resource. Now the question is which one to use and why?
To simplify this, I would say, use cloudformation create-stack when you are 100% sure that you are going to create a stack. And then you use cloudformation update-stack to update it. You can use other commands like describe-stack to know stack status.
But the good thing about cloudformation deploy is that it’s an all-rounder command. You just say deploy it and will figure out whether to create a stack or update it based on the changeset.
Note: The changeset is nothing but the difference between what’s deployed and what you are adding on top of it. In the beginning, when the stack doesn’t exist, the deploy command, will simply create the changeset and execute it as the stack doesn’t exist.
So, after all the discussion we are going to use the cloudformation deploy command to deploy our CloudFormation template.
Step 1: Install and Configure AWS CLI
Before you can use AWS CLI to deploy a resource on AWS using CloudFormation, you need to install CLI on your machine and configure it using your credentials(access key/secret key).
You can install AWS CLI using official page. Once installed, you can open a terminal in your system and configure it with your access/secret keys.
Use aws configure command to configure your CLI with your credentials.
aws configure
Here is a step-by-step tutorial on how to do it – How to Install and Configure AWS CLI in your System
Note: If you don’t use CLI on regular basis there is a quicker option from the AWS console itself. You can use AWS CloudShell. Click on the terminal icon on the top menu of your AWS account and a ready-to-use terminal will open. This terminal already has CLI installed and is configured with your credentials.
Suggested Read: All You need to Know about AWS CloudShell – Your Browser Based CLI
Step 2: Prepare a Template
In this post, we are learning how to deploy a CloudFormation resource on AWS. For that, we’ll need a CloudFormation template.
Here is a ready-to-use template from our previous tutorial- How to Create an S3 Bucket using CloudFormation.
It’s a very simple template that creates an s3 bucket in AWS.
AWSTemplateFormatVersion: 2010-09-09
Resources:
S3Bucket:
Type: AWS::S3::Bucket
This is a dead simple template which doesn’t take any parameters. Save this template with .yml extension and keep it handy in a folder, we will deploy this using AWS CLI.
Step 3: Create a Stack using AWS CLI
Open a terminal where you have kept your CloudFormation file.
I opened the terminal and navigated to the path above.
The command to deploy a temple is below-
aws cloudformation deploy --template-file filepath --stack-name my-stack-name
I have replaced the file path and stack name as per my convenience. The template looks like the below now-
aws cloudformation deploy --template-file s3-bucket.yml --stack-name static-website-12345678
Hit enter and wait for it to create the stack.
After a few seconds, the stack creation is completed and this is how you get a success message.
As you can see, bucket creation is successful. At this moment you can log in to AWS and verify the CloudFormation stack or s3 bucket.
Here is how it looks in the console-
AWS cloudformation deploy parameters example
We have successfully deployed a CloudFormation template. But our template was dead simple and It didn’t have any parameters. Let’s update our template to add a few parameters.
AWSTemplateFormatVersion: 2010-09-09
Description: CloudFormation template for s3 bucket
Parameters:
BucketName:
Type: String
Description: BucketName
Default: i-named-this-bucket
Resources:
S3Bucket:
Type: AWS::S3::Bucket
Description: Creating Amazon S3 bucket from CloudFormation
Properties:
BucketName: !Ref BucketName
Outputs:
S3Bucket:
Description: Bucket Created using this template.
Value: !Ref S3Bucket
To start with, I have added a simple parameter, BucketName in our template.
What do you think the command will look like with parameters?
Well, here is the command-
aws cloudformation deploy --template-file filepath --stack-name my-stack-name --parameter-overrides Key1=Value1 Key2=Value2
Let’s change this as per our stack.
aws cloudformation deploy --template-file s3-bucket.yml --stack-name static-website-12345678 --parameter-overrides BucketName=i-named-this-bucket-98765
So you see –parameter-overrides do this job for us.
Let’s run the command now-
Stack creation with parameter is successful.
You can see that bucket is created with the name we gave-
Passing param as JSON file to cloudformation deploy
While passing the parameter, the above way is great but it’s not feasible when you have many parameters. The command becomes cumbersome. So the best way is to manage your parameters separately in a file. And refer it from your template.
Let’s see how we can do it.
You can mention the parameters like below-
[
{
"ParameterKey": "Key1",
"ParameterValue": "Value1"
},
{
"ParameterKey": "Key2",
"ParameterValue": "Value2"
}
]
For example, this is what my param file looks like-
[
{
"ParameterKey": "BucketName",
"ParameterValue": "i-named-this-bucket-98765"
}
]
You can refer to the file like shown below-
aws cloudformation deploy --template-file s3-bucket.yml --stack-name static-website-12345678 --parameter-overrides file://param.json
As you can see stack creation is successful using parameters from the file.
Step 4: Update the Stack using CLI
As we talked about already, we’ll be using the deploy command only to update the stack as well. Please note that if you want to review the changeset before executing it you will have to pass a parameter –no-execute-changeset
As you can see, when we pass the –no-execute-changeset parameter, cloudformation creates a changeset and asks us to run describe-change-set command to review it.
Once you have reviewed it and are happy with it. Run the deploy command again by removing –no-execute-changeset option. And as expected, this time the changes will execute and your stack is updated.
Here is the command to pass –no-execute-changeset in case you need-
aws cloudformation deploy --template-file s3-bucket.yml --stack-name static-website-12345678 --parameter-overrides file://param.json --no-execute-changeset
Step 5: Delete the Stack
If you are deploying a CloudFormation template for learning, it’s very important that you delete it once you are done.
Let’s delete the stack by using the below command-
aws cloudformation delete-stack --stack-name my-stack
After replacing the stack name, this is what my command looks like-
aws cloudformation delete-stack --stack-name static-website-12345678
Let’s run it in CLI.
You will notice that the delete command doesn’t return any output. However, you can verify the stack deletion by going to the AWS console or by running describe-stack command in CLI.
In the console, if you see, deletion is started and completed.
if you run describe-stack command in CLI,
You see the below error-
This means the stack is deleted and it doesn’t exist anymore as expected.
Conclusion
In this post, we learnt how to deploy CloudFormation template using AWS CLI.
We started with learning two commands that can help you deploy your template into AWS. Then we learnt why one is better than the other.
Then, we prepared a CloudFormation template and ran it using CLI. We also saw how to pass parameters to deploy command. Additionally, we learnt to use a file to store parameters in case of a large number of parameters and referring that from our template.
Finally, we learnt how to deploy our stack and delete it in the end to avoid any charges.
Related: How to Setup Cost Budget on AWS to Avoid billing Shock
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-
- Add a comment below on what you liked and what can be improved.
- Follow us on
- Share this post with your friends
One thought on “How to Deploy CloudFormation Template using AWS CLI”