# Day 64 - Terraform with AWS   (Jan 29, 2024)

## **🙏 Introduction:**

In this blog, we will learn how to integrate Terraform with AWS.

Provisioning on AWS is quite easy and straightforward with Terraform.

## **Prerequisites**

### **AWS CLI installed**

The AWS Command Line Interface (AWS CLI) is a unified tool to manage your AWS services. With just one tool to download and configure, you can control multiple AWS services from the command line and automate them through scripts.

* Install AWS CLI on EC2 instance
    

```plaintext
sudo apt install awscli
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704808204369/c11d87c6-ef0c-4867-a8d9-01ca85d063f9.png?auto=compress,format&format=webp align="left")

* Check the AWS CLI version
    

```plaintext
aws --version
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704808235352/4ecd91b9-e4a8-409d-b59a-cdff9265d37c.png?auto=compress,format&format=webp align="left")

### **AWS IAM user**

IAM (Identity Access Management) AWS Identity and Access Management (IAM) is a web service that helps you securely control access to AWS resources. You use IAM to control who is authenticated (signed in) and authorized (has permissions) to use resources.

* Create a IAM user
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704808955512/0d0bf245-6ddd-4488-b75f-2f21cff472ff.png?auto=compress,format&format=webp align="left")

* Create Access key for IAM user
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704809014526/4f6be92c-c962-482b-8626-028cbf656433.png?auto=compress,format&format=webp align="left")

* In order to connect your AWS account and Terraform, you need the access keys and secret access keys exported to your machine.
    

```plaintext
export AWS_ACCESS_KEY_ID=<access key>
export AWS_SECRET_ACCESS_KEY=<secret access key>
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704809311424/011ccca5-eae1-4ff4-8b97-06712e3359f4.png?auto=compress,format&format=webp align="left")

### **Install required providers**

```plaintext
terraform {
 required_providers {
        aws = {
        source  = "hashicorp/aws"
        version = "~> 4.16"
        }
    }
        required_version = ">= 1.2.0"
}
```

* **Terraform** block defines the version of Terraform that is required to execute this configuration. In this case, it specifies that the Terraform version must be **\\&gt;= 1.2.0**.
    
* **required\_providers** block declares the AWS provider and its version that Terraform will use for the resources defined in this configuration. In this case, it declares the AWS provider with the source **hashicorp/aws** and specifies that the version of the provider should be **~&gt; 4.16**
    
* Add the region where you want your instances to be
    

```plaintext
provider "aws" {
region = "us-west-2"
}
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704809939957/95b28ac8-d459-4693-86ea-b0c8323f9b44.png?auto=compress,format&format=webp align="left")

# **🎯Task: 1**

* ### **Provision an AWS EC2 instance using Terraform**
    

```plaintext
resource "aws_instance" "aws_ec2_test" {
        count = 2
        ami = "ami-008fe2fc65df48dac"
        instance_type = "t2.micro"
        tags = {
            Name = "TerraformTestServerInstance"
  }
}
```

* The resource block has a resource type of **aws\_instance** and a resource name of **aws\_ec2\_test**
    
* **count** parameter is set to 2
    
* **ami** parameter specifies the Amazon Machine Image (AMI) to use for the instances. In this case, the AMI ID is **ami-008fe2fc65df48dac**
    
* **instance\_type** parameter specifies the type of instance to create. In this case, the instance type is **t2.micro**
    
* **tags** parameter specifies metadata to attach to the instance, in this case, a tag named "Name" with the value "**TerraformTestServerInstance**".
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704810594648/5d237ece-350f-47ef-979e-572d4a08a383.png?auto=compress,format&format=webp align="left")

`terraform init`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704810706537/97bcdccf-addf-4074-a259-29bd0485ebc1.png?auto=compress,format&format=webp align="left")

`terraform plan`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704810791834/357c14a0-4568-4fda-9dd0-c17daffa1659.png?auto=compress,format&format=webp align="left")

`terraform apply`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704810898957/7b5dd60d-fba0-46f8-83e1-7880777eaaec.png?auto=compress,format&format=webp align="left")

* Check on EC2 console
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704811078639/5c00f293-037f-4a4c-9058-648939947c85.png?auto=compress,format&format=webp align="left")
