Day 65 - Working with Terraform Resources

Day 65 - 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.

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"]
  }
}        
No alt text provided for this image

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.

Run terraform init to initialize the Terraform project.

No alt text provided for this image

Run terraform apply to create the security group.

No alt text provided for this image

Check Security group is created

Go to EC2 service

Inside 'Network & Security', select 'Security Groups'

You can see web-server-sg security group is created using terraform.

No alt text provided for this image


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.

No alt text provided for this image

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.

Run terraform apply to create the EC2 instance.

No alt text provided for this image
No alt text provided for this image

EC2 instance is created.

No alt text provided for this image

Copy public-ip-address of instance that is created using terraform.

No alt text provided for this image

Browse public ip address of your instance. You can see webpage.

No alt text provided for this image

Thank you for reading!

Sunil Kumar

Senior Cloud Engineer - Nagarro | Google Cloud Certified {"CDI, ACE , PCA, PDE}|Cloud DevOps Specialist | 5x GCP , Azure Certified | Devops | Linux | Docker | Terraform | Jenkins CI/CD | Kubernetes

1 年

Well explained ?? ?? Sayali Shewale

回复
Sayali Jadhav

DevOps Engineer at BH Mobile Pte Ltd || AWS || Linux || CICD || Jenkins || Git GitHub || Docker || Kubernetes || Terraform ||

1 年

Amazing Sayali Shewale

要查看或添加评论,请登录

Sayali Shewale的更多文章

  • DevOps Project-3

    DevOps Project-3

    Project Description The project involves hosting a static website using an AWS S3 bucket. Amazon S3 is an object…

    7 条评论
  • DevOps Project-2

    DevOps Project-2

    Project Description The project is about automating the deployment process of a web application using Jenkins and its…

    2 条评论
  • Day 80: DevOps Project-1

    Day 80: DevOps Project-1

    Project Description The project aims to automate the building, testing, and deployment process of a web application…

    4 条评论
  • Day 73 - Setup Grafana on AWS EC2 Instance

    Day 73 - Setup Grafana on AWS EC2 Instance

    Task: Setup grafana in your local environment on AWS EC2. Go to the AWS console and Launch an EC2 instance Open port…

    2 条评论
  • Day 72 - Grafana

    Day 72 - Grafana

    What is Grafana? Grafana is an open-source data visualization and monitoring tool that allows you to query, visualize…

    3 条评论
  • Day 70 - Terraform Modules

    Day 70 - Terraform Modules

    Modules are containers for multiple resources that are used together. A module consists of a collection of .

    2 条评论
  • Day 69 - Meta-Arguments in Terraform

    Day 69 - Meta-Arguments in Terraform

    When you define a resource block in Terraform, by default, this specifies one resource that will be created. To manage…

    1 条评论
  • Day 68 - Scaling with Terraform

    Day 68 - Scaling with Terraform

    Understanding Scaling Scaling is the process of adding or removing resources to match the changing demands of your…

  • Day 67: AWS S3 Bucket Creation and Management using terraform

    Day 67: AWS S3 Bucket Creation and Management using terraform

    AWS S3 Bucket Amazon S3 (Simple Storage Service) is an object storage service that offers industry-leading scalability,…

    1 条评论
  • Day 66 - Terraform Hands-on Project - Build Your Own AWS Infrastructure with Ease using Infrastructure as Code (IaC)

    Day 66 - Terraform Hands-on Project - Build Your Own AWS Infrastructure with Ease using Infrastructure as Code (IaC)

    Task: 1.Create a VPC (Virtual Private Cloud) with CIDR block 10.

    3 条评论

社区洞察

其他会员也浏览了