TerraWeek Day 3

TerraWeek Day 3

Task 1:Create a Terraform configuration file to define a resource of AWS EC2 instance, Azure storage account, Google Compute Engine, etc. (any one)

Create an EC2 instance in the AWS region “ap-south-1”.

Summary of the section:

  • Creates an EC2 instance.
  • Copy the specified AMI ID to launch the instance
  • Take a instance type is "t2.micro"
  • Define the security group.
  • Add the tag.
  • Defines ingress rules to allow inbound traffic on a port 80 from any IP
  • Defines an egress rule to allow all outbound traffic

mkdir terraweek03
cd /terraweek
vim main.tf        

main.tf

provider "aws" {
 region     = "ap-south-1"
}
resource "aws_instance" "myec2" {
  ami                    = "ami-05552d2dcf89c9b24"
  instance_type          = "t2.micro"
  vpc_security_group_ids = [aws_security_group.ownsg.id]
  key_name               = "tf-key-pair"
  tags = {
      Name = "terraform-Ashish"
  } 
}
resource "aws_security_group" "ownsg" {
  name = "own-sg"
  ingress {
    from_port   = 80
    to_port     = 80
    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"]
}
  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}
  resource "aws_key_pair" "tf-key-pair" {
    key_name   = "tf-key-pair"
  public_key = tls_private_key.rsa.public_key_openssh
  }
resource "tls_private_key" "rsa" {
    algorithm = "RSA"
  rsa_bits  = 4096
}
resource "local_file" "tf-key" {
    content  = tls_private_key.rsa.private_key_pem
  filename = "tf-key-pair"
}        

Task 2:Check state files before running the plan and apply commands & Use validate command to validate your tf file for errors and provide the Output generated by each command.

To use the above configuration, save it in a file with a .tf extension (e.g., main.tf), and then run the following Terraform commands in the same directory:

terraform init
terraform plan
terraform apply        

after terraform apply command it will create a EC2 instance based on the configuration.

Check file test:

To check the state files, you can use the terraform state list command. It lists all the resources managed by Terraform and their current state.

You can see the terraform state list command and output.

Validate Configuration File:

To validate the configuration file for errors, you can use the terraform validate command. It checks the syntax and structure of the Terraform files and reports any errors or warnings.



Task 3:Add a provisioner to the configuration file to configure the resource after it is created and use Terraform commands to apply for changes and destroy to remove resources.

Here an example of adding a provisioner to an AWS EC2 instance resource. you run scripts or execute commands on the resource during creation or destruction.




provider "aws" {
 region     = "ap-south-1"
}
resource "aws_instance" "myec2" {
  ami                    = "ami-05552d2dcf89c9b24"
  instance_type          = "t2.micro"
  vpc_security_group_ids = [aws_security_group.ownsg.id]
  key_name               = "tf-key-pair"
  tags = {
      Name = "terraform-Ashish"
  } 
}
resource "aws_security_group" "ownsg" {
  name = "own-sg"
  ingress {
    from_port   = 80
    to_port     = 80
    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"]
}
  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}
  resource "aws_key_pair" "tf-key-pair" {
    key_name   = "tf-key-pair"
  public_key = tls_private_key.rsa.public_key_openssh
  }
resource "tls_private_key" "rsa" {
    algorithm = "RSA"
  rsa_bits  = 4096
}
resource "local_file" "tf-key" {
    content  = tls_private_key.rsa.private_key_pem
  filename = "tf-key-pair"
}        
terraform init        
terraform plan        



terraform apply        

After run the command you can see the below O/P:


You can see the newly created EC2 instance.

new instance
terraform destroy        

Created instance terminated by terraform destroy command.


Task 4:Add lifecycle management configurations to the configuration file to control the creation, modification, and deletion of the resource and use Terraform commands to apply the changes.



terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "4.67.0"
    }
  }
}

provider "aws" {
  region = "ap-south-1"
}

resource "aws_instance" "terraform" {
  instance_type = "t2.micro"
  ami           = "ami-05552d2dcf89c9b24"
  
  tags = {
    Name = "Terraweek_03"
  }

  lifecycle {
    create_before_destroy = true
    prevent_destroy       = false

    ignore_changes = [
      instance_type,
      key_name,
      tags
    ]
  }
}
ubuntu@ip-172-31-34-211:~/lifecycycle$         

1.create_before_destroy = true ensures that Terraform creates a new instance before destroying the existing one when changes are made. This allows for zero-downtime deployments by minimizing the time when no instance exists.

2.prevent_destroy = false allows Terraform to destroy the instance during the terraform destroy command. By default, Terraform prevents accidental destruction of resources, but setting it to false enables the resource to be destroyed.

To apply changes and create or update the resource with the lifecycle management configurations, you can use the following command: terraform apply

After terraform apply command EC2 instance will be created.

Newly Created EC2 instance


To destroy the resources created by the Terraform configuration, you can use the following command: terraform destroy

terraform init
terraform validate
terraform plan
terraform apply
terraform destory        

Happy learning!



Akshay Gaikwad

Project Associate Engineer ???? | CDAC Bengaluru R&D ???? | ???? Government of India ???? | ???? Ministry of Electronics and Information Technology ?? |

1 年

Very useful

回复

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

Aashish R.的更多文章

  • Serverless on AWS

    Serverless on AWS

    Are you excited to know about serverless architecture!! You can build and run applications without thinking about…

  • TerraWeek Day 7 - Advanced Terraform Topics

    TerraWeek Day 7 - Advanced Terraform Topics

    Welcome to the advanced TerraWeek challenge! In this phase, we will dive into advanced topics that will enhance your…

  • TerraWeek Day 6: Terraform Providers

    TerraWeek Day 6: Terraform Providers

    Introduction In this blog, we will explore the concept of Terraform providers, compare major cloud providers such as…

    2 条评论
  • TerraWeek Day 5

    TerraWeek Day 5

    Task 1: What are modules in Terraform and why do we need modules in Terraform? You already write modules Even when you…

  • Terraweek Day 4 Knowledge about Terraform state, Local and Remote configurations

    Terraweek Day 4 Knowledge about Terraform state, Local and Remote configurations

    Task 1:The importance of Terraform state in managing infrastructure The primary purpose of Terraform state is to store…

  • HashiCorp Configuration language(HCL)

    HashiCorp Configuration language(HCL)

    #TerraWeekChallenge DAY 2 Terraform Syntax , Block parameter and arguments ,variable , data types , expressions , .tf…

  • Deployment of a Microservices Application on K8s- Do MongoDB App Deployment

    Deployment of a Microservices Application on K8s- Do MongoDB App Deployment

    Kubernetes, also known as K8s, is an open-source system for automating deployment, scaling, and management of…

  • How to Install terraform on Ubuntu server.

    How to Install terraform on Ubuntu server.

    What is Terraform? HashiCorp Terraform is an infrastructure as code tool that lets you define both cloud and on-prem…

    1 条评论
  • Deploying Reddit Copy on Kubernetes Cluster using Ingress.

    Deploying Reddit Copy on Kubernetes Cluster using Ingress.

    Prerequisites: ? EC2 (AMI- Ubuntu, Type- t2.medium) ? Docker ? Minikube ? Kubectl Follow below steps for install all…

    5 条评论

社区洞察

其他会员也浏览了