day-01-Terraform
Day-01
What is Infrastructure as Code with Terraform
Infrastructure as code (IaC) tools allow you to manage infrastructure with configuration files rather than through a graphical user interface. IaC allows you to build, change, and manage your infrastructure in a safe, consistent, and repeatable way by defining resource configurations that you can version, reuse, and share.
Terraform is HashiCorp's infrastructure as code tool. It lets you define resources and infrastructure in human-readable, declarative configuration files, and manages your infrastructure's lifecycle. Using Terraform has several advantages over manually managing your infrastructure:
Terraform can manage infrastructure on multiple cloud platforms.
The human-readable configuration language helps you write infrastructure code quickly.
Terraform's state allows you to track resource changes throughout your deployments.
You can commit your configurations to version control to safely collaborate on infrastructure.
Manage any infrastructure
Terraform plugins called providers let Terraform interact with cloud platforms and other services via their application programming interfaces (APIs). HashiCorp and the Terraform community have written over 1,000 providers to manage resources on Amazon Web Services (AWS), Azure, Google Cloud Platform (GCP), Kubernetes, Helm, GitHub, Splunk, and DataDog, just to name a few. Find providers for many of the platforms and services you already use in the Terraform Registry. If you don't find the provider you're looking for, you can write your own.
Standardize your deployment workflow
Providers define individual units of infrastructure, for example compute instances or private networks, as resources. You can compose resources from different providers into reusable Terraform configurations called modules, and manage them with a consistent language and workflow.
Terraform's configuration language is declarative, meaning that it describes the desired end-state for your infrastructure, in contrast to procedural programming languages that require step-by-step instructions to perform tasks. Terraform providers automatically calculate dependencies between resources to create or destroy them in the correct order.
To deploy infrastructure with Terraform:
Scope - Identify the infrastructure for your project.
Author - Write the configuration for your infrastructure.
Initialize - Install the plugins Terraform needs to manage the infrastructure.
Plan - Preview the changes Terraform will make to match your configuration.
Apply - Make the planned changes.
Track your infrastructure
Terraform keeps track of your real infrastructure in a state file, which acts as a source of truth for your environment. Terraform uses the state file to determine the changes to make to your infrastructure so that it will match your configuration
Collaborate
Terraform allows you to collaborate on your infrastructure with its remote state backends. When you use Terraform Cloud (free for up to five users), you can securely share your state with your teammates, provide a stable environment for Terraform to run in, and prevent race conditions when multiple people make configuration changes at once.
You can also connect Terraform Cloud to version control systems (VCSs) like GitHub, GitLab, and others, allowing it to automatically propose infrastructure changes when you commit configuration changes to VCS. This lets you manage changes to your infrastructure through version control, as you would with application code.
Install Terraform:
Amazon-Linux
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
sudo yum -y install terraform
Verify the Installation
terraform -help
Troubleshoot
If you get an error that terraform could not be found, your PATH environment variable was not set up properly. Please go back and ensure that your PATH variable contains the directory where Terraform was installed.
Enable tab completion
If you use either Bash or Zsh, you can enable tab completion for Terraform commands. To enable autocomplete, first ensure that a config file exists for your chosen shell.
touch ~/.bashrc
Then install the autocomplete package.
terraform -install-autocomplete
Once the autocomplete support is installed, you will need to restart your shell.
Terraform Basic Commands:
Step-01: Introduction
---- Understand Basic Terraform Commands
-- terraform init
-- terraform validate
-- terraform plan
-- terraform apply
-- terraform destroy
Step-02: Review terraform manifest for EC2 Instance
Pre-Conditions-1: Ensure you have default-vpc in that respective region
Pre-Conditions-2: Ensure AMI you are provisioning exists in that region if not update AMI ID
Pre-Conditions-3: Verify your AWS credentials in $HOME/.aws/credentials
# TERRAFORM SETTINGS BLOCK
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "4.67.0"
}
}
}
# PROVIDER BLOCK
provider "aws" {
profile = "default" # AWS credentials profile configured on your local desktop terminal $HOME/.aws/credentials
region = "ap-northeast-1" # specify your own region
}
# RESOURCE BLOCK
resource "aws_instance" "ec2demo" {
ami = "ami-03dceaabddff8067e" # amazon-linux in ap-northeast-1, update as per your region
instance_type ="t2.micro"
}
NOTE: in the PROVIDER BLOCK, the region attribute can be configured as, follow-below two screenshots.
Step-02: Terraform Core Commands
terraform init
# Initialise Terraform directory
terraform validate
# Terraform Validate
terraform plan
# Terraform plan to Verify what it is going to Create / Update / Destroy
terraform apply
# Terraform Apply to create EC2 Instance
Step-03: Verify the CE2 Instance in AWS Management Console
-- Go to AWS Management console -> Services -> EC2
-- Verify newly created EC2 Instance