Terraform is an open-source infrastructure as code software tool created by HashiCorp. It enables users to define and provision infrastructure using a high-level configuration language known as HCL (Hashicorp Configuration Language). Terraform is an IAC tool, used primarily by DevOps teams to automate various infrastructure tasks. The provisioning of cloud resources, for instance, is one of the main use cases of Terraform. It's a cloud-agnostic, open-source provisioning tool written in the Go language and created by HashiCorp.
IAC or Infrastructure as Code enables us to manage infrastructure in the form of code. Using Terraform we can automate the process of provisioning and destroying infrastructure.
Let's get started. . .
1. Download Terraform on a Linux machine
Open your Linux terminal and run the following commands to Install Terraform.
sudo apt-get update && sudo apt-get install -y gnupg software-properties-common curl
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt-get update
sudo apt-get install terraform
Check the downloaded version of Terraform using the below command:
terraform version
2. Create AWS User with permission
Create a User and give it permission to interact with AWS from the local machine. Login to the AWS account and in services search for IAM. In side pane select Users and click Add users , enter name of the user and check the box Access key - Programmatic access and click next:Permissions
Select Attach existing policies directly from the top menu and check AmazonEC2FullAccess and click Next: Tags , Review , and Add user.
Copy and keep the Access key ID and Secret access key safe for later use or Download the CSV file.
3. Configure AWS keys
Install AWS CLI using the below commands:
sudo apt install awscli
aws configure
Enter the region and access key details when prompted:
AWS Access Key ID :
AWS Secret Access key:
Default region: us-east-1
Default Output: json
Configure profile to store access key and secret access key. Cat config file located at path ~/.aws/
Edit the file and enter the access key and secret access key and give a name to profile
[default]
aws_access_key_id = <ACCESS_KEY_ID>
aws_secret_access_key = <SECRET_ACCESS_KEY>
region = us-east-1
4. Create terraform configuration file
A Terraform configuration is a complete document in the Terraform language that tells Terraform how to manage a given collection of infrastructure. A configuration can consist of multiple files and directories. Create a configuration file with a .tf extension in any editor or IDE and save the file.
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "my_ec2_instance" {
ami = "ami-08c40ec9ead489470"
instance_type = "t2.micro"
tags = {
Name = "FirstEC2Instnace"
}
}
To format the configuration file run the below command in the terminal in the same directory where the .tf file is present.
terraform fmt
Initialize terraform using the below command. It's the same as the git init command in GIT. This command will initialize the working directory containing Terraform configuration files and install any required plugins
terraform init
To check that the configuration file is error-free, run the following command after terraform init:
terraform validate
Run the below which lets you preview the actions Terraform would take to modify your infrastructure or save a speculative plan which you can apply later.
terraform plan
Finally, run the below command. This is the same as git push. This command executes the actions proposed in a terraform plan. It is used to deploy your infrastructure. Typically apply should be run after terraform init and terraform plan. Enter yes when prompted "Enter a value:".
terraform apply
You can see the instance is running in your AWS Console. You will see terraform.tfstate file is created after running the terraform apply command. This state is used by Terraform to map real-world resources to your configuration, keep track of metadata, and improve performance for large infrastructures. This state is stored by default in a local file named "terraform. tfstate", but it can also be stored remotely, which works better in a team environment.
5. Destroy infrastructure
To destroy the resources you have just created run the below command. The terraform destroy command terminates resources managed by your Terraform project. This command is the inverse of terraform apply in that it terminates all the resources specified in your Terraform state. It does not destroy resources running elsewhere that are not managed by the current Terraform project.
terraform destroy
Top comments (0)