DEV Community

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

Posted on • Edited on

How to create a fleet of AWS EC2s with Terraform?

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.

Introduction

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

ec2s

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 that you can configure and create one or more EC2s-AMIs in a matter of minutes.

Terraform: Files-preview

#---main/module---

data "aws_ami" "amazon_linux" {
  most_recent = true
  owners      = ["amazon"]

  filter {
    name   = "name"
    values = ["amzn2-ami-hvm-*-x86_64-ebs"]
  }

  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }

  tags = {
    Name = "linux-ami"
  }
}

data "aws_ami" "latest_ubuntu" {
most_recent = true
owners = ["099720109477"]

  filter {
      name   = "name"
      values = ["ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-*"]
  }

  filter {
      name   = "virtualization-type"
      values = ["hvm"]
  }

  tags = {
    Name = "ubuntu-ami"
  }
}

resource "aws_instance" "linux_vm" {
  count = var.instances_count
  ami = data.aws_ami.amazon_linux.id
  instance_type = var.instances_type

  tags = {
    Name = "linux-ec2"
  }
}

resource "aws_instance" "ubuntu_vm" {
  count = var.instances_count  
  ami = data.aws_ami.latest_ubuntu.id
  instance_type = var.instances_type

  tags = {
    Name = "my-ubuntu-ec2"
  }
}
Enter fullscreen mode Exit fullscreen mode

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:

providers
variables
modules
resources
types and values
for_each

Remember to share your thoughts and make sure to follow me for more amazing AWS-related content. Enjoy your coding journey and keep building! πŸ‘©β€πŸ’»πŸ‘·β€β™€οΈ

Top comments (2)

Collapse
 
mimahmed profile image
Mim Ahmed

Nice Article.

Collapse
 
valaug profile image
Augusto Valdivia

Thank you @mimahmed