Automating EC2 Instance Deployment with Terraform and AWS
Terraform is an open-source Infrastructure as Code (IaC) tool that allows you to define and manage your infrastructure using a simple and declarative configuration language. In this article, we'll explore how to use Terraform to create and manage AWS EC2 instances, set up security groups, and configure user data for instance initialization.
Prerequisites
Before we begin, ensure you have the following:
Terraform Configuration
Let's break down the Terraform configuration for creating EC2 instances, setting up security groups, and using user data for instance initialization.
Providers
First, we define the AWS providers for different regions. This allows Terraform to interact with AWS services in these regions.
provider "aws" {
region = "eu-central-1"
}
provider "aws" {
region = "eu-north-1"
alias = "eun1"
}
Key Pair
We define a key pair resource to enable SSH access to the EC2 instances. Replace the public key with your actual public key.
resource "aws_key_pair" "deployer" {
key_name = "aws_key"
public_key = "ssh-ef25519 AXXXXaC1lXXXXXXXX kundansaigopalantyakula@Kundans-MacBook-Air.local"
}
Security Group
Next, we define a security group to allow inbound traffic on ports 22 (SSH) and 80 (HTTP) and outbound traffic on all ports.
resource "aws_security_group" "web_sg" {
name = "web_sg"
description = "Allow inbound traffic on ports 22 and 80"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"] # Allows SSH from anywhere, consider limiting this
}
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"] # Allows HTTP from anywhere
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"] # Allows all outbound traffic
}
}
EC2 Instances
We define multiple EC2 instances in different regions using the specified AMI and instance type. The instances are associated with the previously defined security group and key pair.
Recommended by LinkedIn
resource "aws_instance" "ec2_example_euc1" {
ami = "ami-01e444924a2233b07"
instance_type = "t2.micro"
tags = {
Name = "Terraform EC2"
}
security_groups = [aws_security_group.web_sg.name]
key_name = "aws_key"
}
resource "aws_instance" "ec2_example_eun1" {
provider = aws.eun1
ami = "ami-01e444924a2233b07"
instance_type = "t2.micro"
tags = {
Name = "Terraform EC2"
}
security_groups = [aws_security_group.web_sg.name]
key_name = "aws_key"
}
User Data
User data is used to initialize an EC2 instance when it is launched. In this example, we use a shell script to update the package list, install Apache, and create a simple HTML page displaying the server details.
resource "aws_instance" "ec2_example" {
ami = "ami-0767046d1677be5a0"
instance_type = "t2.micro"
tags = {
Name = "Terraform EC2"
}
security_groups = [aws_security_group.web_sg.name]
key_name = "aws_key"
user_data = <<-EOF
#! /bin/bash
yes | sudo apt update
yes | sudo apt install apache2
echo "<h1>Server Details</h1><p><strong>Hostname:</strong> $(hostname)</p><p><strong>IP Address:</strong> $(hostname -I | cut -d" " -f1)</p>" > /var/www/html/index.html
sudo systemctl restart apache2
EOF
}
Outputs
Finally, we define outputs to display the public IP addresses of the created instances. This allows you to easily SSH into the instances.
output "fetched_info_from_aws_euc1" {
value = format("%s%s", "ssh -i demo-key ubuntu@", aws_instance.ec2_example_euc1.public_ip)
}
output "fetched_info_from_aws_eun1" {
value = format("%s%s", "ssh -i demo-key ubuntu@", aws_instance.ec2_example_eun1.public_ip)
}
output "fetched_info_from_aws" {
value = format("%s%s", "ssh -i demo-key ubuntu@", aws_instance.ec2_example.public_ip)
}
Applying the Configuration
To apply the Terraform configuration, follow these steps:
Initialize Terraform: This step initializes the configuration and downloads necessary provider plugins.
terraform init
Review the Execution Plan: This step shows what changes will be made to the infrastructure.
terraform plan
Apply the Configuration: This step applies the changes and creates the resources defined in the configuration.
terraform apply
Conclusion
By using Terraform, you can automate the deployment of AWS EC2 instances, ensuring consistency and reproducibility across your infrastructure. The use of user data scripts allows for instance initialization, making it easy to configure instances as soon as they are launched. With the ability to define resources in multiple regions and manage security groups, Terraform provides a powerful and flexible solution for infrastructure management.