DEV Community

Cover image for How to create a fleet of AWS EC2s with Terraform?
AWS Community Builders

How to create a fleet of AWS EC2s with Terraform?

Augusto Valdivia
AWS community builder | 5 x times AWS certified
・3 min read

There were times when companies of any size were limited by the capabilities of their compu power but thanks to cloud computing not anymore. In this article we will learn about some of the advantages of using Amazon Web Services (AWS) and how to dynamically create virtual machines (EC2) that can help with these limitations in minutes. In this article I will also introduce Terraform and how it can be used to create EC2s-AMIs in a dynamic and productive way.


What are virtual machines-EC2?

Virtual machines or AWS EC2, also called instances, are different operating systems (OS), which can be rented from AWS. Some examples of Os that AWS offers are: Amazon Linux, Red Hat Enterprise, MacOs, Suse Linux, Ubuntu, Microsoft Windows.

As you can see, the selection of OS is extensive which gives a great advantage to being able to choose EC2 based on OS, RAM, and CPU requirements. EC2 also offers Infrastructure as a Service (IaaS) which provides complete control of computing resources, which can be increased and decreased based on demand. One of the great benefits of using EC2 is the savings in administrative cost, planning and investment in extra-hardware that is not necessary, which turns companies large costs into much smaller one. I'll explain more details in a moment.

Cost = Demand * Hour

Cost = πŸ’° πŸ’° πŸ’° πŸ’° πŸ’° πŸ’° πŸ’° πŸ’°
Demand = πŸ–₯ πŸ–₯ πŸ–₯ πŸ–₯ πŸ–₯ πŸ–₯ πŸ–₯ πŸ–₯
x Time = ⏱

EC2 are classified into five types:

  • General purpose
  • Optimized computing
  • Optimized memory
  • Accelerated computing
  • Optimized storages

Diagram 1: AWS EC2


Considering that we can create or configure any number of AWS-EC2 through the console over and over again this takes time which is a problem. Building infrastructure using code (IaC) alleviates this problem. I present to you one of my favorite tools - Terraform.

What is Terraform?

Terraform is a powerful declarative tool that helps us manage the vast majority of cloud services, as well as providing consistency and visibility into infrastructure changes that occur across different workspaces and environments. This functionality is an important piece of project lifecycle quality improvement, and it also keeps software engineers and others on the same team informed.

When using Terraform, we do not need to log in to the AWS management console. This reduces service creation time. It also allows you to detect potential security risks and errors early in the project.

With Terraform we can work directly from the terminal of our computer using a couple of command lines.

In this project you will find a repo that stores a list of Terraform files which you can use to configure and create one or more EC2s-AMIs in a matter of minutes.

Terraform: Files-preview


Now is the time to get down to work on the construction of this infrastructure.

Find the repo for this project and instructions here

Terraform functions, arguments, and expressions used in this project:

types and values

Enjoy πŸ‘©β€πŸ’» and Keep buildingπŸ‘·β€β™€οΈ!

Discussion (2)

mimahmed profile image
Mim Ahmed

Nice Article.

valaug profile image
Augusto Valdivia Author

Thank you @mimahmed