Introduction
In this article, we will explore how to create an EC2 (Elastic Compute Cloud) instance using Terraform on AWS (Amazon Web Services). We will assume that you have already configured your AWS CLI credentials as discussed in the previous article and are familiar with the concept of Terraform providers.
Prerequisites
Before proceeding, ensure the following prerequisites are met:
AWS account: You must have an AWS account with administrative access.
AWS CLI configured: Ensure that you have already configured your AWS CLI credentials on your local machine.
Terraform installed: Make sure Terraform is installed on your local machine.
Step 1: Initialize Terraform Configuration
First, create a directory for your Terraform configuration files and navigate into it:
mkdir terraform-ec2 && cd terraform-ec2
Create a new file named main.tf
and open it in a text editor.
Step 2: Configure Terraform Provider
In the main.tf
file, specify the AWS provider and version. Since we have already configured our AWS CLI credentials, Terraform will use these credentials automatically.
provider "aws" {
region = "us-east-1" # Specify your desired AWS region
}
Step 3: Define EC2 Instance Resource
Below the provider configuration, define an EC2 instance resource. You can customize the instance configuration as per your requirements.
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0" # Specify the AMI ID of the instance
instance_type = "t2.micro" # Specify the instance type
key_name = "your-key-pair-name" # Specify the key pair name for SSH access
}
Step 4: Initialize and Apply Terraform Configuration
Initialize the Terraform configuration by running the following command in the terminal:
terraform init
Apply the Terraform configuration to create the EC2 instance:
terraform apply
Terraform will prompt you to confirm the creation of resources. Type yes
and press Enter to proceed.
Step 5: Verify EC2 Instance Creation
Once the terraform apply
command completes successfully, you can verify the creation of the EC2 instance in the AWS Management Console.
Go to the AWS Management Console:
Navigate to the EC2 service and select "Instances" from the left-hand menu. You should see the newly created EC2 instance listed here.
Step 6: Destroy EC2 Instance
When you no longer need the EC2 instance, you can destroy it using Terraform to clean up resources and avoid unnecessary charges.
Run the following command in the terminal:
terraform destroy
Terraform will prompt you to confirm the destruction of resources. Type yes
and press Enter to proceed.
Conclusion
In this article, we learned how to create and destroy an EC2 instance using Terraform on AWS. By leveraging Terraform's infrastructure as code approach, we can easily define, manage, and clean up AWS resources in a reproducible and automated manner. With our AWS CLI credentials already configured and Terraform providers in place, we were able to seamlessly deploy and destroy the EC2 instance with just a few lines of Terraform code.