Building Scalable Infrastructure on AWS with EC2, RDS, and Terraform

When building cloud applications, understanding compute, databases, and automation is crucial. AWS provides various services to manage these efficiently, including EC2 for compute, RDS for databases, and Terraform for Infrastructure as Code (IaC). This post walks through setting up a complete AWS infrastructure, from launching EC2 instances to automating deployments using Terraform.

Understanding AWS Compute with EC2

EC2 (Elastic Compute Cloud) allows us to run virtual machines (instances) on AWS. These instances can host applications, process requests, and integrate with other AWS services.

Launching an EC2 Instance

To manually launch an EC2 instance using the AWS CLI:

aws ec2 run-instances \
  --image-id ami-0abcdef1234567890 \
  --instance-type t2.micro \
  --key-name my-key \
  --security-groups my-security-group

• image-id specifies the Amazon Machine Image (AMI).

• instance-type defines the hardware (e.g., t2.micro for a small instance).

• key-name allows SSH access.

• security-groups control access permissions.

Once launched, you can connect via SSH:

ssh -i my-key.pem ec2-user@your-instance-ip

To ensure security, only allow necessary traffic in the security group:

aws ec2 authorize-security-group-ingress \
  --group-name my-security-group \
  --protocol tcp --port 22 --cidr 0.0.0.0/0

Load Balancing and Auto Scaling

As applications grow, handling traffic efficiently becomes crucial. AWS provides Application Load Balancers (ALB) to distribute traffic and Auto Scaling Groups (ASG) to adjust the number of instances dynamically.

Creating an ALB and Target Group

aws elbv2 create-load-balancer \
  --name my-load-balancer \
  --subnets subnet-abc123 subnet-def456 \
  --security-groups my-security-group
aws elbv2 create-target-group \
  --name my-target-group \
  --protocol HTTP --port 80 \
  --vpc-id vpc-xyz123

Register EC2 instances with the target group:

aws elbv2 register-targets \
  --target-group-arn arn:aws:elasticloadbalancing:region:123456789012:targetgroup/my-target-group \
  --targets Id=i-1234567890abcdef0 Id=i-0abcdef1234567890

Create a listener to forward HTTP traffic:

aws elbv2 create-listener \
  --load-balancer-arn arn:aws:elasticloadbalancing:region:123456789012:loadbalancer/app/my-load-balancer \
  --protocol HTTP --port 80 \
  --default-actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:region:123456789012:targetgroup/my-target-group

Now, hitting the ALB URL routes traffic to the registered EC2 instances.

Deploying a Database with Amazon RDS

For scalable applications, AWS Relational Database Service (RDS) manages databases without manual maintenance.

Creating an RDS Instance (MySQL)

aws rds create-db-instance \
  --db-instance-identifier mydb \
  --db-instance-class db.t3.micro \
  --engine mysql \
  --allocated-storage 20 \
  --master-username admin \
  --master-user-password secretpass \
  --vpc-security-group-ids sg-xyz123

Retrieve the database endpoint:

aws rds describe-db-instances --query "DBInstances[*].Endpoint.Address"

Now, connect from an EC2 instance:

mysql -h your-db-endpoint -u admin -p

Infrastructure as Code with Terraform

Manually setting up AWS resources can be time-consuming. Terraform automates infrastructure deployment using declarative configuration files.

Defining EC2 in Terraform

Create a file main.tf:

provider "aws" {
  region = "us-east-1"
}

resource "aws_instance" "web" {
  ami           = "ami-0abcdef1234567890"
  instance_type = "t2.micro"
  key_name      = "my-key"

  tags = {
    Name = "Terraform-Instance"
  }
}

Apply the configuration:

terraform init
terraform apply

This provisions an EC2 instance with the defined parameters.

Automating Load Balancing and RDS with Terraform

Load Balancer Configuration

resource "aws_lb" "app_lb" {
  name               = "app-lb"
  internal           = false
  load_balancer_type = "application"
  security_groups    = [aws_security_group.lb_sg.id]
  subnets           = aws_subnet.public[*].id
}

resource "aws_lb_target_group" "app_tg" {
  name     = "app-target-group"
  port     = 80
  protocol = "HTTP"
  vpc_id   = aws_vpc.main.id
}

Deploying RDS with Terraform

resource "aws_db_instance" "db" {
  identifier        = "mydb"
  engine           = "mysql"
  instance_class   = "db.t3.micro"
  allocated_storage = 20
  username         = "admin"
  password         = "secretpass"
  vpc_security_group_ids = [aws_security_group.db_sg.id]
}

Run Terraform commands:

terraform init
terraform apply

Terraform ensures all resources are created and linked correctly.

Conclusion

By using AWS EC2, Load Balancers, RDS, and Terraform, we can build a robust, scalable infrastructure. Key takeaways:

EC2 hosts compute workloads.

Load Balancers distribute traffic efficiently.

RDS provides managed database solutions.

Terraform automates infrastructure provisioning.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *