AWS CloudFormation - CLI Commands

Some of the basic CLI commands to work with CloudFormation

A simple CF template to create a EC2 resource.

Resources:
  AppServer001:
    Type: 'AWS::EC2::Instance'
    Properties:
       ImageId: "ami-0c6120f461d6b39e9"
       InstanceType: "t2.micro"
       SecurityGroups: 
          - "sshSG"
Outputs:
  PublicIp:
    Description: Server Public IP
    Value: !GetAtt AppServer001.PublicIp
    Export:
      Name: !Sub "${AWS::StackName}-PublicIp"

To create a stack:

aws cloudformation create-stack --stack-name cfstack1 --template-body file:///home/ec2-user/CF_Learnings/001_CreateInstance/createInstance.yaml --region ap-southeast-2

2022-06-04 15_17_37-54.66.203.169 (ec2-user).png

To describe stack events:

aws cloudformation describe-stack-events --stack-name cfstack1

describes all events as the resources are created starting from "CREATE_IN_PROGRESS" to "CREATE_COMPLETE"

To list all the resources created:

aws cloudformation list-stack-resources --stack-name cfstack1

2022-06-04 15_32_47-54.66.203.169 (ec2-user).png

To update a stack:

aws cloudformation update-stack --stack-name cfstack1 --template-body file:///home/ec2-user/CF_Learnings/001_CreateInstance/createInstance.yaml

We will now try to add Name tag to the ec2 instance created.

Resources:
  AppServer001:
    Type: 'AWS::EC2::Instance'
    Properties:
       ImageId: "ami-0c6120f461d6b39e9"
       InstanceType: "t2.micro"
       SecurityGroups: 
          - "sshSG"
       Tags:
         -  Key: "Name"
            Value: "AppServer01"
Outputs:
  PublicIp:
    Description: Server Public IP
    Value: !GetAtt AppServer001.PublicIp
    Export:
      Name: !Sub "${AWS::StackName}-PublicIp"

2022-06-04 15_49_27-54.66.203.169 (ec2-user).png

If we check the events, we could see that the status change from "UPDATE_IN_PROGRESS" to "UPDATE_COMPLETE" indicating that the stack update is successful.

2022-06-04 15_49_02-54.66.203.169 (ec2-user).png

Finally to delete a stack

aws cloudformation delete-stack --stack-name cfstack1

2022-06-04 15_54_43-54.66.203.169 (ec2-user).png