🚀📅 Day 53 DevOps Challenge - Terraform with AWS: Provisioning Infrastructure💻☁️

🚀📅 Day 53 DevOps Challenge - Terraform with AWS: Provisioning Infrastructure💻☁️

·

4 min read

Provisioning resources on AWS is made simple and efficient with Terraform, an open-source infrastructure as code (IaC) tool provided by HashiCorp. Terraform allows you to define and provision infrastructure using declarative configuration files. In this blog, we'll guide you through the systematic process of provisioning AWS resources using Terraform. 🚀

Prerequisites

Before diving into provisioning resources using Terraform on AWS, ensure you have the following prerequisites in place:

1. AWS CLI Installation

The AWS Command Line Interface (AWS CLI) is an essential tool for managing AWS services. To begin, make sure you have the AWS CLI installed on your machine. This unified tool allows you to control multiple AWS services from the command line and automate them through scripts. 💡

2. AWS IAM User Setup

AWS Identity and Access Management (IAM) is a crucial component for securely controlling access to AWS resources. Set up an IAM user and configure the necessary permissions for interacting with AWS services. This user will be used to authenticate and authorize access to Terraform. 🔒

To establish the connection between your AWS account and Terraform, configure the AWS CLI using the aws configure command to set up the necessary credentials. This eliminates the need to manually export the access keys and secret access keys to your local machine. Use the following command and provide the required information:

aws configure

Follow the prompts to enter your AWS Access Key ID, AWS Secret Access Key, default region, and preferred output format. This ensures a secure and streamlined approach to authenticate and authorize access for Terraform without exposing your credentials in the terminal.

3. Install Required Providers

Terraform requires the AWS provider to interact with AWS services. Ensure that the AWS provider is defined in your Terraform configuration file with the specified version.

## Inside main.tf ##

terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "5.19.0"
    }
  }
}

4. Configure AWS Provider Region

Define the AWS provider and specify the region where you want to provision your instances. 🌎

provider "aws" {
  region = "ap-south-1"
}

Task-01: Provision an AWS EC2 Instance using Terraform

Now, let's create an AWS EC2 instance using Terraform. The following Terraform configuration will provision four EC2 instances of type t2.micro in the specified region.

## Inside main.tf

resource "aws_instance" "aws_ec2_test" {
  count         = 1
  ami           = "ami-0f5ee92e2d63afc18"
  instance_type = "t2.micro"
  tags = {
    Name = "TerraformTestServerInstance"
  }
}

In this configuration, we define an AWS EC2 instance resource named aws_ec2_test. We specify the Amazon Machine Image (AMI), instance type, and assign tags for identification.

Step 1: Initialize Terraform: First, navigate to the directory containing your Terraform configuration file (typically named main.tf). Run the following command to initialize Terraform and download the required provider plugins:

terraform init

This command initializes the current directory as a Terraform workspace and prepares it for further actions.

Step 2: Plan the Infrastructure: Next, you'll want to review the changes that Terraform will make to your infrastructure. Run the following command to create an execution plan:

terraform plan

Terraform will analyze the configuration and display the planned actions without actually making any changes. Verify that the planned actions match your expectations and ensure there are no errors or unintended modifications.

Step 3: Apply the Configuration: Once you are satisfied with the plan, apply the configuration to provision the AWS EC2 instances. Run the following command:

terraform apply

Terraform will prompt you to confirm the planned changes. Type yes and press Enter to proceed with the provisioning.

Terraform will create the specified EC2 instances and any associated resources defined in the configuration.

By following these steps and using Terraform to manage your AWS infrastructure, you'll achieve streamlined and automated provisioning of resources. Terraform's flexibility and ease of use make it a valuable tool for managing your AWS environment effectively. Happy provisioning! 🛠️

Did you find this article valuable?

Support Adarsh Jha by becoming a sponsor. Any amount is appreciated!