Introduction
Provisioning resources on AWS can be a daunting task, but with Terraform, it's straightforward and efficient.
Terraform is an open-source infrastructure as code tool created by HashiCorp that allows you to define and manage your cloud infrastructure using a simple configuration language.
In this guide, we will walk you through the steps to provision an EC2 instance on AWS using Terraform.
Prerequisites
Before we start, make sure you have the following:
AWS CLI Installed : The AWS Command Line Interface (CLI) lets you manage AWS services from the command line and automate them through scripts. You can download and configure the AWS CLI from here.
AWS IAM User : AWS Identity and Access Management (IAM) helps you securely control access to AWS resources. You'll need an IAM user with sufficient permissions to create and manage EC2 instances.
Terraform Installed : Download and install Terraform.
Setting Up AWS Credentials
To connect your AWS account with Terraform, you need to export your AWS access keys to your machine. Replace <access key>
and <secret access key>
with your actual keys:
export AWS_ACCESS_KEY_ID=<access key>
export AWS_SECRET_ACCESS_KEY=<secret access key>
Installing Required Providers
Terraform uses providers to interact with different cloud services. For AWS, we use the AWS provider. Here’s how you can configure it in your Terraform script:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.16"
}
}
required_version = ">= 1.2.0"
}
Specifying the AWS Region
Define the region where you want to deploy your instances. For this guide, we will use the us-east-1
region:
provider "aws" {
region = "us-east-1"
}
Task 01 : Provisioning an AWS EC2 Instance
In this task, we will provision an EC2 instance using Terraform. Here is the Terraform configuration script:
resource "aws_instance" "aws_ec2_test" {
count = 4
ami = "ami-08c40ec9ead489470"
instance_type = "t2.micro"
tags = {
Name = "TerraformTestServerInstance"
}
}
Detailed Solution Explained
AWS CLI Configuration:
Install the AWS CLI: Follow the instructions on the AWS CLI Installation Guide to install the AWS CLI.
Configure the AWS CLI: Open your terminal and run
aws configure
. You will be prompted to enter yourAWS Access Key ID
,AWS Secret Access Key
, default region name (e.g.,us-east-1
), and default output format (e.g.,json
).aws configure
Create a Terraform Configuration File:
Create a new directory for your Terraform project. Inside the directory, create a file named
main.tf
.Add the following content to
main.tf
:terraform { required_providers { aws = { source = "hashicorp/aws" version = "~> 4.16" } } required_version = ">= 1.2.0" } provider "aws" { region = "us-east-1" } resource "aws_instance" "aws_ec2_test" { count = 4 ami = "ami-08c40ec9ead489470" instance_type = "t2.micro" tags = { Name = "TerraformTestServerInstance" } }
Initialize Terraform:
In your terminal, navigate to your project directory and run the
terraform init
command. This command initializes the project and downloads the necessary provider plugins.terraform init
Plan the Deployment:
Run
terraform plan
to see a preview of the resources that Terraform will create. This step helps ensure that everything is configured correctly before making any changes.terraform plan
Apply the Configuration:
Run
terraform apply
to create the EC2 instances. Terraform will show you a summary of the changes and prompt you to confirm. Typeyes
to proceed.terraform apply
Verify the Instances:
- After the apply command completes, go to the AWS Management Console, navigate to the EC2 Dashboard, and verify that the instances have been created. You should see four
t2.micro
instances with the tagName: TerraformTestServerInstance
.
- After the apply command completes, go to the AWS Management Console, navigate to the EC2 Dashboard, and verify that the instances have been created. You should see four
Detailed Explanation of Terraform Script
terraform block : This block specifies the required providers and the minimum version of Terraform needed to run this configuration. We are using the AWS provider version
~> 4.16
.provider block : This block configures the AWS provider with the region where we want to deploy our resources, which in this case is
us-east-1
.resource block : This block defines an AWS EC2 instance resource.
resource "aws_instance" "aws_ec2_test"
: This defines a resource of typeaws_instance
with the nameaws_ec2_test
.count = 4
: This creates four instances of the defined resource.ami = "ami-08c40ec9ead489470"
: This specifies the Amazon Machine Image (AMI) ID to use for the instances. You can find the AMI ID for your region in the AWS Management Console.instance_type = "t2.micro"
: This specifies the instance type to use.t2.micro
is a free tier eligible instance type suitable for low-traffic workloads.tags = { Name = "TerraformTestServerInstance" }
: This adds a tag to the instances with the keyName
and valueTerraformTestServerInstance
.
Conclusion
By following these steps, you have successfully provisioned EC2 instances on AWS using Terraform.
This guide provided a comprehensive walkthrough, ensuring that even new learners can understand and apply the concepts.
Terraform is a powerful tool that simplifies infrastructure management, making it easier to deploy, manage, and scale resources in a reproducible manner. With Terraform, you can manage your infrastructure as code, ensuring consistency and enabling version control. Happy provisioning!