Squadcast made it to the "Best Software Award" in the IT Management category by G2 🎉 Read full report here.

How to deploy multiple EC2 instances in one go using Terraform

November 30, 2021
Share this post:
How to deploy multiple EC2 instances in one go using Terraform
November 30, 2021
Share this post:
Squadcast way to resolve Incidents
Subscribe to our latest updates
Enter your Email Id
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Infrastructure as Code tools like Terraform provides greater support for automation, especially when dealing with multiple resources in a complex cloud infrastructure. In this blog, learn how Terraform can make your life simple by launching multiple instances in one-go.

If you are new to Amazon AWS and need to create or launch multiple resources in one go, then nothing is better than using Terraform count.

The Terraform count argument allows you to launch as many resources as you wish, of the same kind.

In this tutorial with the help of a step by step guide you will learn how to launch multiple ec2 instances in an AWS account using Terraform count!

Let's dive in!

This is the second blog in our 2-part blog series on Terraform. In our first blog, we understood the ease of managing and automating deployments via Infrastructure as Code by using Terraform. In case you missed it, read it here.

Prerequisites

Before we get started ensure that you have the following in place:

  • An AWS account
  • Terraform - This tutorial will use Terraform v1.0.8 already installed on Ubuntu 18.04.5 LTS machine.
  • A code editor - Make sure you have a code editor that can support HCL (Terraform language). Hint: Try out Visual Studio (VS) Code.

Preparing the Terraform Configuration files using Terraform count to launch multiple EC2 instances

Terraform is an automation tool that allows you to work with various cloud providers and their services while allowing you to manage infrastructure in a very easy and effective way. Terraform has its own syntax and configuration files that are either in .tf or .tf.json formatted.

Lets learn how to prepare configuration files that are required to launch multiple AWS EC2 instances:

  • Open your favorite SSH client and connect to the terminal.
  • Create a folder named terraform-count-ec2-demo in the home directory and then change (cd) the working directory to that folder. This folder will contain all of the configuration files you'll be working with to launch ec2 instances.

<p> CODE: https://gist.github.com/ShubhanjanMedhi-dev/bc65ec70249120544e30f04f2ca992cf.js</p>

  • Next open the editor you wish to use and copy-paste the below configuration in the main.tf inside the ~/terraform-count-ec2-demo directory. This main.tf file is used to define all the resources you wish to launch or manage with terraform. Below are some high level walkthroughs of the code:
    • count = 4 signifies that Four resources of the same kind will be provisioned.
    • Amazon machine image (ami) - Allows you to launch instances containing all the required softwares and operating system.
    • tags - Tags allow you to define the specific resource with a label, which is further beneficial for cost calculations.
    • aws_instance - Creates the EC2 instances in AWS console.
    • count.index - Allows you to define the name according to index. For example if the value of count is 4 then index starts with ( 0 to 3 )
    • Lookup retrieves the value of a single element from a map, given its key. If you don't declare any key then the default value is considered.

<p> CODE: https://gist.github.com/ShubhanjanMedhi-dev/0b0a541b583605758f5f4cad1e1faa66.js</p>

  • Now create another file under the ~/terraform-count-ec2-demo directory and name it as vars.tf. This file will contain all the variables that you referred to in the main.tf configuration file.
    • Variables contain different data types such as strings, boolean, array or even list.

<p> CODE: https://gist.github.com/ShubhanjanMedhi-dev/06f60e9653ac7411b857be5712ea739e.js</p>

  • Create one more file called provider.tf inside ~/terraform-count-ec2-demo directory and again copy-paste the code below to the provider.tf file. The provider.tf file allows you to define the provider that will help you connect to the correct cloud services.
    • The below code will deploy the resources in the 'us-east-2' region.
    • 'aws' is the name of the provider.

<p> CODE: https://gist.github.com/ShubhanjanMedhi-dev/5f2e69429dddfcb040496bba27907037.js</p>

  • Create one last file inside the ~/terraform-count-ec2-demo directory and name it terraform.tfvars. Copy and paste the below code in the terraform.tfvars. The terraform.tfvars file will contain the values that Terraform uses to replace the variable references inside of a main configuration file.

<p> CODE: https://gist.github.com/ShubhanjanMedhi-dev/e0aeb5a2000c35353499073440f15690.js</p>

Running Terraform commands to Launch multiple AWS EC2 instances

In the previous section you learnt to configure all the terraform configuration files that are required to launch multiple ec2 instances. Great work!

But unless you run terraform commands and deploy it to the actual AWS console it will not do much. Now let's run all the required terraform commands and create four ec2 instances of the same kind.

  • Open a terminal and navigate to the ~\terraform-count-ec2-demo directory.

<p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/21dcaa4d130c0339a9eacf696c421959.js</p>

  • Next run the terraform init command in the same directory. The terraform init command initializes the plugins and providers which are required to work with AWS resources that need to be provisioned. After the command runs successfully you will see a message displayed on Terminal stating: Terraform has been successfully initialized.

<p> CODE: https://gist.github.com/ShubhanjanMedhi-dev/d011be9f7072e5618f3531d6297fb835.js</p>

  • Now run the terraform plan command. Terraform plan command gives you an overview of which resources will be provisioned in your infrastructure.

<p> CODE: https://gist.github.com/ShubhanjanMedhi-dev/ca27b906c0f663f4a495fc920fc8890b.js</p>

  • You are about to launch an instance shortly. To launch instances in the AWS console, finally run the terraform apply command as shown below. Terraform apply command reads all the configuration files such as main.tf, variables.tf, terraform.tfvars and then using the provider in provider.tf file it connects to the AWS account and launches the ec2 instances.

<p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/fc08312cfc2b60509eb061c64db9332a.js</p>

Verifying the AWS EC2 instances on AWS Console

Good Job! In the previous section you created all the four instances using Terraform. But it is important to actually verify it on AWS console to confirm. Let's verify the instances.

  • Open your favorite web browser and login to your AWS account. After you login to the AWS console look for EC2 as shown below, which will further show you an option 'EC2'. Then click on 'EC2'.
  • After you navigate to the EC2 instance dashboard, you'll notice that all the four EC2 instances have been created successfully. Also you will notice that all instance IDs must be the same which Terraform returned earlier after running Terraform apply command.

Conclusion

In this tutorial we learnt how to launch multiple AWS EC2 instances using Terraform and terraform count functionality.

So, what do you plan to launch next using Terraform count in your AWS account?

Squadcast is an incident management tool that’s purpose-built for SRE. Your team can get rid of unwanted alerts, receive relevant notifications, work in collaboration using the virtual incident war rooms, and use automated tools like runbooks to eliminate toil.

squadcast
Written By:
November 30, 2021
November 30, 2021
Share this post:
Related Content
CI/CD Pipeline: A Quick Guide
CI/CD Pipeline: A Quick Guide
July 6, 2021
How to improve your influence as an SRE
How to improve your influence as an SRE
November 10, 2021
Understanding Technical Debt for Software Teams
Understanding Technical Debt for Software Teams
January 25, 2022
Experience the Journey from
On-call to SRE
Experience the Journey from On-call to SRE
Squadcast - On-call shouldn't suck. Incident response for SRE/DevOps, IT | Product Hunt Embed
Squadcast is a leader in Incident Management on G2 Squadcast is a leader in Incident Management on G2 Users love Squadcast on G2 Best IT Management Products 2022 Squadcast is a leader in IT Service Management (ITSM) Tools on G2 Squadcast is a leader in IT Service Management (ITSM) Tools on G2 Squadcast is a leader in IT Service Management (ITSM) Tools on G2
Squadcast - On-call shouldn't suck. Incident response for SRE/DevOps, IT | Product Hunt Embed
Squadcast is a leader in IT Service Management (ITSM) Tools on G2 Squadcast is a leader in Incident Management on G2 Users love Squadcast on G2
Best IT Management Products 2022 Squadcast is a leader in IT Service Management (ITSM) Tools on G2 Squadcast is a leader in IT Service Management (ITSM) Tools on G2
Squadcast is a leader in IT Service Management (ITSM) Tools on G2
Copyright © Squadcast Inc. 2017-2022