TerraWeek Day 3

Β·

7 min read

Task 🎈

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

AWS EC2 instance definition in a Terraform configuration file. Don't forget that you'll need to install Terraform on your computer and set up your AWS credentials.

# Define the provider (AWS)
provider "aws" {
  region = "us-east-1" # Change this to your desired AWS region
}

# Define an AWS EC2 instance
resource "aws_instance" "Master" {
  ami           = "ami-0c94855ba95c71c99" # Specify the AMI ID for your desired OS and region
  instance_type = "t2.micro"             # Specify the instance type

  tags = {
    Name = "Master"
  }
}

# Output the public IP of the instance
output "public_ip" {
  value = aws_instance.Master.public_ip
}

This configuration file is written in the Terraform language known as HCL (HashiCorp Configuration Language). As well as an EC2 instance resource, it defines an AWS provider. Replace the ami and instance_type with values suitable for your use case.

To apply this configuration, save it with a .tf extension (e.g., Master.tf) and run the following commands:

terraform init
terraform apply

The resources you've chosen will be created in your AWS account and Terraform will be started. To prevent paying extra fees, remember to use terraform destroy to destroy the resources once you are finished with them.

Task 🎈🎈

  • Check state files before running the plan and apply commands & Use the validate command to validate your tf file for errors and provide the Output generated by each command.

Terraform provides several commands for managing your infrastructure. In this situation, we will use terraform validate, terraform plan, and terraform apply to validate, plan, and apply your configuration. Additionally, I'll show you how to inspect the state files.

  • Validate the Terraform Configuration:

The terraform validate command checks the configuration files for syntax errors and other basic issues. Run it as follows:

terraform validate

Result:

Success! The configuration is valid.

This Result means that your Terraform configuration is free of syntax errors.

  • Check State Files:

The file before running the terraform plan and terraform apply commands, you can check the state of your infrastructure with the terraform state list command. However, since we haven't applied any changes yet, there won't be any resources in the state.

terraform state list

Result:

# No resources in the Terraform state
  • Plan Your Infrastructure:

The terraform plan command allows for applying changes to your infrastructure while still displaying what changes will be made. It's a useful technique to review what Terraform is trying to do.

terraform plan

Result:

Refreshing state...

------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_instance.example_instance will be created
  + resource "aws_instance" "Master" {
      + ami                          = "ami-0c94855ba95c71c99"
      + arn                          = (known after apply)
      + associate_public_ip_address  = (known after apply)
      + availability_zone            = (known after apply)
      + cpu_core_count               = (known after apply)
      + cpu_threads_per_core         = (known after apply)
      + get_password_data            = false
      + host_id                      = (known after apply)
      + id                           = (known after apply)
      + instance_state               = (known after apply)
      + instance_type                = "t2.micro"
      + ipv6_address_count           = (known after apply)
      + ipv6_addresses               = (known after apply)
      + key_name                     = (known after apply)
      + network_interface_ids        = (known after apply)
      + outpost_arn                  = (known after apply)
      + password_data                = (known after apply)
      + placement_group              = (known after apply)
      + primary_network_interface_id = (known after apply)
      + private_dns                  = (known after apply)
      + private_ip                   = (known after apply)
      + public_dns                   = (known after apply)
      + public_ip                    = (known after apply)
      + secondary_private_ips        = (known after apply)
      + security_groups              = (known after apply)
      + subnet_id                    = (known after apply)
      + tags                         = {
          + "Name" = "Master"
        }
      + tenancy                      = (known after apply)
      + vpc_security_group_ids       = (known after apply)
    }

Plan: 1 to add, 0 to change, 0 to destroy.

This output provides a summary of the planned changes. In this case, Terraform will create an AWS EC2 instance.

  • Apply the Changes:

After seeing the plan and ensuring it's correct, you can apply the changes using the terraform apply command:

terraform apply

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

Result:

aws_instance.Master: Creating...
aws_instance.Master: Still creating... [10s elapsed]
aws_instance.Master: Still creating... [20s elapsed]
aws_instance.Master: Creation complete after 26s [id=i-0123456789abcdef0]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Outputs:

public_ip = "172.93.26"

This output confirms that the EC2 instance has been created successfully and provides the public IP address as an output.

Now, you can use terraform state list again to see the resources in the Terraform state:

terraform state list

Result:

aws_instance.Master

You can also use terraform show to display detailed information about the resources in the state.

Remember to use terraform destroy when you're done with your infrastructure to clean up the resources and avoid unnecessary charges.

Task 🎈🎈🎈

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

After a resource has been established, it can be configured using a provisioner. In this example, when the AWS EC2 instance resource is established, a provisioner will be added to allow the installation of a basic web server (NGINX). Additionally, we'll utilize Terraform commands to implement the modifications and remove the resources once you're done.

Here is the updated Terraform configuration file that includes the provisioner:

# Define the provider (AWS)
provider "aws" {
  region = "us-east-1" # Change this to your desired AWS region
}

# Define an AWS EC2 instance
resource "aws_instance" "Master" {
  ami           = "ami-0c94855ba95c71c99" # Specify the AMI ID for your desired OS and region
  instance_type = "t2.micro"             # Specify the instance type

  tags = {
    Name = "Master"
  }

  # Provisioner to install NGINX after the instance is created
  provisioner "remote-exec" {
    inline = [
      "sudo yum update -y",
      "sudo yum install -y nginx",
      "sudo systemctl start nginx",
      "sudo systemctl enable nginx"
    ]

    connection {
      type        = "ssh"
      user        = "ec2-user"
      private_key = file("~/.ssh/id_rsa") # Specify the path to your SSH private key
    }
  }
}

# Output the public IP of the instance
output "public_ip" {
  value = aws_instance.Master.public_ip
}

Here is how to apply the changes and destroy the resources using Terraform:

Save the modified configuration file and run the following commands:

terraform init
terraform apply

The EC2 instance will be created by Terraform, and it will execute the supplied provisioner commands to install NGINX.

  • Destroy the Resources:

When you're completed with the resources, you can destroy them to avoid incurring charges:

terraform destroy

Terraform will ask you to confirm the destruction of the resources. Type "yes" to proceed.

The resources will be created using these commands, which will also be used to destroy them once you're done. The EC2 instance will be set up by the provisioner as instructed.

Task 🎈🎈🎈🎈

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

The creation, modification, and destruction of resources can be managed by Terraform configuration using the lifecycle block. Using the following example, you can set up lifecycle management for your AWS EC2 instance resource:

# Define the provider (AWS)
provider "aws" {
  region = "us-east-1" # Change this to your desired AWS region
}

# Define an AWS EC2 instance
resource "aws_instance" "Master" {
  ami           = "ami-0c94855ba95c71c99" # Specify the AMI ID for your desired OS and region
  instance_type = "t2.micro"             # Specify the instance type

  tags = {
    Name = "Master"
  }

  # Lifecycle configuration for the resource
  lifecycle {
    create_before_destroy = true

    prevent_destroy = false
  }

  # Provisioner to install NGINX after the instance is created
  provisioner "remote-exec" {
    inline = [
      "sudo yum update -y",
      "sudo yum install -y nginx",
      "sudo systemctl start nginx",
      "sudo systemctl enable nginx"
    ]

    connection {
      type        = "ssh"
      user        = "ec2-user"
      private_key = file("~/.ssh/id_rsa") # Specify the path to your SSH private key
    }
  }
}

# Output the public IP of the instance
output "public_ip" {
  value = aws_instance.Master.public_ip
}

In this example, we've added a lifecycle block to the aws_instance resource:

  • create_before_destroy is set to true, which means Terraform will create a new instance before destroying the existing one when changes are applied. This can help with zero-downtime updates.

  • prevent_destroy is set to false, which allows Terraform to destroy the resource when you run terraform destroy. You can set this to true if you want to prevent accidental deletions.

To apply these changes, follow these steps:

  1. Save the modified configuration file.

  2. Run the following commands:

terraform init
terraform apply
  1. Terraform will create the EC2 instance with the specified lifecycle management settings.

When you're done and want to destroy the resources, run:

terraform destroy

Terraform will prompt you to confirm the destruction of the resources based on the prevent_destroy setting you configured.


Happy Learning

Thanks For Reading! :)

-SriparthuπŸ’

Β