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
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
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"
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.
Finally to delete a stack
aws cloudformation delete-stack --stack-name cfstack1