Terraform Setup for AWS EC2 Instance

Setup your AWS Environments before using Terraform

Before you begin, make sure your AWS environment is set up correctly by following the prerequisites described in the AWS Prerequisites section.

Terraform Code to Initialize AWS EC2 Instance

Create a file named main.tf in the terraform-config-file directory with the following content:

provider "aws" {
  region = "us-east-1" # Replace with your desired AWS region
}

resource "aws_key_pair" "aqai-web-server-access" {
  key_name   = "example-key"
  public_key = file("~/.ssh/id_rsa.pub") # Replace with the path to your public SSH key
}

resource "aws_instance" "aqai-web-server" {
  ami           = "ami-0cff7528ff583bf9a" # Replace with your desired Amazon Machine Image (AMI) ID
  instance_type = "t2.micro"
  key_name      = aws_key_pair.example.key_name

  tags = {
    Name = "aqai-web-server-ec2-instance"
  }
}

Initialize Terraform

To initialize Terraform, run the following command in the directory containing your main.tf file:

terraform init

Apply the Terraform Configuration

To apply the Terraform configuration and create your EC2 instance, run:

terraform apply

Destroy the EC2 Instance

To avoid being charged, you can destroy the EC2 instance with the following command:

terraform destroy

Find the Latest Amazon Machine Image (AMI)

To find the latest Amazon Machine Image (AMI) ID, use the AWS CLI:

aws ec2 describe-images --owners amazon --query 'sort_by(Images, &Name)[-1].ImageId' --output text --region us-east-1 --filters 'Name=name,Values=amzn3-ami-hvm-2.0.????????.?-x86_64-gp2' 'Name=state,Values=available'

This command will provide the latest AMI ID available in the specified region.