Terraform Notes - October - First Week - 2019 - Bangalore | |
submit your details - https://docs.google.com/forms/d/e/1FAIpQLScrY0Y4iex47e7bfxVKiYBVcalH9TRT-Qqe_8WTbusB1jeVoA/viewform | |
JENKINS - ANSIBLE | |
AZURE | |
========================================== | |
WWW.DevOpsSchool.com/slides | |
-------------------------------------------------- | |
Problem Statement? | |
Why we are learning terraform? | |
============================================ | |
Client - Peter | |
Project Mgrx - AccentureMgrX | |
Prod - Prodx | |
----------------------------------------- | |
Objective | |
- Under DevOps | |
- EVERY COMMIT SHOULD GO TO PRODUCTION SAME DAY == Immediate release | |
- Quality Redefined - NO ERROR - SAME DAY MUST BE FIXED = Qulality Release | |
- Low Budget... | |
What is DevOps? - https://www.youtube.com/watch?v=-VaVmb_UOC8 | |
================== | |
Hiring | |
- -------------- | |
Architect - PLANNING - SHAREPOINT, JIRA, Confluence | |
Dev - 10 - 10 Module | |
Code Integration | |
IDE - EClipse + VS | |
Repo - GIT using github | |
Code Analysis | |
Peer Code Review - Gerrit | |
Static Code Analysis - SonarQube | |
Unit Testing | |
Junit - Nunit | |
Build mgmt - maven - Ant - MSBUild | |
Pack mgmt - | |
APP - Jar - WAR - ZIP | |
SYSTEM - exe - rpm - dmg | |
Repo mgmt - Artifactory | |
-------------------- | |
Prodx4.5.Pack ----> 200 Server | |
1 Dep - 9 mins | |
200 DEP - 9 mins | |
100 WIN - 100 LINUX | |
100 APACHE - 100 NGIINX | |
50 T - 50 JB - 50 WB - 50 ISS | |
50 MYSQL - 50 MSSQL - 50 ORA - 50 PS | |
100 dif confi | |
Solutions - Ansible - Puppet - Salt - Chef - cfengine | |
------------------------------------------- | |
How to get 200 boxes? How much time it takes? - 1min | |
==================================================== | |
1 SECOND | |
Docker -> Kubernetes | |
================= | |
AT - Seli | |
================ | |
FEEDBACK - | |
Code Coverage - Jacoco - Covertura - Coverity | |
================================================================================ | |
JENKINS | |
https://www.devopsschool.com/path/ | |
QA | |
IT | |
DB | |
BR | |
OPS | |
SECURITY | |
=============================================== | |
What is Terraform? | |
----- | |
Tool to code for Infra. | |
FOR Infra as CODE. | |
From Hashicrop | |
Varrant | |
Terraform | |
Package | |
Vault | |
Written in GO | |
Release | |
Community - FREE | |
Enterprise - PAID | |
====================================== | |
Why Terrafom? | |
Do we have other tool? - NONE | |
====================================== | |
How to install terraform? | |
WINDOWS | |
LINUX | |
MAC | |
Download -> Unzip -> Set in the path - Verify it? | |
====================================================== | |
https://www.terraform.io/downloads.html | |
C:\Tools\terraform - PATH | |
===================================================== | |
Terraform Manages | |
Providers ??? ---- https://www.terraform.io/docs/providers/index.html | |
AWS(Provider) | |
Resources - > https://www.terraform.io/docs/providers/aws/index.html | |
Attributes | |
Connection | |
Provisionor | |
Where i should store a terraform Resources declarations? | |
-------------------------------- | |
specila config lang of terraform ==> JSON | |
What it contains? | |
provider(s) | |
Resources | |
What file extenstion? | |
.tf | |
How to get a providers? | |
1. Specify which one? | |
2. run | |
$terraform init | |
Woring with basic workflow of terraform? | |
Step1 - Decare a Resources with Attributes | |
Step2 - terraform plan | |
Step3 - terraform apply - CREATING | |
Step4 - terraform apply - UPDATING | |
Step5 - terraform destroy | |
Woring with basic workflow of terraform with GCP | |
# download provider. | |
Step1 - Decare a Resources with Attributes | |
Step2 - terraform plan | |
Step3 - terraform apply - CREATING | |
Step4 - terraform apply - UPDATING | |
Step5 - terraform destroy | |
t2.micro | |
https://www.devopsschool.com/tutorial/terraform/lab/example-program1.html | |
============================================ | |
provider "aws" { | |
region = "eu-west-1" | |
access_key = "" | |
secret_key = "" | |
} | |
provider "google" { | |
credentials = "fpath" | |
project = "DevOps" | |
region = "us-central1" | |
} | |
provider "github" { | |
token = "" | |
organization = "" | |
} | |
resource "github_repository" "example" { | |
name = "example1111" | |
description = "My awesome codebase" | |
private = false | |
} | |
resource "aws_instance" "example" { | |
ami = "ami-04facb3ed127a2eb6" | |
instance_type = "t2.micro" | |
tags = { | |
Name = "HelloWorld" | |
} | |
} | |
================================================== | |
# Example 3 - Variables | |
# Kinds of Variables | |
# INPUT | |
# String | |
# number | |
# bool | |
# list | |
# map | |
# OUTPUT | |
============== String Variable========================= | |
variable "template" { | |
type = "string" | |
default = "01000000-0000-4000-8000-000030080200" | |
} | |
storage = "${var.template}" | |
============== Lists Variable========================= | |
Lists are defined either explicitly or implicitly | |
# implicitly by using brackets [...] | |
variable "cidrs" { default = [] } | |
variable "users" { | |
type = "list" | |
default = ["root", "user1", "user2"] | |
} | |
username = "${var.users[0]}" | |
============= Boolean Variable========================= | |
The last of the available variable types is boolean. They give the option to employ simple true or false values. For example, you might wish to have a variable that decides when to generate the root user password on a new deployment. | |
variable "set_password" { | |
default = false | |
} | |
The above example boolean can be used similarly to a string variable by simply marking down the correct variable. | |
create_password = "${var.set_password}" | |
By default, the value is set to false in this example. However, you can overwrite the variable at deployment by assigning a different value in a command line variable. | |
terraform apply -var set_password="true" | |
============= Map Variable========================= | |
Maps are a collection of string keys and string values. These can be useful for selecting values based on predefined parameters such as the server configuration by the monthly price. | |
We've replaced our sensitive strings with variables, but we still are hard-coding AMIs. Unfortunately, AMIs are specific to the region that is in use. One option is to just ask the user to input the proper AMI for the region, but Terraform can do better than that with maps. | |
Maps are a way to create variables that are lookup tables. An example will show this best. Let's extract our AMIs into a map and add support for the us-west-2 region as well: | |
variable "amis" { | |
type = "map" | |
default = { | |
"us-east-1" = "ami-b374d5a5" | |
"us-west-2" = "ami-4b32be2b" | |
} | |
} | |
A variable can have a map type assigned explicitly, or it can be implicitly declared as a map by specifying a default value that is a map. The above demonstrates both. | |
Then, replace the aws_instance with the following: | |
resource "aws_instance" "example" { | |
ami = var.amis[var.region] | |
instance_type = "t2.micro" | |
} | |
dynamic lookup - The square-bracket index notation used here is an example of how the map type expression is accessed as a variable, with [var.region] referencing the var.amis declaration for dynamic lookup. | |
static value lookup - the map type expression can also use a static value lookup directly with var.amis["us-east-1"]. | |
------------- | |
variable "plans" { | |
type = "map" | |
default = { | |
"5USD" = "1xCPU-1GB" | |
"10USD" = "1xCPU-2GB" | |
"20USD" = "2xCPU-4GB" | |
} | |
} | |
plan = "${var.plans["5USD"]}" | |
The values matching to their keys can also be used to look up information in other maps. For example, underneath is a short list of plans and their corresponding storage sizes. | |
variable "storage_sizes" { | |
type = "map" | |
default = { | |
"1xCPU-1GB" = "25" | |
"1xCPU-2GB" = "50" | |
"2xCPU-4GB" = "80" | |
} | |
} | |
These can then be used to find the right storage size based on the monthly price as defined in the previous example. | |
size = "${lookup(var.storage_sizes, var.plans["5USD"])}" | |
variable "set_password" { | |
default = false | |
} | |
==================================================== | |
Where can you decalare variables? | |
1. .tf inside a resources | |
# must be mentioned with "variable" | |
2. .tfvars | |
var = value | |
3. any file but .tf --- call through --var-file over a commands. | |
4. TF_myname | |
========================================== | |
What is provisionar? | |
============================= | |
chef Provisioner | |
file Provisioner | |
habitat Provisioner | |
local-exec Provisioner | |
puppet Provisioner | |
remote-exec Provisioner | |
salt-masterless Provisioner | |
Which are the options for declaring variable | |
========================================== | |
- Command-line flags | |
$ terraform apply -var 'region=us-east-2' | |
- From a file | |
Create a file named terraform.tfvars with the following contents: | |
region = "us-east-2" | |
- If the file is named something else | |
If the file is named something else, you can use the -var-file flag directly to specify a file. | |
You can use multiple -var-file arguments in a single command, with some checked in to version control and others not checked in. For example: | |
$ terraform apply -var-file="secret.tfvars" -var-file="production.tfvars" | |
- From environment variables | |
Terraform will read environment variables in the form of TF_VAR_name to find the value for a variable. For example, the TF_VAR_region variable can be set to set the region variable. | |
- UI Input | |
If you execute terraform apply with certain variables unspecified, Terraform will ask you to input their values interactively. These values are not saved, but this provides a convenient workflow when getting started with Terraform. | |
- Variable Defaults | |
If no value is assigned to a variable via any of these methods and the variable has a default key in its declaration, that value will be used for the variable. | |
- Using variables in resources | |
========================================= | |
SAMPLE CODE | |
===================================== | |
# http://devopsschool.com/tutorial/terraform/lab/example-program3.html | |
resource "aws_instance" "example" { | |
ami = "${var.ami-mine}" | |
instance_type = "t2.micro" | |
key_name = "rajesh" | |
provisioner "local-exec" { | |
command = "echo ${aws_instance.example.private_ip} >> private_ips.txt" | |
} | |
provisioner "local-exec" { | |
command = "echo mkdir -p /tmp/devopsschool >> script.sh" | |
} | |
provisioner "file" { | |
source = "script.sh" | |
destination = "/tmp/" | |
} | |
connection { | |
type = "ssh" | |
user = "ec2-user" | |
private_key = "${file("rajesh.pem")}" | |
host = "${self.public_ip}" | |
} | |
provisioner "remote-exec" { | |
inline = [ | |
"sudo yum install httpd -y", | |
] | |
} | |
} | |










I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND