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.
Table of Contents:
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.
Before we get started ensure that you have the following in place:
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.
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.
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.
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.
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.
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.
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.
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.