Provisioning EC2 Instances
Now that your environment is set up, it’s time to start provisioning resources on AWS. In this part, you’ll use Terraform to create an EC2 instance, which will serve as the compute resource for hosting your web application. You will also configure an SSH key pair to securely access the instance.
Declare Variables for Configuration
Variables allow your Terraform configuration to be dynamic and reusable. Open the variables.tf file and define the following variables:
variable "region" {
default = "us-west-2"
description = "AWS region to deploy resources"
}
variable "instance_type" {
default = "t2.micro"
description = "Instance type for the EC2 instance"
}
variable "key_name" {
description = "Name of the SSH key pair to use for accessing the instance"
}
This allows you to control the deployment region, instance type, and key pair name dynamically.
Configure the EC2 Instance in main.tf
In the main.tf file, define your EC2 instance and the SSH key pair resource:
Provider Configuration: Add the AWS provider, specifying the region:
provider "aws" {
region = var.region
}
Key Pair Resource: Create a key pair to enable SSH access. Terraform will use the public key from your local machine to configure the EC2 instance:
resource "aws_key_pair" "web_key" {
key_name = var.key_name
public_key = file("~/.ssh/id_rsa.pub")
}
Replace "~/.ssh/id_rsa.pub" with the path to your actual SSH public key file. Ensure your SSH key exists. If not, generate one using:
ssh-keygen -t rsa -b 2048
EC2 Instance Resource: Define the EC2 instance resource:
resource "aws_instance" "web" {
ami = "ami-0c02fb55956c7d316" # Amazon Linux 2 AMI (replace if needed)
instance_type = var.instance_type
key_name = aws_key_pair.web_key.key_name
tags = {
Name = "WebAppInstance"
}
}
The ami ID specifies the operating system for the instance. The provided ID corresponds to the Amazon Linux 2 AMI in the us-west-2 region. Use the appropriate AMI ID for your chosen region.
Add outputs to outputs.tf
To make it easier to retrieve important information about the instance (e.g., public IP address), define outputs in outputs.tf:
output "instance_public_ip" {
value = aws_instance.web.public_ip
description = "Public IP address of the EC2 instance"
}
output "instance_id" {
value = aws_instance.web.id
description = "ID of the EC2 instance"
}
When you apply your Terraform configuration, these outputs will be displayed.
Initialize Terraform
If you haven’t already initialized Terraform, do so now to download the required AWS provider plugins:
terraform init
This ensures Terraform is ready to manage AWS resources.
Plan and Apply the Configuration
Plan the Configuration: Before applying changes, preview them to ensure everything is configured correctly:
terraform plan
Terraform will display the resources it plans to create, update, or delete. Review the output to verify correctness.
Apply the Configuration: To create the resources, run:
terraform apply
Terraform will prompt you to confirm the operation. Type yes to proceed. Once applied, Terraform will output the instance details, including the public IP address.
Verify the Instance
To confirm the instance is running and accessible, check the public IP address of the instance, which is output by Terraform or can be retrieved with:
terraform output instance_public_ip
SSH into the instance using the key pair:
ssh -i ~/.ssh/id_rsa ec2-user@<instance_public_ip>
Replace <instance_public_ip> with the IP address of the instance. If successful, you will have access to the EC2 instance. Verify the operating system:
uname -a
You have created an EC2 instance using Terraform and configured secure access with an SSH key pair. This EC2 instance will serve as the foundation for hosting your web application in subsequent parts. Next, you’ll enhance the security of your instance by adding a security group to control network access.