How to deploy a CloudFormation template using CLI

AWS CloudFormation is a powerful service that enables us to model and provision AWS infrastructure resources declaratively. CloudFormation enables automation of infrastructure provisioning and management, making it suitable for both small-scale deployments and large-scale, complex architectures. We can use CloudFormation alongside other AWS services like AWS CodePipeline and AWS CodeDeploy to implement continuous integration and continuous delivery (CI/CD) pipelines for our infrastructure.

CloudFormation integrates with various AWS services and features, such as AWS CloudTrail for auditing and logging, AWS Identity and Access Management (IAM) for managing permissions, AWS CloudWatch for monitoring, and AWS Config for compliance and governance.

Create a CloudFormation template

Before provisioning and configuring any AWS cloud resources in a CloudFormation stack, we need to create a CloudFormation template. Templates are the blueprints for our AWS infrastructure, describing all the AWS resources we want to create and configure.

Templates are written in a declarative language, and JSON and YAML are currently supported. For the Answer, we’ll use YAML for the provided CloudFormation templates.

We'll create an S3 bucket using CloudFormation so our template will look like this:

AWSTemplateFormatVersion: '2010-09-09'
Parameters:
BucketName:
Type: String
Resources:
MyBucket:
Type: 'AWS::S3::Bucket'
Properties:
BucketName: !Ref BucketName
PublicAccessBlockConfiguration:
BlockPublicAcls: false
BlockPublicPolicy: false
IgnorePublicAcls: false
RestrictPublicBuckets: false

A brief explanation of this template is as follows:

  • Line 1: We specify the template version.

  • Lines 3–5: We list the input parameter, BucketName, that will be used to get the bucket name as input.

  • Lines 7–16: We specify the resources we want to create and their desired configurations.

Deploy the template using CLI

Now, that we have our template we can deploy it to create an S3 bucket. In AWS CLI, we use the aws cloudformation create-stack command to create a stack. Here are some arguments that can be used with this command:

  • stack-name: This is used to name the stack. It is the only required argument for this command.

  • template-body: This is set to a string that specifies the template file for our stack.

  • template-url: This is set to a string, the URL of our template file. This argument is used if our template can be accessed via a URL.

Note: When using the command, we must specify the location of the template file using either template-body or template-url argument.

  • parameters: This is an array used to set values to the required input parameters for stack creation.

Using this command and these arguments, we'll deploy our template to create an S3 bucket. The final form of our command will be as follows:

aws cloudformation create-stack \
--stack-name cfn-template \
--template-body file://template.yaml \
--parameters ParameterKey=BucketName,ParameterValue=<Unique_Name_For_Bucket>

Click the "Run" button in the playground provided below and execute this command in its terminal to initialize the stack creation.

Note: Don't forget to replace the <Unique_Name_For_Bucket> placeholder with a unique name for your bucket.

The playground below also contains a status.sh script that retrieves the status of the cfn-template stack and logs it at 30-second intervals.

Enter your AWS Access_Key_ID and Secret_Access_Key in the widget below before running any commands. If you don’t have these keys, follow this Answer to generate them: How to generate AWS access keys.

Note: The IAM user whose credentials are being used must have the permissions to perform all the required actions.

STACK_NAME="cfn-template"
REGION="us-east-1"

while true; do
    STATUS=$(aws cloudformation describe-stacks --stack-name $STACK_NAME --region $REGION --query "Stacks[0].StackStatus" --output text)
    
    if [ "$STATUS" == "CREATE_COMPLETE" ]; then
        echo "Stack creation complete."
        break
    elif [ "$STATUS" == "ROLLBACK_COMPLETE" ]; then
        echo "Stack creation failed. Check the CloudFormation console for details."
        break
    else
        echo "Current status: $STATUS"
        sleep 30  # Wait for 30 seconds before checking again
    fi
done
CloudFormation template

You'll get the stack ID as the output of the aws cloudformation create-stack stack command.

To monitor the template, paste the following command in the terminal:

./status.sh
Shell script to monitor stack creation

The status.sh script retrieves the status of the cfn-template stack and logs it at 30-second intervals. Successful stack creation will be indicated by "Complete" status of the stack.

In conclusion, deploying a CloudFormation template using the AWS CLI simplifies the process of creating and managing AWS resources. By following a structured approach—creating a template, using the aws cloudformation create-stack command, and monitoring the deployment—we can efficiently automate and scale our infrastructure. This method ensures consistent and repeatable deployments, enhancing the reliability and management of our AWS environments.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved