Building a Complete AWS Environment with Terraform

Building a Complete AWS Environment with Terraform

ยท

4 min read

In this blog, we'll learn how to use Terraform to provision the AWS services like EC2, Security Groups, S3, DynamoDB, VPC, Subnets, Internet Gateway and Route Tables.

Before you begin

  1. You should have a good understanding of the mentioned AWS services.

  2. Having a solid grasp of Terraform fundamentals is essential as well.

  3. Make sure you have AWS CLI set up on your system. If you don't have it already, click here to set it up.

If you're new to Terraform fundamentals, check out this beginner-friendly guide by clicking here.

Let's Start

This blog will be divided into three phases. In the first phase, we'll set up EC2 instances with security groups. In the second phase, we'll create S3 buckets and DynamoDB tables. In the final phase, we'll configure VPCs, Subnets, Internet Gateway, and Route Tables.

Provisioning EC2 with Security Group

  1. Here's the Terraform code to set up an EC2 instance with security groups. You can copy and use it for your configuration.

     # Main terraform block
     terraform {
       required_providers {
         aws = {
           source  = "hashicorp/aws"
           version = "5.18.1"
         }
       }
     }
    
     # Define your AWS provider configuration
     provider "aws" {
       region = "us-east-1"
     }
    
     # Create a security group
     resource "aws_security_group" "my-sg" {
       name        = "my-ec2-sg" # Give your own name
       description = "Security group for EC2" # Give some description
    
       ingress {
         from_port   = 22
         to_port     = 22
         protocol    = "tcp"
         cidr_blocks = ["0.0.0.0/0"]
       }
     }
    
     # Launch an EC2 instance
     resource "aws_instance" "my-ec2" {
       ami           = "ami-053b0d53c279acc90" # Give any ami (amazon machine image)
       instance_type = "t2.micro" # Give any instance type
       security_groups = [aws_security_group.my-sg.name]
    
       tags = {
         Name = "ec2-server"
       }
     }
    
  2. Now, apply the terraform init command to set up Terraform for this configuration.

  3. Next, run terraform plan to preview what changes will occur when you apply this configuration.

  4. Finally, execute terraform apply to put the configuration into action. Terraform will take care of creating these services for you, automating the process without manual intervention.

  5. Additionally, you can verify the setup by checking the AWS console to ensure that the EC2 instance has been created as expected.

Setting up S3 Bucket and DynamoDB Table

As part of our second phase, we'll proceed to create S3 buckets and DynamoDB tables. Follow the steps outlined below:

  1. Below is the Terraform code for configuring an S3 bucket and DynamoDB Table. Feel free to copy and use it in your setup.

     # Main terraform block
     terraform {
       required_providers {
         aws = {
           source  = "hashicorp/aws"
           version = "5.18.1"
         }
       }
     }
    
     # Define your AWS provider configuration
     provider "aws" {
       region = "us-east-1"
     }
    
     # Create an S3 Bucket
     resource "aws_s3_bucket" "s3-bucket" {
       bucket = "my-s3-bucket" # Give your own bucket name
       acl    = "private"
    
       tags = {
         Name = "my-s3-bucket"
       }
     }
    
     # Create a DynamoDB Table
     resource "aws_dynamodb_table" "dynamodb-table" {
       name           = "my-dynamodb-table" # Give your own table name
       billing_mode   = "PAY_PER_REQUEST"  # Change to "PROVISIONED" for provisioned capacity   
       hash_key = "LockID"
    
       attribute {
         name = "LockID"
         type = "S"
       }
     }
    
  2. Run terraform apply once more and patiently wait for Terraform to create these resources on your behalf.

    Please note that we haven't run terraform init again, assuming we're working in the same directory where we provisioned the EC2 instance. If you're in a different directory, be sure to run terraform init again before proceeding.

  3. You can also confirm the setup by checking the AWS console to ensure that the S3 bucket and DynamoDB table have been created as intended.

Provisioning VPC and all ...

This is our final phase, and now we'll automate the provisioning of VPC, Subnets, Internet Gateway, and Route Tables. To begin, follow the steps outlined below:

  1. Here's the provided Terraform code for setting up a VPC, Subnets, Internet Gateway, and Route Tables. You can use this code in your configuration.

     # Create a VPC
     resource "aws_vpc" "myVPC" {
     cidr_block = "10.0.0.0/16"
     tags = {
       Name = "myVPC"
       }
     }
    
     # Create a public subnet
     resource "aws_subnet" "PublicSubnet" {
       vpc_id           = aws_vpc.myVPC.id
       cidr_block       = "10.0.1.0/24"
     }
    
     # Create a private subnet
     resource "aws_subnet" "PrivateSubnet" {
       vpc_id           = aws_vpc.myVPC.id
       cidr_block       = "10.0.2.0/24"
     }
    
     # Create internet gateway
     resource "aws_internet_gateway" "igw" {
       vpc_id = aws_vpc.myVPC.id
     }
    
     # Create route table for public subnet
     resource "aws_route_table" "PublicRouteTable" {
       vpc_id = aws_vpc.myVPC.id
       route {
         cidr_block = "0.0.0.0/0"
         gateway_id = aws_internet_gateway.igw.id
         }
     }
    
     # route table association public subnet
     resource "aws_route_table_association" "PublicRTAssociation" {
       subnet_id      = aws_subnet.PublicSubnet.id
       route_table_id = aws_route_table.PublicRouteTable.id
     }
    
  2. Run terraform apply again and wait patiently for Terraform to create these resources for you.

This is how you can use Terraform to automate your infrastructure and provision AWS services. Additionally, to prevent large bills, remember to execute terraform destroy at the end to remove all the resources you've created.

ย