Working with Terraform Resources

Working with Terraform Resources

·

3 min read

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.

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

Note: Where to find the references for these codes? The answer is in the Terraform registry.

Task 2: Create an EC2 instance

Now you can create an EC2 instance with Terraform. Follow these steps:

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 python3 -m http.server80 &
  EOF
}

///if you use python2 version then use-

   nohup python -m SimpleHTTPServer 80 &

Note: Replace the ami and key_name values with your own.

We use the user_data parameter to execute a Bash script that sets up a basic web server using Python's SimpleHTTPServer.

  • The <<-EOF and EOF syntax is a way to create a multi-line string in Terraform. It's a heredoc syntax, allowing you to include multiline strings without the need for excessive escaping.

  • #!/bin/bash: This is known as a shebang line, specifying that the script should be executed using the Bash shell.

  • echo "<html><body><h1>Welcome to my website!</h1></body></html>" > index.html: This line creates an HTML file named index.html with a simple "Welcome to my website!" message. The echo command is used to write the HTML content into the file.

  • nohup python3 -m http.server 80 &: This line starts a simple HTTP server using Python. It uses nohup to run the server in the background and allows the script to continue executing. The server listens on port 80, serving the contents of the current directory.

    Upon launching an EC2 instance using Terraform and incorporating this user_data script, a web server will be configured to serve the content of the index.html file on port 80. To access the website, simply navigate to the public IP address of your instance using a web browser.

Execute terraform apply to generate the EC2 instance.

The EC2 instance has been successfully generated.

Retrieve the public IP address of the instance created through Terraform.

Navigate to the public IP address of your instance in a web browser to view the webpage.


Thankyou for reading until here. See you in the next one.