Day 65 - Working with Terraform Resources ๐Ÿš€

Day 65 - Working with Terraform Resources ๐Ÿš€

ยท

2 min read

Welcome back to the 64th day of our 90 days of DevOps journey! Today, we delve deeper into Terraform, a powerful tool for building, changing, and versioning infrastructure efficiently. Specifically, we'll explore Terraform resources and how to utilize them with AWS to provision infrastructure seamlessly.

What are Terraform Resources?

Terraform resources represent the infrastructure objects managed by Terraform. They could be instances, networks, databases, or any other components needed to run your application. These resources are declared in Terraform configuration files using a domain-specific language (DSL) called HCL (HashiCorp Configuration Language).

Now, let's put our knowledge into practice with AWS.

Task 1: Create a Security Group

Our first task is to create a security group in AWS to control the inbound traffic to our EC2 instance.

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

Here, we define a security group named "web_server" that allows inbound traffic on port 80 from any IP address (0.0.0.0/0).

Task 2: Create an EC2 Instance

Next, let's provision an EC2 instance using Terraform.

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
}

Here, we define an EC2 instance named "web_server" with the specified AMI, instance type, and associated security group. We also provide user data to set up a basic web server that serves a simple HTML page.

Task 3: Access Your Website

Once the EC2 instance is up and running, you can access your website by navigating to the public IP address or DNS name of the instance in a web browser.

With these tasks completed, you've successfully utilized Terraform to provision infrastructure on AWS. This demonstrates the power and simplicity of infrastructure as code (IaC) and how it can streamline your DevOps workflows.

Stay tuned for more exciting DevOps adventures tomorrow!

Happy coding! ๐Ÿš€

ย