Task 1:
Create a Terraform configuration file to define a resource of AWS EC2 instance, Azure storage account, Google Compute Engine, etc. (any one)
Here's a basic Terraform configuration file (aws.tf) that defines an AWS EC2 instance resource:
# aws.tf
# Configure the AWS provider
provider "aws" {
region = "us-east-1" # Desired AWS region
}
# Define an AWS EC2 instance
resource "aws_instance" "example" {
ami = "ami-053b0d53c279acc90"
instance_type = "t2.micro"
key_name = "KeyToEc2"
}
You can customize this configuration further by adding additional attributes and resources as required for your specific use case.
As we will be planning to launch an EC2 instance in the next task, make sure to save .pem key file in the same folder, aws.tf was created.
Task 2:
Check state files before running the plan and apply commands & Use the validate command to validate your .tf file for errors.
Check State: Before you run any Terraform commands, you can check the current state of your Terraform environment using the terraform show command. Since you haven't run any commands yet, it should indicate that there are no resources currently in your state.
Validate: You can also use the terraform validate command to check your Terraform configuration for syntax errors.
terraform show terraform validate
Plan Execution: To preview the changes that Terraform will make, you can run the below command:
terraform plan
The plan will list the changes, and you can review them to ensure they align with your expectations.
- Apply Execution: Once you're satisfied with the plan, you can apply the configuration to create the AWS EC2 instance:
terraform apply
After applying, Terraform will provide a summary of the changes it made, including the number of resources added, changed, or destroyed.
You have successfully created an EC2 instance using Terraform.
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.
- Adding Provisioner: To add a provisioner that configures the AWS EC2 instance after it is created, we will add a block- provisioner within aws_instance resource definition in the aws.tf file. In this example, we'll add a simple shell script provisioner that runs a command on the instance after creation.
Here's the updated terraform file:
# aws.tf
# Configure the AWS provider
provider "aws" {
region = "us-east-1" # Desired AWS region
}
# Define an AWS EC2 instance with a provisioner
resource "aws_instance" "example" {
ami = "ami-053b0d53c279acc90"
instance_type = "t2.micro"
key_name = "KeyToEc2"
# Provisioner block for running commands on the instance after creation
provisioner "local-exec" {
command = "echo 'Hello, Terraform Provisioner!' > /tmp/provisioner_output.txt"
}
}
In this updated configuration within the provisioner block, we specify a simple shell command using the command attribute. It writes a message to a file called "provisioner_output.txt" in the /tmp directory on the EC2 instance.
- Apply Changes: To create the AWS EC2 instance and execute the provisioner, run the following command:
terraform apply
This command will create the EC2 instance and execute the provisioner command. You should see output indicating that the EC2 instance was created and that the provisioner executed successfully.
Destroy Resources: To remove the AWS EC2 instance and associated resources, you can use the following command:
terraform destroy
Terraform will prompt you to confirm the destruction of resources. Confirm and proceed, and Terraform will destroy the EC2 instance and any associated resources created by your configuration.
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.
You can use the lifecycle block within aws_instance resource definition in the aws.tf file.
In this example, we'll configure the lifecycle block to control when the EC2 instance is created and when it can be replaced or destroyed.
Here's the updated aws.tf file with lifecycle management configurations:
# aws.tf
# Configure the AWS provider
provider "aws" {
region = "us-east-1" # Desired AWS region
}
# Define an AWS EC2 instance with a provisioner
resource "aws_instance" "example" {
ami = "ami-053b0d53c279acc90"
instance_type = "t2.micro"
key_name = "KeyToEc2"
# Provisioner block for running commands on the instance after creation
provisioner "local-exec" {
command = "echo 'Hello, Terraform Provisioner!' > /tmp/provisioner_output.txt"
}
}
# Lifecycle block for controlling resource behavior
lifecycle {
create_before_destroy = true # Create new resource before destroying the old one
prevent_destroy = false # Allow the resource to be destroyed
}
}
In this updated configuration we've added a lifecycle block within the aws_instance resource. This block includes two settings:
create_before_destroy: When set to true, it tells Terraform to create the new resource before destroying the old one during updates or replacements. This can help prevent resource downtime during updates.
prevent_destroy: When set to false, it allows the resource to be destroyed using terraform destroy. By default, this setting is true, which prevents accidental resource deletion. Setting it to false enables you to destroy the resource using Terraform.
To apply and destroy you can use the same Terraform commands used previously.
These lifecycle configurations provide you with more control and flexibility when managing your resources with Terraform.
Hope you like my post. Don't forget to like, comment, and share.