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

6 3 1 1 1

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! πŸ‘©β€πŸ’»πŸ‘·β€β™€οΈ

Playwright CLI Flags Tutorial

5 Playwright CLI Flags That Will Transform Your Testing Workflow

  • 0:56 --last-failed: Zero in on just the tests that failed in your previous run
  • 2:34 --only-changed: Test only the spec files you've modified in git
  • 4:27 --repeat-each: Run tests multiple times to catch flaky behavior before it reaches production
  • 5:15 --forbid-only: Prevent accidental test.only commits from breaking your CI pipeline
  • 5:51 --ui --headed --workers 1: Debug visually with browser windows and sequential test execution

Learn how these powerful command-line options can save you time, strengthen your test suite, and streamline your Playwright testing experience. Click on any timestamp above to jump directly to that section in the tutorial!

Watch Full Video πŸ“ΉοΈ

Top comments (2)

Collapse
 
mimahmed profile image
Mim Ahmed β€’

Nice Article.

Collapse
 
valaug profile image
Augusto Valdivia β€’

Thank you @mimahmed

Best Practices for Running  Container WordPress on AWS (ECS, EFS, RDS, ELB) using CDK cover image

Best Practices for Running Container WordPress on AWS (ECS, EFS, RDS, ELB) using CDK

This post discusses the process of migrating a growing WordPress eShop business to AWS using AWS CDK for an easily scalable, high availability architecture. The detailed structure encompasses several pillars: Compute, Storage, Database, Cache, CDN, DNS, Security, and Backup.

Read full post

πŸ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay