Terraform Notes – Day 1 – 18 Sept 2023

DevOps

MOTOSHARE 🚗🏍️
Turning Idle Vehicles into Shared Rides & Earnings

From Idle to Income. From Parked to Purpose.
Earn by Sharing, Ride by Renting.
Where Owners Earn, Riders Move.
Owners Earn. Riders Move. Motoshare Connects.

With Motoshare, every parked vehicle finds a purpose. Owners earn. Renters ride.
🚀 Everyone wins.

Start Your Journey with Motoshare
https://www.scmgalaxy.com/tutorials/terraform-duplicate-required-providers-configuration/
==============================
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?
<blockquote class="wp-embedded-content" data-secret="9qMAPayir8"><a href="https://www.devopsschool.com/blog/terraform-install-configurations/">Terraform Tutorials: Installation & Configurations</a></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; visibility: hidden;" title="“Terraform Tutorials: Installation & Configurations” — DevOpsSchool.com" src="https://www.devopsschool.com/blog/terraform-install-configurations/embed/#?secret=Pw1nkwhKtY#?secret=9qMAPayir8" data-secret="9qMAPayir8" width="600" height="338" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>

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

Code language: JavaScript (javascript)
================================================
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
}
--------------------------------Code language: PHP (php)

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 destroyCode language: PHP (php)

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"
}

Code language: PHP (php)

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

Code language: PHP (php)

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
<blockquote class="wp-embedded-content" data-secret="pVBRJ5TXcD"><a href="https://www.devopsschool.com/blog/terrafrom-example-code-for-remote-exec-provisioner/">Terrafrom – Example Code for remote-exec, local-exec & file provisioner</a></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; visibility: hidden;" title="“Terrafrom – Example Code for remote-exec, local-exec & file provisioner” — DevOpsSchool.com" src="https://www.devopsschool.com/blog/terrafrom-example-code-for-remote-exec-provisioner/embed/#?secret=ktZYYGq9qs#?secret=pVBRJ5TXcD" data-secret="pVBRJ5TXcD" width="600" height="338" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
Code language: HTML, XML (xml)
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x