Terraform terraweek day 3
SUMIT Dahiya
"DevOps Enthusiast | Eager Learner | Passionate about Revolutionizing IT through Automation & Continuous Improvement | Skilled in AWS and Azure Cloud Technologies"
Task -1
Create a Terraform configuration file to define a resource of AWS EC2 instance.
# Specify the provider (AWS in this case)
provider "aws" {
?access_key = "<YOUR_AWS_ACCESS_KEY>"
?secret_access_key = "<YOUR_AWS_SECRET_ACCESS_KEY>"
?region = "us-west-1"??
}
# Define the EC2 instance resource
resource "aws_instance" "sumit_dahiya" {
?ami??????= "ami-014d05e6b24240371"??
?instance_type = "t2.micro"??
?tags = {
??Name = "sumitawsinstance"
}
?}
The?provider?block defines the AWS provider that Terraform will use. The?access_key?and?secret_access_key?attributes specify your AWS access key ID and secret access key, respectively. The?region?attribute specifies the AWS region where the EC2 instance will be create
The?resource?block defines the EC2 instance resource. The?ami?attribute specifies the AMI ID that the EC2 instance will be created from. The?instance_type?attribute specifies the instance type of the EC2 instance. The?tags?attribute specifies the tags that will be applied to the EC2 instance.
Task -2
Check state files before running terraform plan and apply commands and Use validate command to validate your tf file for errors and provide the Output generated by each command:
1- Checking state files before running the terraform plan and apply commands:
Before running the?plan?or?apply?commands, it is important to check the state files to make sure that they are up-to-date. The state files contain information about the current state of your infrastructure, and Terraform uses this information to determine what changes need to be made. If the state files are not up-to-date, Terraform may make incorrect changes to your infrastructure.
$ terraform state list
aws_instance.example_instance
$ terraform state show aws_instance.example_instance
# Output will display the detailed information of the resource
2- Using the?validate?command to validate your tf file for errors.
The?terraform validate?command can be used to validate your Terraform configuration file for errors.
To use the?validate?command, simply run the following command:
$ terraform validate
Success! The configuration is valid.
If there are any errors in your configuration file, Terraform will print out a list of the errors. You can then fix the errors in your configuration file and try running the?validate?command again
3-?Output generated by each command:
The output generated by each command will vary depending on the specific state of your infrastructure and your Terraform configuration file. However, here is a general overview of the output that you can expect to see.
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:
# Specify the provider (AWS in this case)
provider "aws" {
?access_key = "<YOUR_AWS_ACCESS_KEY>"
?secret_access_key = "<YOUR_AWS_SECRET_ACCESS_KEY>"
?region = "us-west-1"??
}
# Define the EC2 instance resource
resource "aws_instance" "example_instance" {
?ami??????= "ami-014d05e6b24240371"??
?instance_type = "t2.micro"??
?tags = {
??Name = "ExampleInstance"
?}
}
# Add a provisioner
provisioner "remote-exec" {
??inline = ["echo 'Hello, world!' > /home/ubuntu/hello.txt"]
?}
In this example, the?aws_instance?resource is configured to use the remote-exec provisioner. The?inline?argument to the?remote-exec?provisioner specifies the command that will be run on the remote resource. In this case, the command is?echo 'Hello, world!' > /home/ubuntu/hello.txt.
To apply the changes and create the EC2 instance, you can run the following Terraform commands:
To destroy and remove the resources created by Terraform, you can use the following command:
terraform destroy: Destroys all the resources managed by Terraform.
What is a Provisioner block:
In Terraform, the?provisioner?block is used to define actions or scripts that should be executed on a resource after it is created or destroyed. It allows you to configure and customize the resource as needed, such as installing software, running commands, or executing configuration scripts
The?provisioner?block has two main configurations:?create?and?destroy
Provisioner are powerful tools in Terraform that allow you to automate additional setup or configuration tasks for your resources. They provide flexibility and extensibility in managing your infrastructure, ensuring that your resources are properly configured and customized according to your requirements.
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:
# Specify the provider (AWS in this case)
provider "aws" {
?access_key = "<YOUR_AWS_ACCESS_KEY>"
?secret_access_key = "<YOUR_AWS_SECRET_ACCESS_KEY>"
?region = "us-west-1"??
}
# Define the EC2 instance resource
resource "aws_instance" "example_instance" {
?ami?= "ami-014d05e6b24240371"??
?instance_type = "t2.micro"??
?tags = {
??Name = "ExampleInstance"
?}
}
# Add a provisioner
provisioner "remote-exec" {
??inline = ["echo 'Hello, world!' > /home/ubuntu/hello.txt"]
?}
# Add lifecycle management configurations
lifecycle {
??create_before_destroy = true
??ignore_changes????= [tags]
?}
In this example, we've added a?lifecycle?block within the?aws_instance?resource block. The?lifecycle?block allows you to configure the behavior of Terraform during resource creation, modification, and deletion.
The available configurations are:
To apply the changes and create/update the EC2 instance based on the new lifecycle configurations, you can run the following Terraform commands:
What is a lifecycle block:
In Terraform, the?lifecycle?block is used to define lifecycle management configurations for resources. It allows you to control the behavior of Terraform during resource creation, modification, and deletion. The?lifecycle?block provides options for managing resource replacement, preventing certain changes from triggering resource recreation, and specifying other resource-specific behaviors.
The?lifecycle?block supports the following configurations:
resource "aws_instance" "example_instance" {
?# Resource attributes...
?lifecycle {
??create_before_destroy = true
??prevent_destroy????= true
??ignore_changes????= [tags]
?}
}
In this example, the?lifecycle?block is added to an AWS EC2 instance resource. It specifies that the new resource should be created before destroying the old one (create_before_destroy = true). It also prevents the resource from being destroyed (prevent_destroy = true) and ignores changes made to the?tags?attribute (ignore_changes = [tags]).
When using the?ignore_changes?configuration in the lifecycle block to ignore changes for a specific attribute, such as an EC2 instance name tag, you need to specify the fully qualified attribute name within the resource.
In the case of an EC2 instance name tag, the attribute name would be?aws_instance.example_instance.tags["Name"]. Here's an example of how you can use it:
resource "aws_instance" "example_instance" {
?# Resource attributes...
?lifecycle {
??ignore_changes = [aws_instance.example_instance.tags["Name"]]
?}
}
Note that the attribute name within the ignore_changes configuration should match the exact attribute reference used in the resource block. If the EC2 instance name tag has a different name or is defined using a variable, you would need to adjust the attribute name accordingly.
The?lifecycle?block allows you to fine-tune the behavior of Terraform for specific resources, ensuring controlled updates, preventing accidental destruction, and ignoring certain changes when managing your infrastructure.
"DevOps Enthusiast | Eager Learner | Passionate about Revolutionizing IT through Automation & Continuous Improvement | Skilled in AWS and Azure Cloud Technologies"
1 年Thanks dosto... But what can i do.... All meterial are same... But may be i post a unique project related to it........ That stand me in a unique way.... I will try my best first of all thanks #tws for teaching us in a unique way.... This type of techear... Or guide u can not buy in a rupees... This type of teacher u can only find... When u take a candle in a hand and find.... Some one teaching us from..heart..... Special thanks to... #Trainwithshubham