Enhancing AWS Infrastructure with Terraform: Adding Security Groups and EC2 Instances

Enhancing AWS Infrastructure with Terraform: Adding Security Groups and EC2 Instances

Day 63 & 64: Terraform with AWS

ยท

2 min read

Introduction

In the previous tutorial, we embarked on our journey with Terraform by provisioning an EC2 instance. Now, let's take our infrastructure to the next level by adding a security group to control incoming traffic and deploying a simple website on our EC2 instance. This guide will walk you through the process step-by-step, empowering you to harness the full potential of Terraform for managing AWS resources efficiently.


Task 1: Creating a Security Group

We'll begin by creating a security group to regulate incoming traffic to our EC2 instance. Open your main.tf file and add the following code:

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"]
  }
}

After adding the code, initialize and apply the Terraform configuration:

terraform init
terraform apply

Confirm the action by typing yes and pressing Enter.


Task 2: Creating an EC2 Instance with Website

Next, let's update our main.tf file to include the creation of an EC2 instance with a simple website hosted on it:

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
}

Remember to replace the ami and key_name values with your own. Then, apply the changes:

terraform apply


Task 3: Accessing Your Website

Once the EC2 instance is up and running, you can access the website hosted on it:

  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 now see the welcome message on the website hosted on your EC2 instance.


Conclusion

Congratulations on successfully enhancing your AWS infrastructure with Terraform! By adding a security group and provisioning an EC2 instance with a website, you've demonstrated the power of Terraform for managing AWS resources. Continue exploring and customizing your infrastructure to meet your specific requirements, leveraging Terraform's automation capabilities to streamline your workflow.

Happy Learning ๐Ÿš€

Follow me on LinkedIn.

ย