Day65 ---> 90DaysOfDevOps Challenge @TWS

Day65 ---> 90DaysOfDevOps Challenge @TWS

Working with Terraform Resources πŸš€

Understanding Terraform Resources

A resource in Terraform represents a component of your infrastructure, such as a physical server, a virtual machine, a DNS record, or an S3 bucket. Resources have attributes that define their properties and behaviors, such as the size and location of a virtual machine or the domain name of a DNS record.

When you define a resource in Terraform, you specify the type of resource, a unique name for the resource, and the attributes that define the resource. Terraform uses the resource block to define resources in your Terraform configuration.

In this blog, we are going to host a website on an Ec2 instance using the Python HTTP server command

Task 1: Create a security group

To allow traffic to the EC2 instance, you need to create a security group. Follow these steps:

In your main.tf file, add the following code to create a security group:

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

image

Run terraform init to initialize the Terraform project.

image

Run terraform apply to create the security group.

image

image

image

Task 2: Create an EC2 instance

In your main.tf file, add the following code to create an EC2 instance:

resource "aws_instance" "web_server" {
  ami           = "ami-0557a15b87f6559cf"
  instance_type = "t2.micro"
  key_name      = "my-key-pair"
  security_groups = [
    aws_security_group.web_server.name
  ]

  user_data = <<-EOF
              #!/bin/bash
              echo "<html><body><h1>Welcome to my website!</h1></body></html>" > index.html
              nohup python -m SimpleHTTPServer 80 &
              EOF
}

Note: Replace the ami and key_name values with your own. You can find a list of available AMIs in the AWS documentation.

image

image

image

terraform {
  required_providers {
    aws = { source  = "hashicorp/aws"}
  }
}
provider "aws" {
  region = "us-east-1"
}

resource "aws_security_group" "web_server" {
  name_prefix = "web-server-sg"
  tags = { Name = "SG_Web_server" }
  ingress {
    from_port   = 8000
    to_port     = 8000
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

resource "aws_instance" "web_server" {
  ami                    = "ami-0557a15b87f6559cf"
  instance_type          = "t2.micro"
  key_name               = "ssh"
  vpc_security_group_ids = [aws_security_group.web_server.id]
  tags                   = { Name = "Python_Web_server" }

  user_data = <<-EOF
    #!/bin/bash
    echo '<!DOCTYPE html>
    <html>
    <head>
      <style>
        body {
          background-color: #87CEEB;
          color: white;
          display: flex;
          justify-content: center;
          align-items: center;
          height: 100vh;
          margin: 0;
          padding: 0;
        }

        h1, p {
          font-size: 24px;
        }
      </style>
    </head>
    <body>
      <div>
        <h1>Welcome to my website!</h1>
        <p>Hosted using Python HTTP server</p>
      </div>
    </body>
    </html>' > index.html
    nohup python -m http.server 8000 &
    EOF
}

image

Run terraform apply to create the EC2 instance.

image

image

image

image

Task 3: Access your website

Your EC2 instance is up and running, you can access the website you just hosted on it.

246646185-62a16c48-edd7-4efc-9d61-158bd15c6386

Happy Terraforming!

Day 65 task is complete!

90DaysOfDevOps TasksπŸ‘‡

github.com/Chaitannyaa/90DaysOfDevOps.git

Chaitannyaa Gaikwad | LinkedIn

Β