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.

Pre-Requisites

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.

What is CloudFormation?

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…

CloudFormation Template

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.

Create the CloudFormation Stack

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

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

Updating a CloudFormation Stack

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

Removing a CloudFormation Stack

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

Closing

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

Any questions or comments? Leave them below.

comments powered by Disqus

See Also