Introduction
This documentation provides a detailed guide on how to launch an EC2 instance in AWS using Terraform. Terraform is an infrastructure-as-code tool that allows users to define and provision infrastructure using declarative configuration files.
Prerequisites
AWS Account: You must have an AWS account.
AWS CLI Configured: Ensure that the AWS CLI is configured with access keys to manage your AWS resources. You can set it up by running:
aws configure
AWS Access Key ID
AWS Secret Access Key
- Terraform Installed: You must have Terraform installed on your local machine.
Step-by-Step Guide to Launch an EC2 Instance
Step 1: Set Up Working Directory
Create a directory where you will store your Terraform configuration files.
mkdir terraform-ec2
cd terraform-ec2
Step 2: Create a Terraform Configuration File
In this directory, create a file named main.tf
. This file will contain your Terraform configuration for launching an EC2 instance.
touch main.tf
Step 3: Define Terraform Configuration
Open the main.tf
file and add the following code. This configuration defines the AWS provider and specifies the creation of an EC2 instance.
provider "aws" {
access_key = "AKIAQKPILTGGFYQGB"
secret_key = "l1AbwwAr1tyGzcXt66d85eYswKLtFxSFVPv"
region = "ap-south-1"
}
resource "aws_instance" "myos1" {
ami = "ami-078264b8ba71bc45e"
instance_type = "t2.micro"
tags = {
Name = "aws task1"
TEAM = "dev"
}
}
Step 4: Initialize Terraform
Before applying the configuration, you need to initialize your working directory. This will download the necessary provider plugins.
terraform init
Step 5: Validate Configuration
You can validate your configuration to check for syntax errors or misconfigurations.
terraform validate
If everything is correct, you’ll see a message like Success! The configuration is valid.
Step 6: Plan the Terraform Execution
The terraform plan
command helps you preview the changes that Terraform will make to your infrastructure.
terraform plan
This command will display the resources that will be created (EC2 instance, VPC, subnet).
Step 7: Apply the Terraform Configuration
Once you’re satisfied with the planned changes, apply the configuration to create the resources.
terraform apply
Terraform will prompt for confirmation. Type yes
to continue.
Step 8: Verify the EC2 Instance
After Terraform successfully applies the configuration, you’ll see the public IP address of the newly launched EC2 instance in the output. You can also verify the instance by going to the AWS Management Console:
Navigate to EC2 Dashboard.
Check for an instance named "aws task1".
Step 9: Clean Up Resources
Once you no longer need the EC2 instance, you can destroy the resources to avoid unnecessary charges.
terraform destroy
Terraform will ask for confirmation. Type yes
to terminate the EC2 instance and other resources.
Key Terraform Commands:
terraform init
: Initializes the working directory with provider plugins.terraform plan
: Previews the changes that Terraform will make.terraform apply
: Provisions the infrastructure as per the configuration.terraform validate
: Validates the syntax and configuration.terraform destroy
: Deletes all resources created by Terraform.
Conclusion
This documentation provides a complete guide to launching an EC2 instance in AWS using Terraform, including creating a VPC and subnet. You can use Terraform to automate infrastructure deployment and manage AWS resources efficiently.