TerraWeek day 3:

Passionate AWS Developer | DevOps Engineer with a strong background in cloud architecture and solutions engineering. Leveraging the power of Amazon Web Services (AWS), knowledge of the AWS global infrastructure, design and implement robust cloud-based solutions that align with clients' specific needs.
Task 01:
Create a Terraform configuration file to define a resource of AWS EC2 instance, Azure storage account, Google Compute Engine, etc. (anyone).
First of all, Make sure you have the AWS Command Line Interface (CLI) and Terraform installed on your local machine.
Ensure that you have configured your AWS credentials on your machine.
To generate the key- Navigate to the .ssh directory and run the command ssh-keygen, give the key a name.

Create a Terraform Configuration File (main.tf)
In your project directory, create a file named main.tf with the following content:
# Define the AWS provider and specify the region
provider "aws" {
region = "us-east-1"
}
# Create an AWS key pair
resource "aws_key_pair" "my_key" {
key_name = "terra-key"
public_key = file("/home/ubuntu/.ssh/terra-key.pub")
}
# Using default AWS Virtual Private Cloud (VPC)
resource "aws_default_vpc" "default" {
tags = {
Name = "default"
}
}
# Create an AWS security group to allow SSH traffic
resource "aws_security_group" "allow_ssh" {
name = "allow_ssh_traffic"
description = "Allow SSH inbound traffic"
description = "Allow SSH inbound traffic"
vpc_id = aws_default_vpc.default.id
ingress {
description = "SSH from VPC"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "allow-ssh"
}
}
# Create an AWS EC2 instance
resource "aws_instance" "demo-instance" {
key_name = aws_key_pair.my_key.key_name
ami = var.ami_id
instance_type = var.instance_type
security_groups = [aws_security_group.allow_ssh.name]
tags = {
Name = "Secured Instance"
}
}
Since we've used variables in our Terraform script, let's create a variables.tf file to declare them:
variable "ami_id" {
default = "ami-053b0d53c279acc90"
}
variable "instance_type" {
instance_type = "t2.micro"
}
Create terraform.tf for Provider Initialization
As we are using the AWS provider, we need to create a terraform.tf file to specify provider details:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.16.1"
}
}
}
Using these configurations in place, we can now use Terraform to provision an AWS EC2 instance. Simply run terraform init, terraform plan, and terraform apply to create and manage your infrastructure.
Task 02:
Check state files before running plan and apply commands & Use validate command to validate your tf file for errors and provide the Output generated by each commands.
Check State Files: To check the state files, you can use the terraform state list command. Run the following command in your terminal:
terraform state list

This command will list all the resources currently being managed by Terraform. Since you haven't applied anything yet, it should return an empty list.
Initialize terraform repository: To initialize a working directory containing Terraform configuration files, run the following command:
terraform init

Validate Terraform Configuration: To validate your Terraform configuration for errors, you can use the terraform validate command. Run the following command:
terraform validate

This shows that the terraform files is validated and is error-free.
Output: The output of the above commands will be as follows:
terraform state list(Before applying): Empty list, indicating no resources are being managed.terraform validate: No output if the configuration is valid. Otherwise, it will display an error message if there are any issues with your configuration.
Plan and Apply: Run terraform plan to get an overview of what resource will be created with what arguments and attributes.


Here, the plan is to add 4 resources - ec2 instance, security group, vpc, key pair.
Now run terraform apply command to create all the plan.


State list: State list after terraform applied.

Task 03:
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.
resource "aws_instance" "demo-instance" {
key_name = aws_key_pair.my_key.key_name
ami = var.ami_id
instance_type = var.instance_type
security_groups = [aws_security_group.allow_ssh.name]
tags = {
Name = "Secured Instance"
}
provisioner "remote-exec" {
inline = [
"echo 'provisioner example' > /home/ubuntu/test.txt",
]
connection {
type="ssh"
user="ec2-user"
private_key = file("/home/ubuntu/.ssh/terra-key3")
host=self.public_ip
}
}

Task 04:
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.
To control the creation, modification, and deletion of an AWS EC2 instance using lifecycle management configurations in Terraform, you can use the lifecycle block. Here's an example of how to do this:
resource "aws_instance" "demo-instance" {
key_name = aws_key_pair.my_key.key_name
ami = var.ami_id
instance_type = var.instance_type
security_groups = [aws_security_group.allow_ssh.name]
tags = {
Name = "Secured Instance"
}
provisioner "remote-exec" {
inline = [
"echo 'provisioner example' > /home/ubuntu/test.txt",
]
connection {
type="ssh"
user="ec2-user"
private_key = file("/home/ubuntu/.ssh/terra-key3")
host=self.public_ip
}
}
lifecycle {
prevent_destroy = false # Doesn't Allow destroying the instance
}
}
In this example:
prevent_destroy = truedoesn't allow you to destroy the instance when runningterraform destroy. If you set this tofalse, Terraform will not prevent accidental destruction of the instance.
Once you've added these lifecycle configurations to your Terraform configuration file, you can use the following Terraform commands to apply the changes:
terraform apply
Terraform will create the EC2 instance with the specified lifecycle configurations.
Destroy the Resources:
If you want to destroy the resources (including the EC2 instance), you can use the following command:
terraform destroy

Terraform will prevent the deletion. To delete the instance, set value of prevent_destroy to false and do terraform apply
After editing the main.tf and terraform apply


These commands will help you control the creation, modification, and deletion of the AWS EC2 instance using Terraform's lifecycle management configurations.




