Introduction To Terraform
SUMIT Dahiya
"DevOps Enthusiast | Eager Learner | Passionate about Revolutionizing IT through Automation & Continuous Improvement | Skilled in AWS and Azure Cloud Technologies"
Introduction to terraform and terraform basics
what is terraform and how can it help you manage infrastructure as code?
Terraform is an Infrastructure as code tool that lets you build, change, and version cloud on premise resources safely and efficiently. you can then use a consistent workflow to provision and manage all of your infrastructure throughout its lifecycle. Terraform can manage low-level components like compute, storage and networking resources, as well as high-level components like DNS entries and SaaS features.
Why do we need Terraform and how does it simplify infrastructure provisioning?
Terraform is important because it helps us create and manage infrastructure easily using code. It automates the setup of servers, networks, and storage across different cloud platforms or in our own data centers. By writing infrastructure configurations in code, Terraform ensures consistency, reduces mistakes, and allows teams to work together effectively. It simplifies the process of deploying complex systems, speeds up software development, and helps us handle growth. With Terraform, we can easily make changes to our infrastructure, follow best practices, and adapt to new requirements. Overall, Terraform makes managing infrastructure faster, more reliable, and more flexible.
Terraform simplifies infrastructure provisioning by letting you describe your desired infrastructure in a code-like format. Instead of manually setting up servers, networks, and other resources, you write a configuration file that defines what you want. Then, Terraform takes care of creating and managing those resources for you. This automation saves time and reduces errors compared to manual setups. Plus, since your infrastructure is defined in code, you can easily version control and share it with your team. Terraform also works with different cloud providers, giving you flexibility and avoiding lock-in to a specific platform.
By using infrastructure as code, Terraform brings several benefits:
How can you install Terraform and set up the environment for AWS, Azure and GCP?
Install Terraform in Ubuntu
Ensure that your system is up to date and you have installed the?gnupg,?software-properties-common, and?curl?packages installed. You will use these packages to verify HashiCorp's GPG signature and install HashiCorp's Debian package repository.
sudo apt-get update && sudo apt-get install -y gnupg software-properties-common
Install the HashiCorp?GPG key.
wget -O- https://apt.releases.hashicorp.com/gpg | \
gpg --dearmor | \
sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg
Verify the key's fingerprint.
gpg --no-default-keyring \
--keyring /usr/share/keyrings/hashicorp-archive-keyring.gpg \
--fingerprint
The?gpg?command will report the key fingerprint:
/usr/share/keyrings/hashicorp-archive-keyring.gpg
-------------------------------------------------
pub rsa4096 XXXX-XX-XX [SC]
AAAA AAAA AAAA AAAA
uid [ unknown] HashiCorp Security (HashiCorp Package Signing) <[email protected]>
sub rsa4096 XXXX-XX-XX [E]
Add the official HashiCorp repository to your system. The?lsb_release -cs?command finds the distribution release codename for your current system, such as?buster,?groovy, or?sid.
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] \
https://apt.releases.hashicorp.com $(lsb_release -cs) main" | \
sudo tee /etc/apt/sources.list.d/hashicorp.list
Download the package information from HashiCorp.
sudo apt update
Install Terraform from the new repository.
sudo apt-get install terraform
Verify the installation
Verify that the installation worked by opening a new terminal session and listing Terraform's available subcommands.
terraform -help
Build infrastructure of AWS
You will need:
To use your IAM credentials to authenticate the Terraform AWS provider, set the?AWS_ACCESS_KEY_ID?environment variable
install AWS CLI for using aws services
export AWS_ACCESS_KEY_ID=
export AWS_SECRET_ACCESS_KEY=
Now you are ready to write configuration
mkdir learn-terraform-aws-instance
cd learn-terraform-aws-instance
touch main.tf
领英推荐
terraform {
?required_providers {
??aws = {
???source?= "hashicorp/aws"
???version = "~> 4.16"
??}
?}
?required_version = ">= 1.2.0"
}
provider "aws" {
?region?= "us-west-2"
}
resource "aws_instance" "app_server" {
?ami??????= "ami-830c94e3"
?instance_type = "t2.micro"
?tags = {
??Name = "ExampleAppServerInstance"
?}
}
Here are five important terminologies in Terraform with examples:
1. **Provider**: A provider is a plugin that allows Terraform to interact with a specific cloud or infrastructure platform. Providers manage the lifecycle of resources, such as creating, updating, and deleting them. For example, the AWS provider allows you to provision and manage resources on Amazon Web Services (AWS).
2. **Resource**: A resource represents a specific infrastructure component that you want to create and manage. It could be a virtual machine, a network interface, a storage bucket, or any other entity offered by the provider. For instance, the following code creates an AWS EC2 instance:
```hcl
resource "aws_instance" "example" {
?ami??????= "ami-0c94855ba95c71c99"
?instance_type = "t2.micro"
}
```
3. **Module**: A module is a reusable unit of Terraform configuration that encapsulates a set of resources and their associated dependencies. It allows you to organize and package configurations to be used in multiple parts of your infrastructure. For example, you can create a module for provisioning a VPC (Virtual Private Cloud) with subnets, security groups, and routing.
4. **Variable**: Variables are used to parameterize Terraform configurations and make them more flexible and reusable. They allow you to provide dynamic input values at runtime. For example, you can define a variable for the instance type in the AWS EC2 resource:
```hcl
variable "instance_type" {
?description = "Type of EC2 instance"
?default???= "t2.micro"
}
```
5. **Output**: Outputs are used to define values that Terraform should display after applying the configuration. They can be useful for exposing information to other Terraform configurations or for human-readable outputs. For example, you can define an output to display the public IP address of an EC2 instance:
```hcl
output "public_ip" {
?value = aws_instance.example.public_ip
}
```
this is first blog related to day 1 #terraweek challenge.