# How to launch multiple EC2 instances in one go using Simple and Easy Terraform Code

![](https://3357139798-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FXvKD07lcqUSM1KyWllyX%2Fuploads%2F8Muem1uQZGYP47CAdOKu%2F0.png?alt=media)

**Overview:**

**Infrastructure as Code** tools like **Terraform** can be used to Build, change, and destroy AWS infrastructure, especially when dealing with multiple resources in a complex cloud infrastructure.

In this blog, we will learn how Terraform can simplify your life by launching multiple instances in one go.

#### **Prerequisites** <a href="#id-7ueloq94tcwx" id="id-7ueloq94tcwx"></a>

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

* An [AWS account](https://aws.amazon.com/premiumsupport/knowledge-center/create-and-activate-aws-account/)
* [Terraform](https://www.terraform.io/docs/enterprise/install/index.html) - This tutorial will use Terraform v1.4.2 on an Ubuntu Linux machine.
* A code editor - Make sure you have a code editor that can support HCL (Terraform language). Hint: Try out [Visual Studio (VS) Code](https://code.visualstudio.com/).

#### **Steps to launch multiple EC2 Instances using Terraform Configuration files:** <a href="#rno7niiavx4v" id="rno7niiavx4v"></a>

**Step 1:**&#x4F;pen the terminal on Visual Studio code and create a folder named **terraform-ec2-demo** in the home directory and then change(cd) the working directory to that folder.

This folder will contain all the configuration files you'll be working with to launch ec2 instances.

![](https://3357139798-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FXvKD07lcqUSM1KyWllyX%2Fuploads%2FlbeAujfSN8JhdBpRhCjd%2F1.png?alt=media)

**Step 2:** Now we need to set up our AWS User secret and access keys on our local machine. Also, this avoids exposing our aws credentials in the terraform code.

For this, you must have configured AWS CLI on your machine.

**mudasir\@linux:\~/terraform-ec2-demo$ aws configure**

**AWS Access Key ID \[None]: \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*XBKG**

**AWS Secret Access Key \[None]: \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*Csoz**

**Default region name \[None]: us-east-1**

**Default output format \[None]:**

**Step 3**: Preparing the Terraform Configuration files.

* First of all, we will create a provider.tf file inside the **\~/terraform-ec2-demo** directory.
* 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-1' region.
* 'aws' is the name of the provider.

![](https://3357139798-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FXvKD07lcqUSM1KyWllyX%2Fuploads%2F21drGHEJoVog9WOEK9kI%2F2.png?alt=media)

Now in order to let Terraform know about the added provider we need to run **terraform init** command to initialize the [plugins](https://www.terraform.io/docs/extend/how-terraform-works.html) 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 the Terminal stating: **Terraform has been successfully initialized**.

![](https://3357139798-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FXvKD07lcqUSM1KyWllyX%2Fuploads%2FlZqDELjOTC3T05dGWrZI%2F3.png?alt=media)

We can also run **terraform validate** command to validate the code in the providers.tf file.

![](https://3357139798-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FXvKD07lcqUSM1KyWllyX%2Fuploads%2FZF48TmQEdwgxI3oZyE2A%2F4.png?alt=media)

Now let's create another file named ec2.tf that will define all the resources you wish to launch or manage with terraform.

Open the editor and copy-paste the below configuration into the ec2.tf inside the ***\~/terraform-ec2-demo*** directory.

![](https://3357139798-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FXvKD07lcqUSM1KyWllyX%2Fuploads%2Fji2baRm4KpxYi93jpRTc%2F5.png?alt=media)

Lets’s go through the various terms used in the above code:

* **aws\_instance :** is used to create the EC2 instances in the AWS console
* **ami:** Allows you to launch instances containing all the required softwares and operating systems. In our case, we are using Amazon Linux AMI, and can be copied from the AWS console under launch an instance wizard:

![](https://3357139798-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FXvKD07lcqUSM1KyWllyX%2Fuploads%2FApmOvGMD2vDvf9szhJ27%2F6.png?alt=media)

* **Instance\_type:** This allows you to Select an instance type that meets your computing, memory, networking, or storage needs.
* **count =** 5 signifies that five resources of the same kind will be provisioned.
* **tags:** Tags allow you to define the specific resource with a label, which is further beneficial for cost calculations.

#### **Step 4**: Running Terraform commands to Launch multiple AWS EC2 instances. <a href="#uhis9bv8zfds" id="uhis9bv8zfds"></a>

* Since we have already applied terraform init command to initialize the [plugins](https://www.terraform.io/docs/extend/how-terraform-works.html) and providers, so our 2nd command would be **Terraform plan** command. This gives us an overview of which resources will be provisioned in your infrastructure.

**terraform plan**

* The **terraform plan** command creates an execution plan, which lets you preview the changes that Terraform plans to make to your infrastructure.
* The below output shows that Terraform will perform the following actions such as creating resource -aws\_instance with the mentioned ami and so on and so forth.
* At the end of the output you would see something like this:

**Plan: 5 to add, 0 to change, 0 to destroy**

![](https://3357139798-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FXvKD07lcqUSM1KyWllyX%2Fuploads%2FvZFb5zwI7ZRAx2Kzx343%2F7.png?alt=media)

* Finally, to launch multiple instances in the AWS console, we will run the **terraform apply** command.
* The **terraform apply** command executes the actions proposed in a Terraform plan.
* **Terraform apply** command reads all the code in the configuration file such as ec2.tf and then uses the provider in provider.tf file, it connects to the AWS account and launches the ec2 instances.

![](https://3357139798-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FXvKD07lcqUSM1KyWllyX%2Fuploads%2Fv3JSBN9KyGadUHKrBP0W%2F8.png?alt=media)

As seen in the above output five resources have been successfully added.

#### **Step 5**:Verifying the AWS EC2 instances on AWS Console: <a href="#urg73d5uyg89" id="urg73d5uyg89"></a>

* Login to your AWS account. After you log in to the AWS console go to the EC2 dashboard and check the instances(running) section as shown below:

![](https://3357139798-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FXvKD07lcqUSM1KyWllyX%2Fuploads%2FDbcf8K8NtXS0uoqIknnk%2F9.png?alt=media)

You can also view the detailed view of all the five Instances such as Name, Instance ID, Instance type, and AZ.

![](https://3357139798-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FXvKD07lcqUSM1KyWllyX%2Fuploads%2FLXXEUW8piDfKtrT0IoZg%2F10.png?alt=media)

### **Destroy** <a href="#id-6ne7xepa7nej" id="id-6ne7xepa7nej"></a>

#### The **terraform destroy** command terminates resources managed by your Terraform project. This command is the inverse of terraform apply in that it terminates all the resources specified in your Terraform state. It does not destroy resources running elsewhere that are not managed by the current Terraform project. <a href="#id-5bc87z901rry" id="id-5bc87z901rry"></a>

#### **Conclusion** <a href="#id-9qijnyqlz9gi" id="id-9qijnyqlz9gi"></a>

#### In this tutorial, we learned how to launch multiple AWS EC2 instances using Terraform and terraform count functionality. <a href="#riy5z11pulqe" id="riy5z11pulqe"></a>

**Thanks**

#### &#x20;<a href="#vu7pxnuvd469" id="vu7pxnuvd469"></a>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mudasir-1.gitbook.io/multiple-ec2-instances-using-terraform/page-1/how-to-launch-multiple-ec2-instances-in-one-go-using-simple-and-easy-terraform-code.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
