Using CloudFormation to Manage EC2 in AWS

Share on:

An update to a topic I previously covered! How can we automate management of virtual machines in AWS? Cloud Formation (CFT) is another way that can accomplish it. This blog will identify how you can create, update and delete a CFT stack to be able to manage your EC2 infrastructure.

Amazon Web Services Account

  • This is the infrastructure to run the EC2 virtual machines.

Install the AWS CLI

  • Use aws configure to login to your AWS Account and Region.

AWS Cloudformation is a service that helps you automate the deployment of resources. CloudFormation uses a Template that can be used to Create and then Update a Stack. This allows you to easily handle the provisioning and configuration of AWS Resources. It is an alternative to the many automation tools out there–Terraform, Ansible, Powershell, CLI, etc…

A sample CFT below will deploy an Amazon Linux AMI and install and configure NGINX. All that needs to be update is your subnet, security groups, keypair and tags.

Here is an example of all the properties that can be passed to the EC2 template.

AWSTemplateFormatVersion: 2010-09-09
Description: Deploy an EC2 Instance and Install and Enable NGINX
Resources:
Instance1:
Type: AWS::EC2::Instance
Properties:
ImageId: ami-0efa651876de2a5ce # Amazon Linux 2023 AMI
InstanceType: t2.micro # AWS Instance Type to Use
KeyName: MyAWSKeypair #AWS Keypair
SubnetId: subnet-0eeab############ #Subnet ID
SecurityGroupIds: #List of SecurityGroup IDs
- sg-00###############
- sg-037##############
Tags: #Any Tag Key/Values that need to be added.
- Key: Name
Value: "nginx-cft-01"
- Key: Owner
Value: "David Stamen"
UserData: #UserData thats base64 encoded. To be run on initial launch of machine.
Fn::Base64: !Sub |
#!/bin/bash
sudo yum update -y
sudo yum install nginx -y
sudo systemctl enable nginx
sudo systemctl start nginx

There are many parameters that can be passed into the create stack such as individual parameters if you want to pass them manually into the template body. This example has them all hardcoded. Check out the documentation on all the specific items that can be used to create a CloudFormation Stack.

1aws cloudformation create-stack --stack-name myteststack --template-body file://create_nginx_ec2.yaml
bash

If everything is successful your EC2 instance should be deployed and configured based on your template!

If you ever wish to update a deployed CFT, all you need to do is create a new template, or update the existing one and run the following command.

1aws cloudformation update-stack --stack-name myteststack --template-body file://updated_create_nginx_ec2.yaml
bash

If you ever wish to remove a deployed CFT Stack, all you need to do is delete it through the UI or run the following command.

WARNING: There will be no confirmation, so deletion will be immediate

1aws cloudformation delete-stack --stack-name myteststack
bash

Hopefully this helped you get started with automating EC2 instance deployment with CloudFormation!

Any questions or comments? Leave them below.

See Also