Day 64 & 65 Terraform with AWS

Day 64 & 65 Terraform with AWS

Day 64 & 65 of #90daysofdevops

Hey Techies! Welcome to this blog

In this blog, we are going to start with Terraform with AWS.

We are going to learn provisioning on AWS is quite easy and straightforward with Terraform.

Prerequisites

AWS CLI installed

The AWS Command Line Interface (AWS CLI) is a unified tool to manage your AWS services. With just one tool to download and configure, you can control multiple AWS services from the command line and automate them through scripts.

AWS IAM user

IAM (Identity Access Management) AWS Identity and Access Management (IAM) is a web service that helps you securely control access to AWS resources. You use IAM to control who is authenticated (signed in) and authorized (has permissions) to use resources.

Let's configure our AWS account

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 = "ap-south-1"  # Set your desired AWS region
}

resource "aws_security_group" "server" {
  name_prefix = "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:

Task 2: Create an EC2 Instance

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

# main.tf

provider "aws" {
  region = "ap-south-1"  # Set your desired AWS region
}

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

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

resource "aws_instance" "instance" {
  ami           = "ami-03f4878755434977f" # Replace with your desired AMI
  instance_type = "t2.micro"
  key_name      = "newkey" # Replace with your key pair name
  security_groups = [
    aws_security_group.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 -auto-approve

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.

Thank you so much for taking the time to read till the end! Hope you found this blog informative and helpful.

Feel free to explore more of my content, and don't hesitate to reach out if need any assistance from me or in case of you have any questions.

Happy Learning!

~kritika :)

Connect with me: LinkedIn