TerraWeek Day 3

TerraWeek Day 3

Create a Terraform configuration file to define a resource of AWS EC2 instance

To create a Terraform configuration file for an AWS EC2 instance, you'll need to follow these steps. First, make sure you have Terraform installed on your machine. You'll also need AWS credentials configured.

If Terraform not installed on your machine check this link for terraform installation :https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli

  1. Create a directory for your Terraform project and navigate into it:

     mkdir terraform-ec2-example
     cd terraform-ec2-example
    
  2. Create a Terraform configuration file, typically named main.tf, and open it in a text editor:

     vim main.tf
    

    Add the following content to main.tf to define an AWS EC2 instance:

     # Configure the AWS provider with your credentials
     provider "aws" {
       region = "us-east-1" # Replace with your desired AWS region
     }
    
     # Define an AWS EC2 instance
     resource "aws_instance" "example" {
       ami           = "ami-0c55b159cbfafe1f0"  # Replace with your desired AMI ID
       instance_type = "t2.micro"              # Replace with your desired instance type
     }
    

    Note: Make sure to replace the ami with the desired Amazon Machine Image (AMI) ID and the instance_type with the desired instance type.

  3. Initialize the Terraform project in your directory:

     terraform init
    
  4. Preview the changes that Terraform will make:

     terraform plan
    

    This command will show you what resources Terraform will create, modify, or delete.

  5. Apply the Terraform configuration to create the EC2 instance:

     terraform apply
    

    Terraform will prompt you to confirm the changes. Type "yes" to proceed.

  6. Terraform will provision the EC2 instance. Once it's done, you will see the output, including the public IP address and other information about the instance.

  7. To destroy the EC2 instance when you no longer need it, use the following command:

     terraform destroy
    

    Terraform will ask for confirmation before destroying any resources.

Make sure to adapt the configuration to your specific needs, such as choosing the appropriate AWS region, AMI, instance type, and other settings. This is a basic example, and you can extend it with additional resources and configurations as your project requires.

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 command.

  1. Check State Files:

Before running any Terraform commands, it's a good practice to check the state of your existing infrastructure. This ensures that Terraform has an accurate understanding of the current state. To check state files, you can use the terraform state command.

terraform state list

This command will list all the resources tracked in your state file. It's a good initial step to verify that Terraform is aware of the resources you intend to manage.

  1. Validate Terraform Files:

It's crucial to validate your Terraform configuration files for syntax errors and other issues before proceeding further. Use the validate command for this purpose:

terraform validate

This command will check your Terraform files (.tf) for syntax errors and other problems. If there are any issues, Terraform will report them, allowing you to fix them before proceeding.

  1. Run Plan Command:

After validating your Terraform configuration, you can create an execution plan to see what changes Terraform will make to your infrastructure without actually applying them. Use the plan command for this:

terraform plan

This command will generate a plan and save it to a file named tfplan. It will display the changes that Terraform intends to apply, including resource creation, modification, or destruction.

  1. Apply the Changes:

Once you're satisfied with the plan and have reviewed the proposed changes, you can apply them using the apply command:

terraform apply

This command will apply the changes outlined in the plan file, and it will prompt you to confirm the changes before proceeding. Be cautious when applying changes, especially in production environments.

By following these steps, you can maintain a controlled and validated Terraform workflow to manage your infrastructure effectively as a DevOps learner.

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.

I'll walk you through adding a provisioner to a Terraform configuration file, applying changes, and destroying resources. Let's assume you want to create an AWS EC2 instance and run a simple shell command on it once it's created.

  1. Install Terraform: https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli

  2. Create a Terraform Configuration File: Create a new directory for your Terraform project and create a file named main.tf within it. This is where you define your infrastructure resources.

  3. Write Terraform Configuration: Open main.tf and add the following code to create an AWS EC2 instance with a provisioner that runs a shell command after creation.

     provider "aws" {
       region = "us-east-1"  # Replace with your desired AWS region
     }
    
     resource "aws_instance" "example" {
       ami           = "ami-0c55b159cbfafe1f0"  # Replace with your desired AMI ID
       instance_type = "t2.micro"
     }
     # Provisioner block
     provisioner "remote-exec" {
       inline = [
         "sudo apt-get update",
         "sudo apt-get install -y nginx",
         "sudo service nginx start"
       ]
    
       connection {
         type        = "ssh"
         user        = "ubuntu"  # For AWS Ubuntu-based AMIs
         private_key = file("~/.ssh/your-private-key.pem")  # Replace with your private key path
         host        = aws_instance.example.public_ip
       }
     }
    

    Make sure to replace the AMI ID, instance type, private key path, and any other settings according to your requirements.

  4. Initialize the Terraform Project: Open a terminal in your project directory and run:

     terraform init
    
  5. Apply the Terraform Configuration: Run the following command to create the AWS EC2 instance and execute the provisioner script:

     terraform apply
    

    Terraform will display a summary of the changes it's about to make. Type "yes" to confirm and proceed with the creation.

  6. Destroy Resources: To remove the created resources (in this case, the EC2 instance), run:

     terraform destroy
    

    Again, Terraform will confirm the destruction, so type "yes" to proceed.