Skip to content

aws cloudformation

  • With CloudFormation, you declare all your resources and dependencies in a template file.
  • The template defines a collection of resources as a single unit called a stack.
  • CloudFormation creates and deletes all member resources of the stack together and manages all dependencies between the resources for you.
# ec2-template.yaml
# Simple EC2 instance
Resources:
  MyInstance:
    Type: AWS::EC2::Instance
    Properties:
      AvailabilityZone: us-east-1a
      ImageId: ami-a4c7edb2
      InstanceType: t2.micro

create-stack

aws cloudformation create-stack \
  --stack-name my-stack \
  --template-body file://~/Documents/ec2-template.yaml

aws ec2 describe-instances \
  --filters "Name=tag:aws:cloudformation:stack-name,Values=my-stack"

update-stack

  • It's not possible to update stack in "ROLLBACK_COMPLETE" status
aws cloudformation update-stack \
  --stack-name my-stack \
  --template-body file://~/Documents/ec2-template-updated.yaml

delete-stack

  • Delete a stack and all its resources
  • Deleted stacks are still there, just at with a Deleted status
aws cloudformation delete-stack --stack-name my-stack

list-stacks

aws cloudformation list-stacks --stack-status-filter CREATE_COMPLETE UPDATE_COMPLETE

describe-stacks

aws cloudformation describe-stacks \
  --stack-name my-stack

get-template

  • Get the template content for a stack
aws cloudformation get-template --stack-name my-stack

list-stack-resources

  • List the resources created by a stack
aws cloudformation list-stack-resources --stack-name my-stack

deploy

  • This command is more high-level and can create, update, or delete stacks
aws cloudformation deploy \
  --stack-name "Karpenter-my-cluster" \
  --template-file karpenter-infra.yaml \
  --capabilities CAPABILITY_NAMED_IAM \
  --parameter-overrides "ClusterName=my-cluster"