Day 1: Introduction to Terraform and Terraform Basics
TerraWeek Day 1
What is Terraform and how can it help you manage infrastructure as code?
Terraform is an open-source infrastructure as code (IaC) tool developed by HashiCorp. It enables you to define and provision infrastructure resources in a declarative and version-controlled manner. With Terraform, you can describe your desired infrastructure configuration using a simple and human-readable language called HashiCorp Configuration Language (HCL) or JSON.
Infrastructure as Code (IaC) :- Infrastructure as Code (IaC) is an approach that involves managing and provisioning infrastructure resources using code, just like you would manage software code. Instead of manually setting up and configuring infrastructure components, IaC allows you to define your desired infrastructure state using code.
Think of IaC as a recipe or blueprint for your infrastructure. Instead of manually performing actions like creating virtual machines, configuring networks, or setting up databases, you write code that describes these resources and their desired configurations.
When you use IaC, you can treat your infrastructure as a piece of code that can be version-controlled, reviewed, tested, and deployed. It brings the benefits of software development practices to infrastructure management, such as collaboration, repeatability, and automation.
Why do we need Terraform and how does it simplify infrastructure provisioning?
1. Automating the process: Terraform automates the creation and management of infrastructure resources, reducing manual effort and the risk of errors.
2. Enabling Infrastructure as Code: With Terraform, infrastructure configuration is written as code, allowing for version control, easy collaboration, and reproducibility.
3. Supporting Multiple Platforms: Terraform supports various cloud providers and on-premises infrastructure, providing a unified way to manage different environments.
4. Ensuring Consistency: Terraform ensures consistent infrastructure deployments by applying the same configuration each time, avoiding inconsistencies and configuration drift.
5. State Management: Terraform keeps track of the current infrastructure state, making it easier to manage and update resources without disrupting existing infrastructure.
6. Plan and Apply Workflow: Terraform’s plan and apply workflow allows users to preview and validate infrastructure changes before applying them, ensuring control and reducing the chance of unintended consequences.
Important terminologies of Terraform
1. Providers: Providers are plugins in Terraform that interact with various infrastructure platforms such as AWS, Azure, or Google Cloud. They handle the API interactions and resource management for the specific platform. Providers are responsible for creating, updating, and deleting resources.
Example :
provider “aws” {
region= ”us-east-1”
}
2. Resources: Resources represent the infrastructure components that Terraform manages, such as virtual machines, storage, networks, and databases. Each resource has a specific configuration and attributes.
For example, an AWS EC2 instance resource defines the desired characteristics of a virtual machine
resource “aws_instance” “myec2” {
ami= “ami-04a0ae173da5807d3”
instance_type= “t2.micro”
}
3. Variables: Variables are placeholders for values that can be customized in Terraform configurations. They allow for flexibility and reusability of configurations. Here’s an example of a variable declaration:
variable “region” {
description = “AWS region to deploy resources”
default = “us-west-2”
}
4. Modules: Modules are self-contained, reusable components that encapsulate Terraform configurations. They allow for code organization and reusability by defining a set of resources and variables. For instance, a module for creating an AWS VPC:
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "2.0.0"
cidr_block = "10.0.0.0/16"
region = "us-west-2"
}
5. Outputs: Outputs in Terraform allow you to retrieve and display information about the provisioned infrastructure after applying the configuration. They can be used for informational purposes or to pass values to other configurations. Here’s an example of outputting the IP address of an EC2 instance:
output "instance_ip" {
value = aws_instance.example.public_ip
description = "The public IP address of the instance"
}
How can you install Terraform and set up the environment for AWS?
To install Terraform and set up the environment for AWS, you can follow these steps:
1. Create an AWS Ec2 Instance.
• Login into AWS (https://signin.aws.amazon.com) and search for Ec2.
• Create an Ec2 instance with the preferable OS (here I select Amazon Linux with t2. micro)
2. Install Terraform:
• Go to the official website of Terraform in that go to install Terraform and select Linux and its flavor, here I’ve installed it in Amazon Linux. (https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli)
- Go to the terminal and run these commands.
- To verify your installation, check the version using the command
"terraform –version"
• Terraform is successfully installed.
3. Set Up AWS Credentials:
• Go AWS console and click on security credentials and find for access keys option
• Click on Create access key and the Access key and secret access key are generated for that user.
• Make a note of these credentials as they will be required for Terraform to interact with AWS.
To configure the AWS CLI on your local machine by running the following commands in your terminal:
aws configure
Enter the access key, secret key, default region, and output format when prompted.
Or
Use export commands
export AWS_ACCESS_KEY_ID=
export AWS_SECRET_ACCESS_KEY=
4. Initialize Terraform:
• Create a new directory for your Terraform configuration files and navigate to the new directory.
• Create a new Terraform configuration file with a .tf extension (e.g.- main.tf). This file will contain your infrastructure code.
- In the configuration file, specify the AWS provider and any resources you want to manage. For example, you can add the following code to create an AWS EC2 instance.
provider "aws" {
region= "us-east-1"
}
resource "aws_instance" "myec2" {
ami= "ami-04a0ae173da5807d3"
instance_type= "t2.micro"
vpc_security_group_ids=[aws_security_group.ownsg.id]
tags= {
Name= "terraform-example"
}
}
resource "aws_security_group" "ownsg" {
name= "own-sg"
ingress {
from_port=80
to_port=80
protocol="tcp"
cidr_blocks=["0.0.0.0/0"]
}
}
- Initialize Terraform by Running the following command and download the necessary provider plugins:
"terraform init"
- Before applying changes to your infrastructure, you can generate an execution plan to preview the changes Terraform will make. Run the following command:
"terraform plan"
- This will show you a summary of the changes that Terraform will apply based on your configuration files.
After applying the command, it shows 2 to add which means that 2 resources will be added i.e. ec2 instance and security group. 0 to change = nothing to change & 0 to destroy = nothing to destroy.
- If you’re satisfied with the execution plan and ready to provision or update the infrastructure, run the following command: “terraform apply”
• Terraform will evaluate the configuration files, determine the necessary changes, and prompt you to confirm the changes. You can provide the confirmation by typing “yes” when prompted.
Here you can see Terraform created an ec2 instance with the name tag terror-example.
• If you want to tear down and destroy the provisioned infrastructure, you can use the following command:
"terraform destroy"
• This command will prompt you to confirm the destruction of resources and then proceed with removing them.
Conclusion
From this blog, we can conclude that Terraform is an open-source infrastructure as code (IaC) tool that simplifies managing and provisioning infrastructure resources. It automates the creation and management of resources, supports multiple platforms, and ensures consistency in deployments. Terraform’s plan and apply workflow allows for previewing and validating changes. Key terminologies include providers, resources, variables, modules, and outputs. To set up Terraform for AWS, install it, set up credentials, initialize Terraform in your project directory, and use commands like “terraform plan” and “terraform apply”. Terraform improves automation, collaboration, and control, making infrastructure management more efficient and reliable.
Happy Learning! :)
Thanks for reading. I hope you find this article useful.
Don’t forget to follow me to read more articles related to Cloud Engineering and DevOps.
Basanagouda Patil
Click here to check the LinkedIn Post