TERRAFORM, Day - 3

TERRAFORM, Day - 3

Managing Resources

Task 1:

Create a Terraform configuration file to define a resource of AWS EC2 instance, Azure storage account, Google Compute Engine, etc. (Anyone)

Before that perform the below steps :)

step 1: Connect with your ec2 instance and visit your aws and search for IAM as shown below.

step 2: Now click on users and add users

step 3: Once you add the user by giving certain permissions, we can see our user is added. (Don't forget to download your credential file which includes the access keys)

step 4: Now create a directory and install awscli

step 5: Now type aws s3 ls If you see this message, it means awscli is installed successfully, but it doesn't have certain credentials.

step 6: Now type aws configure command and enter your access key id and secret access key

step 7: Now execute these 3 commands by providing your access key's as shown below

Step 8: Now, create a main.tf file which defines the required resources of aws_ec2 instances.


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.

step 1: Now execute terraform init command to initialize

step 2: Validate it

step 3: Check terraform plan

step 4: Now execute terraform apply command


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.

step 1: Create a provisioner file as shown below and apply the changes using terraform apply command

resource "aws_instance" "my_ec2_instance" {
  count = 2
  ami           = "ami-053b0d53c279acc90"
  instance_type = "t2.micro"
  tags = {
    Name = "TerraformBatch-Instance"
  }

  provisioner "local-exec" {
    command = "./provision.sh"
  }
}

step 2: Now use terraform destroy command to remove resources


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.

step 1: Add lifecycle management configuration as shown below.

resource "aws_instance" "my_ec2_instance" {
  lifecycle {
    create_before_destroy = true
    prevent_destroy       = false
  }
}

step 2: To apply the changes, navigate to the directory containing both the main.tf and lifecycle.tf files and run the following commands:

  1. terraform init: Initializes the Terraform configuration and downloads the necessary provider plugins.

  2. terraform plan: Creates an execution plan, showing the changes that will be made. Review the plan to ensure it aligns with your expectations.

  3. terraform apply: Applies the changes and provisions the resources based on the configuration. You will be prompted to confirm before proceeding.

These commands will apply the lifecycle management configurations to the aws_instance resource and control its creation, modification, and deletion behaviour.

Finally, our main.tf looks something like this...

Thank you so much for reading.

Follow me on LinkedIn to see interesting posts like this : )

Linkedin

Β