# Day 65 - Working with Terraform Resources 🚀  (Jan 30, 2024)

## **🙏 Introduction:**

In this blog, we will do hands-on practice on Terraform Resources.

## **🔶What is 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.
    

# **🎯Task: 1**

### **Create a security group**

* In your [**main.tf**](https://sutish.hashnode.dev/day-65-working-with-terraform-resources#heading-what-is-terraform-resources) file, add the following code to create a security group
    

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

* In this configuration, we first configure the AWS provider with the desired region.
    
* Configure a security group using the aws\_security\_group resource block that allows incoming traffic on port 80 from any IP address. This ensures that we can access the website from the public internet.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704813605158/ce9b9b93-1702-43d9-b75d-e3e835c190a0.png?auto=compress,format&format=webp align="left")

`terraform init`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704814048968/c65ef14b-fcce-4510-af99-51f48745f272.png?auto=compress,format&format=webp align="left")

`terraform plan`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704814134705/fbd4456e-a2bb-4885-aaea-76a9d84a4ccb.png?auto=compress,format&format=webp align="left")

`terraform apply`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704814413153/ca0d9a3c-73b5-46cc-8fb8-f5f4bd6b2f68.png?auto=compress,format&format=webp align="left")

* Check Security group
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704814543076/7b54a4ae-2ad2-4f2d-9d1b-0aa34da46063.png?auto=compress,format&format=webp align="left")

# **🎯Task: 2**

### Create an EC2 instance

* In your [**main.tf**](https://sutish.hashnode.dev/day-65-working-with-terraform-resources#heading-what-is-terraform-resources) file, add the following code to create an EC2 instance
    

```plaintext
resource "aws_instance" "web_server" {
  ami           = "ami-008fe2fc65df48dac"
  instance_type = "t2.micro"
  key_name      = "terra_key"
  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
}
```

* When we launch an EC2 instance using Terraform with this **user\_data** script, it will set up a web server serving the content of index.html on port 80. You can then access the website by navigating to the public IP address of your instance in a web browser
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704814774297/545144de-35e2-4f40-94b9-7ea5c0c5156d.png?auto=compress,format&format=webp align="left")

`terraform init`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704814826248/f5ad80fe-fa8a-4fe5-a5b6-2694ed694afd.png?auto=compress,format&format=webp align="left")

`terraform plan`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704814869146/b18d3c89-3ef2-4d89-9c47-48497ee049cf.png?auto=compress,format&format=webp align="left")

`terraform apply`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704815095565/3828ee6f-b6f8-474b-949d-d1e1502a70f7.png?auto=compress,format&format=webp align="left")

* EC2 instance is created
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704815167424/14c1b4b6-1535-462f-8baf-5f5bfec5e510.png?auto=compress,format&format=webp align="left")
