Create AWS EC2 Instance Using Terraform
Kundan Antyakula
DevSecOps | AWS Certified (2x) | GitHub Certified (1x) | Kubernetes & Containerization | CI/CD & Infrastructure Automation | Driving Secure & Scalable DevOps Solutions
Introduction
Terraform is an open-source infrastructure as code (IaC) tool that allows you to define and provision data center infrastructure using a high-level configuration language. In this guide, we'll walk you through the steps to launch an Amazon EC2 instance using Terraform. This tutorial assumes you have a basic understanding of AWS and Terraform. We will show how to create an EC2 instance on the Amazon Web Services (AWS) cloud platform using Terraform. We will detail the prerequisites, how to authenticate, how to set up your Terraform configuration files, and how to run through the Terraform lifecycle to initialize, plan, apply, and verify the deployment. We will also look at how to create multiple instances with different configuration values.
Prerequisites
aws configure
Run the following command to generate a new SSH key pair:
ssh-keygen -t rsa -b 4096
You will be prompted to enter a file name to save the key pair. By default, it will be saved in the .ssh directory within your user’s home directory. You can specify a different file name or directory if desired.
You will also be prompted to enter a passphrase for the key pair. While this is optional, it is recommended for added security.
The ssh-keygen command will generate two files: a private key file and a public key file. Keep the private key file secure and do not share it with anyone. The public key file can be shared with Amazon EC2 instances to enable SSH access.
To use the key pair with an Amazon EC2 instance, you must add the public key to the instance during its configuration with Terraform.Step-by-Step Guide
1. Set Up Your Terraform Configuration Directory
Create a new directory for your Terraform configuration files. This helps keep your project organized.
mkdir terraform-ec2
cd terraform-ec2
2. Define AWS Providers
Create a file named provider.tf to specify the AWS providers. Here, we're using AWS region: us-east-1
provider "aws" {
alias = "use1"
region = "us-east-1"
}
3. Define Your EC2 Instances
Create a file named main.tf to define the EC2 instances and associated resources.
resource "aws_instance" "ec2_example_use1" {
provider = aws.use1
ami = "ami-01e444924a2233b07"
instance_type = "t2.micro"
tags = {
Name = "Terraform EC2"
}
security_groups = [aws_security_group.web_sg.name]
key_name = "aws_key"
}
4. Define Security Groups
In the same main.tf file or in a separate file (e.g., security.tf), define the security group to allow inbound traffic on ports 22 (SSH) and 80 (HTTP).
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
}
}
5. Associate a Key Pair
Add the key pair resource to your configuration. This key pair will be used to SSH into the EC2 instance.
resource "aws_key_pair" "deployer" {
key_name = "aws_key"
public_key = "ssh-ed25519 AAAAC3NzaC1lZDI1XXXXXXXXXkundansaigopalantyakula@Kundans-MacBook-Air.local"
}
6. Outputs
Create an outputs.tf file to output the public IP address of the EC2 instance, which can be used to SSH into the instance.
output "fetched_info_from_aws" {
value = format("ssh -i keypair ubuntu@%s", aws_instance.ec2_example_use1.public_ip)
}
7. Initialize and Apply Terraform Configuration
Run the following commands in your terminal:
# Initialize the Terraform configuration
terraform init
# Validate the configuration files
terraform validate
# Apply the configuration to create the resources
terraform apply
Terraform will prompt you to confirm before applying the configuration. Type yes to proceed.
If you are confident enough you can direct use --auto-approve directly proceeds for the creation.
Conclusion
By following these steps, you will have successfully launched an EC2 instance on AWS using Terraform. You can SSH into the instance with the provided public key and the IP address output by Terraform.
This guide covers the fundamental process of launching an EC2 instance with Terraform. You can build on this by adding more resources, configuring networking, and exploring advanced Terraform features. Happy Terraforming!