AWS CLI Commands for CloudFormation - krdheeraj51/aws-labs GitHub Wiki
Introduction
AWS CloudFormation is a service that helps you model and set up your AWS resources so that you can spend less time managing those resources and more time focusing on your applications. The AWS CLI provides a powerful way to interact with CloudFormation from the command line. Here are some essential AWS CLI commands for CloudFormation:
1. Create a Stack
To create a new CloudFormation stack, use the create-stack
command. This command requires a stack name and a template.
aws cloudformation create-stack \
--stack-name STACK_NAME \
--template-body file://TEMPLATE_FILE \
--parameters ParameterKey=KEY,ParameterValue=VALUE
- STACK_NAME: The name of your stack.
- TEMPLATE_FILE: The path to your AWS CloudFormation template file.
- KEY and VALUE: Optional parameters for your template.
2. Update a Stack
To update an existing stack, use the update-stack
command. This is similar to creating a stack but updates the existing resources.
aws cloudformation update-stack \
--stack-name STACK_NAME \
--template-body file://TEMPLATE_FILE \
--parameters ParameterKey=KEY,ParameterValue=VALUE
3. Delete a Stack
To delete a stack, use the delete-stack
command.
aws cloudformation delete-stack --stack-name STACK_NAME
4. Describe Stacks
To get information about your stacks, use the describe-stacks
command.
aws cloudformation describe-stacks --stack-name STACK_NAME
5. List Stacks
To list all your stacks, use the list-stacks
command.
aws cloudformation list-stacks
6. Validate a Template
To validate a template without creating a stack, use the validate-template
command.
aws cloudformation validate-template --template-body file://TEMPLATE_FILE
7. Deploy a Stack
The deploy
command is a more convenient way to create or update a stack. It automatically creates and executes a change set.
aws cloudformation deploy \
--template-file /path/to/template.yaml \
--stack-name STACK_NAME \
--parameter-overrides Key1=Value1 Key2=Value2
- STACK_NAME: The name of the stack to create or update.
- Key1=Value1: Optional parameters for your template.
8. Create a Change Set
To manually manage changes before applying them, you can create a change set.
aws cloudformation create-change-set \
--stack-name STACK_NAME \
--change-set-name CHANGE_SET_NAME \
--template-body file://TEMPLATE_FILE \
--capabilities CAPABILITY_IAM
- STACK_NAME: The name of the stack to update.
- CHANGE_SET_NAME: The name of the change set.
- CAPABILITY_IAM: Required if your template includes IAM resources.
9. Execute a Change Set
After creating a change set, you can execute it to apply the changes.
aws cloudformation execute-change-set --change-set-name CHANGE_SET_NAME --stack-name STACK_NAME
Conclusion
These AWS CLI commands provide a comprehensive way to manage your AWS CloudFormation stacks. Whether you're creating, updating, or deleting stacks, the AWS CLI offers flexibility and automation capabilities that can streamline your workflow.
Additional Resources
- AWS CLI Command Reference: For detailed documentation on all AWS CLI commands, including CloudFormation.
- AWS CloudFormation User Guide: Comprehensive guide to using CloudFormation, including templates, stacks, and resources.
Example Use Cases
- Automating Stack Creation: Use
create-stack
in a script to automate the deployment of infrastructure. - Continuous Integration/Continuous Deployment (CI/CD): Use
deploy
to update stacks as part of a CI/CD pipeline. - Template Validation: Use
validate-template
to ensure templates are correct before deployment.