In this project, we will create a basic AWS infrastructure using Terraform. This project will help you understand how to apply the concepts learned in the previous modules to a real-world scenario. We will cover the following steps:

  1. Setting Up AWS Credentials
  2. Creating a VPC
  3. Creating Subnets
  4. Setting Up an Internet Gateway
  5. Creating Security Groups
  6. Launching EC2 Instances
  7. Outputting Important Information

  1. Setting Up AWS Credentials

Before we start, ensure you have your AWS credentials configured. You can set up your credentials using the AWS CLI or by creating a ~/.aws/credentials file.

aws configure

Alternatively, you can create a ~/.aws/credentials file:

[default]
aws_access_key_id = YOUR_ACCESS_KEY
aws_secret_access_key = YOUR_SECRET_KEY

  1. Creating a VPC

First, let's create a new directory for our project and initialize it with Terraform.

mkdir aws-infrastructure
cd aws-infrastructure
terraform init

Create a main.tf file and add the following code to define a VPC:

provider "aws" {
  region = "us-west-2"
}

resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"

  tags = {
    Name = "main-vpc"
  }
}

  1. Creating Subnets

Next, we will create two subnets within our VPC.

resource "aws_subnet" "subnet1" {
  vpc_id     = aws_vpc.main.id
  cidr_block = "10.0.1.0/24"
  availability_zone = "us-west-2a"

  tags = {
    Name = "subnet1"
  }
}

resource "aws_subnet" "subnet2" {
  vpc_id     = aws_vpc.main.id
  cidr_block = "10.0.2.0/24"
  availability_zone = "us-west-2b"

  tags = {
    Name = "subnet2"
  }
}

  1. Setting Up an Internet Gateway

We need an Internet Gateway to allow our instances to access the internet.

resource "aws_internet_gateway" "igw" {
  vpc_id = aws_vpc.main.id

  tags = {
    Name = "main-igw"
  }
}

resource "aws_route_table" "rt" {
  vpc_id = aws_vpc.main.id

  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.igw.id
  }

  tags = {
    Name = "main-route-table"
  }
}

resource "aws_route_table_association" "a" {
  subnet_id      = aws_subnet.subnet1.id
  route_table_id = aws_route_table.rt.id
}

resource "aws_route_table_association" "b" {
  subnet_id      = aws_subnet.subnet2.id
  route_table_id = aws_route_table.rt.id
}

  1. Creating Security Groups

We will create a security group to allow SSH and HTTP access.

resource "aws_security_group" "web_sg" {
  vpc_id = aws_vpc.main.id

  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }

  tags = {
    Name = "web-sg"
  }
}

  1. Launching EC2 Instances

Now, let's launch two EC2 instances in our subnets.

resource "aws_instance" "web1" {
  ami           = "ami-0c55b159cbfafe1f0" # Amazon Linux 2 AMI
  instance_type = "t2.micro"
  subnet_id     = aws_subnet.subnet1.id
  security_groups = [aws_security_group.web_sg.name]

  tags = {
    Name = "web1"
  }
}

resource "aws_instance" "web2" {
  ami           = "ami-0c55b159cbfafe1f0" # Amazon Linux 2 AMI
  instance_type = "t2.micro"
  subnet_id     = aws_subnet.subnet2.id
  security_groups = [aws_security_group.web_sg.name]

  tags = {
    Name = "web2"
  }
}

  1. Outputting Important Information

Finally, let's output the public IP addresses of our instances.

output "web1_public_ip" {
  value = aws_instance.web1.public_ip
}

output "web2_public_ip" {
  value = aws_instance.web2.public_ip
}

Applying the Configuration

To apply the configuration, run the following commands:

terraform plan
terraform apply

Conclusion

In this project, we created a basic AWS infrastructure using Terraform. We set up a VPC, subnets, an Internet Gateway, security groups, and launched EC2 instances. This project demonstrated how to use Terraform to manage AWS resources and provided a foundation for more complex infrastructure setups.

Next Steps

  • Experiment with adding more resources, such as RDS instances or S3 buckets.
  • Explore using Terraform modules to organize your code better.
  • Try deploying a simple web application on the EC2 instances.

By completing this project, you have gained practical experience in using Terraform to manage AWS infrastructure, which is a valuable skill for any cloud engineer or DevOps professional.

© Copyright 2024. All rights reserved