Terraform Notes – Day 1 – 18 Sept 2023
Limited Time Offer!
For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!
==============================
AWS
==============================
CMD
Windows
------------------------------------
A -> B --> C
------------------------------------
====================================================
Discuss --> Demo --> LAB
---------------- -------------
20 mins 30 mins
--------------------------------------------------------------------
What is Terraform?
---------------------------------------
I A A C
---------
Coding for Infra
Release
community - cli - free [ FOCUS ]
cloud - web - Paid
Enterprise - web - paid
Writren Go
Platform ind - win - linux - mac
Hashicorp
https://www.hashicorp.com/
What is Infra? - 3522
---------------------------------------
https://registry.terraform.io/browse/providers
Servers - Networks -
AWS -> CloudFormation YAML - only for AWS
Azure -> ARM YAML - only for Azure
Vmware
GC
Kubernetes
Why Terraform?
=================================
- CODE for All
- ONE CODING STANDARD
- Consistent change managment
- Desire based (IDEOMPOTENT)
Declare our desire(Yaml/Json|Xml) --> Tool
using HCL
------------------
Easy to learn - debug - test - extend - share
=================================
How Terraform Works?
How to install terraform?
Terraform Tutorials: Installation & Configurations
How to set environment variable using command line
Windows - cmd
$ set PATH "%PATH%;C:\tools\terraform";
$ setx PATH "%PATH%;C:\tools\terraform";
Windows - powershell
$env:PATH += ";C:\tools\terraform"
Linux - shell
export PATH=$PATH:/opt/terraform
Gitbash - shell
export PATH=$PATH:/opt/terraform
================================================
How to download Terraform Providers?
-------------------------------------------------
Using Code
---------------------------
How to store terraform code?
- anyfilename.tf
In one directory, if we have one or multiple terraform code, its considered a ONE terraform code Only
one.tf + two.tf + third.tf
-------------------------
oNe code
AWS
Azure
Github
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.17.0"
}
}
}
provider "aws" {
# Configuration options
}
How to download?
-----------------------
$ terraform init
How to see list of providers downloaded
$ terraform providers
-------------------------
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.17.0"
}
azurerm = {
source = "hashicorp/azurerm"
version = "3.73.0"
}
github = {
source = "integrations/github"
version = "5.37.0"
}
}
}
provider "aws" {
# Configuration options
}
provider "azurerm" {
# Configuration options
}
provider "github" {
# Configuration options
}
--------------------------------
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance
https://registry.terraform.io/providers/hashicorp/aws/latest/docs
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/virtual_machine
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs
========================================================
API
Every feature - resource
AWS
resource1
arguments1 REQUIRED|Optional
arguments2 REQUIRED|Optional
arguments3 REQUIRED|Optional
resource2
arguments1 REQUIRED|Optional
arguments2 REQUIRED|Optional
arguments3 REQUIRED|Optional
resource3
arguments1 REQUIRED|Optional
arguments2 REQUIRED|Optional
arguments3 REQUIRED|Optional
Azure
resource1
arguments1 REQUIRED|Optional
arguments2 REQUIRED|Optional
arguments3 REQUIRED|Optional
resource2
arguments1 REQUIRED|Optional
arguments2 REQUIRED|Optional
arguments3 REQUIRED|Optional
resource3
arguments1 REQUIRED|Optional
arguments2 REQUIRED|Optional
arguments3 REQUIRED|Optional
Arguments MUST be one of these
---------------------------------
integ
string
bool
list
dict
aws.tf
---------------------------------------
resource "aws_instance" "web" {
ami = "ami-053b0d53c279acc90"
instance_type = "t3.micro"
tags = {
Name = "HelloWorld-rajesh"
}
}
$ terraform init
$ terraform validate
$ terraform plan
$ terraform apply
$ terraform show
$ terraform destroy
Single-line comments:
# begins a single-line comment, ending at the end of the line.
// also begins a single-line comment, as an alternative to #.
Multi-line comments:
/* and */ are start and end delimiters for a comment that might span over multiple lines.
=============================================================================
How to work with Another providers?
--------------------------------------
Step 1- Add provider in tf code and Set AUTH
Step 2 - Add a resources of that part* providers
Step 3 -
$ terraform init
$ terraform validate
$ terraform plan
$ terraform apply
$ terraform show
$ terraform destroy
https://www.devopsschool.com/blog/github-tutorials-how-to-generate-pat-in-github/
resource "github_repository" "example" {
name = "example-XBBSHSGSGSS"
description = "My awesome codebase"
visibility = "public"
}
provider "github" {
token = "ddddddo1z"
}
Assignment
OUTPUT VALUES
=================================================================
OUTPUT Block which would display a vars from statefile.
---------------------------
https://www.devopsschool.com/blog/terraform-output-variable-example/
output "instance_public_ip" {
value = aws_instance.web.public_ip
}
output "ec2_instance_ips" {
value = [for instance in aws_instance.ec2_instances : instance.private_ip]
}
output "ec2_instance_pubips" {
value = [for instance in aws_instance.ec2_instances : instance.public_ip]
}
output "instance_public_ip" {
value = aws_instance.web.security_groups[0]
}
# How to display tags. Write a output block
output "github_repo_url" {
value = github_repository.example.http_clone_url
}
$ terraform apply --auto-approve
$ terraform output
Terraform Provisionar
https://www.devopsschool.com/blog/terraform-provisioners-tutorials-and-complete-guide/
\https://developer.hashicorp.com/terraform/language/resources/provisioners/null_resource
file-exec - COPY A file from HOST to the Vm created at AWS/Azure
connection to vm
local-exec - Running a cmd/script in HOST after vm created.
remote-exec - Running a cmd/script in VM after vm created.
connection to vm
Terrafrom – Example Code for remote-exec, local-exec & file provisioner