Day 64 & 65: Terraform with AWS

Day 64 & 65: Terraform with AWS

Introduction

In the previous tutorial, we learned how to provision an EC2 instance using Terraform. In this guide, we'll enhance our infrastructure by adding a security group to control incoming traffic to our EC2 instance. Additionally, we'll modify our Terraform configuration to include the creation of an EC2 instance with a simple website hosted on it.

Task 1: Create a Security Group

Open your main.tf file and add the following code to create a security group:

# main.tf

provider "aws" {
  region = "us-east-1"  # Set your desired AWS region
}

resource "aws_security_group" "web_server" {
  name_prefix = "web-server-sg"

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

Now, initialize and apply the Terraform configuration:

terraform init
terraform apply

Confirm the action by typing yes and pressing Enter.

Task 2: Create an EC2 Instance

Update your main.tf file with the following code to create an EC2 instance:

# main.tf

resource "aws_instance" "web_server" {
  ami           = "ami-0557a15b87f6559cf"  # Replace with your desired AMI
  instance_type = "t2.micro"
  key_name      = "my-key-pair"             # Replace with your key pair name
  security_groups = [
    aws_security_group.web_server.name
  ]

  user_data = <<-EOF
                #!/bin/bash
                sudo apt-get update -y
                sudo apt-get install -y apache2
                sudo systemctl start apache2
                sudo systemctl enable apache2
                echo "<h1>Welcome to my website!</h1>" | sudo tee /var/www/html/index.html > /dev/null
              EOF
}

Make sure to replace the ami and key_name values with your own. You can find a list of available AMIs in the AWS documentation.

Now, apply the changes:

terraform apply

Task 3: Access Your Website

Now that your EC2 instance is up and running, you can access the website hosted on it. Follow these steps:

  1. Log in to the AWS Management Console.

  2. Navigate to the EC2 instances section.

  3. Find the public IP address or public DNS of the newly created EC2 instance.

  4. Open a web browser and enter the public IP address or DNS in the address bar.

You should see the welcome message on the website hosted on your EC2 instance.

Conclusion

Congratulations! You've successfully created a security group to control incoming traffic and provisioned an EC2 instance with a simple website using Terraform. This example demonstrates how Terraform can be used to manage and automate the deployment of AWS resources. Explore further to customize and expand your infrastructure based on your specific requirements.

Follow me on LinkedIn.

Checkout my GitHub profile.