Hey,
My name is Sarvar, and I am working as Senior Developer at Luxoft India. With years of experience working on cutting-edge technologies, I have honed my expertise in Cloud Operations (Azure and AWS), Big Data, and DevOps. Throughout my career, I’ve worked with clients from all around the world, delivering excellent results, and going above and beyond expectations. I am passionate about learning the latest and treading technologies.
I hope you have read the first and second parts of this series. If not, click on the links below to read the first and second parts of the series. We are exploring some of the most advanced terraforming topics in this article, which will greatly benefit us in our daily lives. To assist you better comprehend the terraform, we’ll study some advanced topics while using an example. In addition, we’ll use a Terraform template to install an AWS EC2 instance. So let’s get going.
Link: Terraform Beginner to Advance: Using AWS Cloud (Part-1)
Link: Terraform Beginner to Advance: Using AWS Cloud (Part-2)
Terraform Commands:
terraform init:
Terraform init command we have seen in detail in part 2 of this series but in short this command will creates your Terraform working directory, downloads provider plugins, and configures the backend.
terraform plan:
creates an execution plan without actually making any modifications, outlining the steps Terraform will take to reach the desired infrastructure state.
terraform apply:
Apply the modifications outlined in the Terraform configuration to add, edit, or remove resources in order to bring them into the desired state. Before making modifications, a confirmation question is presented.
terraform destroy:
Destroys the resources that Terraform has been configured to manage in the infrastructure. Before destroying anything, it asks for confirmation.
terraform validate:
Verifies your Terraform configuration files’ syntax and organization, looking for mistakes and warnings.
terraform fmt:
ensures uniform code style by rewriting Terraform configuration files that conform to the accepted formatting standards.
terraform refresh:
reflects the current state of the infrastructure in the real world in the state file, allowing Terraform to follow changes more precisely.
Advance Topic of Terraform:
Here, we’re going to look at some of the more advanced topics in the field of terraforming that you ought to be aware of. I can assure you that this will help you prepare for the interview.
Terraform Variables:
Terraform variables encourage consistency, reusability, and straightforward configuration management. They make it easier to adjust and manage infrastructure configurations over time since they let you retain a single set of configuration files for many contexts and circumstances.
The variable block is used to define variables in your Terraform setup. For instance:
variable "instance_count" {
description = "Number of instances you want"
type = number
default = 5
}
Variable Files: Using the -var-file flag, you can supply variable values to Terraform Apply or Terraform Plan from separate .tfvars files.
Terraform Provisioners:
The components known as Terraform provisioners are used to run scripts or commands on resources after they have been created or changed. After resources have been supplied by Terraform, provisioners give you the ability to carry out operations like software installation, configuration management, or resource initialization.
In Terraform, there are primarily two sorts of provisioners:
Local-exec Provisioner: Runs scripts or commands on the computer where Terraform is installed. It is helpful for activities like running local configuration scripts or configuring local environment variables.
provisioner "local-exec" {
command = "echo it's linux command"
}
Remote-exec Provisioner: Runs scripts or commands on the remote resource that Terraform built. Usually used to install software or start services, this is used to configure the newly provisioned resource.
provisioner "remote-exec" {
inline = [
"linux command 1",
"linux command 2",
"linux command 3",
]
}
Terraform Provisioner:
Backends for Terraform are the parts that control how Terraform handles its state files. Since it keeps track of the current state of your infrastructure resources and their configurations, the state file is essential to Terraform’s functionality. Backends specify how state locking is handled, where this state data is stored, and how it can be retrieved.
Amazon S3, Azure Blob Storage, Google Cloud Storage, HashiCorp Consul, and HashiCorp Terraform Cloud are popular choices for remote backends. Here, we see an example of a Terraform State file using an Amazon S3 bucket as the backend.
terraform {
backend "s3" {
bucket = "my-bucket"
key = "example/terraform.tfstate"
region = "us-east-2"
encrypt = true
dynamodb_table = "my-terraform-table-local"
}
}
Terraform Backends:
You can leverage data from external systems or resources and get it using Terraform data sources in your configurations. You can leverage existing resources that are handled outside of your Terraform configuration as inputs for building and configuring your infrastructure by accessing them through data sources. An overview of Terraform data sources is shown below:
data "aws_vpc" "existing_vpc" {
id = "vpc-12345566970213"
}
Let’s Create amazon EC2 Instance using Terraform:
Using a data source to find the Amazon Machine Image (AMI) ID for Amazon Linux 2, with a 20 GB SSD, within the default VPC, here is an example Terraform configuration that creates one Amazon EC2 instances. It additionally tags instances with “env = production”:
provider "aws" {
region = "us-east-2"
}
data "aws_vpc" "default" {
default = true
}
data "aws_ami" "amazon_linux_2" {
most_recent = true
filter {
name = "name"
values = ["amzn2-ami-hvm-*-x86_64-gp2"]
}
}
resource "aws_instance" "ec2_instance" {
ami = data.aws_ami.amazon_linux_2.id
instance_type = "t2.small"
root_block_device {
volume_size = 20
volume_type = "gp2"
}
subnet_id = data.aws_vpc.default.public_subnets[0]
tags = {
Name = "EC2 Instance"
Env = "production"
}
}
Conclusion: Congratulations! You came a great distance for this Terraform Beginner to Advance: Using AWS Cloud series. We have examined some important terraforming advance topics and necessary commands. Here we put an end to this Terraform series. Stay tuned for further articles as I’ll keep adding new features and updates to this Terraform series.
— — — — — — — —
Here is the End!
Thank you for taking the time to read my article. I hope you found this article informative and helpful. As I continue to explore the latest developments in technology, I look forward to sharing my insights with you. Stay tuned for more articles like this one that break down complex concepts and make them easier to understand.
Happy Learning!