Introduction
Deploying an EC2 instance on AWS is a fundamental step in setting up cloud-based applications. This article will guide you through creating an EC2 instance using Terraform, generating an SSH key pair for secure access, and installing essential software inside the instance. By automating infrastructure provisioning with Terraform, you ensure consistency, repeatability, and efficiency.
Prerequisites
Before you begin, ensure you have the following installed on your local machine:
Terraform Configuration
1. Define the Infrastructure (main.tf
)
This file contains the Terraform configuration for deploying an EC2 instance along with its key pair.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}
tls = {
source = "hashicorp/tls"
version = "~> 4.0"
}
}
required_version = ">= 1.2.0"
}
provider "aws" {
region = var.aws_region
}
# Generate an RSA private key
resource "tls_private_key" "nb-keypair" {
algorithm = "RSA"
rsa_bits = 2048
}
# Save the private key to a local file
resource "local_file" "private_key" {
content = tls_private_key.nb-keypair.private_key_pem
filename = "${path.root}/nb-key-pair.pem"
}
# Create an AWS Key Pair using the generated public key
resource "aws_key_pair" "nb-keypair" {
key_name = "nb-key-pair"
public_key = tls_private_key.nb-keypair.public_key_openssh
}
# Deploy an EC2 instance using the key pair
resource "aws_instance" "demo-instance" {
ami = var.aws_ami_image
instance_type = var.aws_instance_type
key_name = aws_key_pair.nb-keypair.key_name
tags = {
Name = "Demo EC2 Instance"
}
}
2. Define Variables (variables.tf
)
This file allows customization of key parameters for the deployment
variable "aws_key_name" {
description = "The name of the AWS Key Pair to use"
type = string
default = "nb-key-pair"
}
variable "aws_ami_image" {
description = "The AWS AMI to use"
type = string
default = "ami-09042b2f6d07d164a"
}
variable "aws_region" {
description = "The AWS region to deploy resources"
type = string
default = "eu-central-1"
}
variable "aws_instance_type" {
description = "The AWS instance type to use"
type = string
default = "t2.micro"
}
3. Define Outputs (output.tf
)
These outputs will provide useful connection details.
output "public_ip" {
value = aws_instance.demo-instance.public_ip
}
output "ssh_command" {
value = "ssh -i ${path.root}/nb-key-pair.pem ec2-user@${aws_instance.demo-instance.public_ip}"
sensitive = true
}
Initializing and Applying Terraform
To deploy the infrastructure, run the following Terraform commands:
Terraform initialization
terraform init
output:
Terraform validation
terraform validate
output:
terraform plan
Terraform Apply
Once applied, Terraform will output the public IP of your EC2 instance and an SSH command to connect.
terraform apply -auto-approve
Connecting to the EC2 Instance
1. Set File Permissions
Ensure your private key file has the correct permissions:
chmod 400 nb-key-pair.pem
2. SSH into the Instance
Use the provided Terraform output to SSH into your instance:
ssh -i nb-key-pair.pem ubuntu@<public_ip>
Installing Required Packages
After connecting to the instance, update the package list and install Git:
sudo apt update
sudo apt install -y git
output:
And we can verify the installation of git by using
git --version
output:
You can also install other tools based on your needs e.g docker, docker compose and so on, and also feel free to modify the code and change the region if you will prefer another region
Great, after everything we just have to exit
from the ec2 instance and destroy using terrafrom destroy -auto-aprove
tf destroy -auto-approve
Conclusion
By following these steps, you have successfully deployed an EC2 instance using Terraform, created an SSH key pair for secure access, and installed necessary software inside the machine. If you face any challenges, feel free to drop a comment or ask questions.
Happy coding! 🚀
Ref:
Hashicorp
Top comments (0)