2/22/2022
Terraform - class notes
Recap
Teffaform Lifecycle
- init
- plan
- apply
- destroy
file: main.tf
we can add info about,
- provider
- variables
- resources
It maintains the desired state.
How?
- it maintains the terraform.tfstate file.
- By default it stores on local machine.
- You have to store it on remote location (such as s3 bucket, blob storage)
Terraform authentication
- aws configure
1. Create 2 aws instance t2.mocro
ansible-host
ansible-client
2. Login to your system
$ sudo -i
# cd example
# cat backend.tf
# cat main.tf
we want to seperate our instance on environment lavel,
dev, different type,
# vi variables.tf
variable "image_id" {
type = string
}
variables in terraform
# vi main.tf
terraform {
.
required_version = >= 0.14.9"
}
provider "aws" {
profile = "default"
region = "us-west-2"
}
.
resources "ami_instance" "app_server" {
ami = var.image_id
instance_type = "t2.micro"
tags = {
name = "demo"
}
# vi dev.tfvars
image_id = "ami-830c94e3"
# cp dev.tfvars test.tfvars
# cp dev.tfvars prod.tfvars
here,
dev.tfvars => variable.tf => main.tf
# vi variables.tf
variable "image_id" {
type = string
default = "" # defaine the default value here
}
# vi main.tf
# cp main.tf provider.tf
# vi provider.tf
terraform
provider
remove everything else
# tf plan -out dev.plan -var-file dev.tfvars
# vi main.tf
resources "ami_instance" "app_server" {
ami = var.image_id
instance_type = "t2.micro"
tags = {
name = var.tag_name
}
# variables.tf
variable "imae_id"
type = string
default = ""
}
variable "tag_name"
type = string
default = "" .... [8:00]
}
vi dev.tfvars
image_id = ani_84...
tag_name = "example Demo"
# terraform plan -out dev.plan -var-file dev.tfvars
If aws cli is not configured,
Go to aws, user -> credential -> delete old key and create new
copy
# aws configure
access key: *******
secret key: *******
# tf apply dev.plan
It will create example demo instance. Login to aws console and check...
check out this url for example ..
https://github.com/qfitsolutions/aws-terraform-course/blob/master/EC2withJenkins/ec2_jenkins.tf
google: for other platform,
azure terraform examle
use: azure cli
login/authentication
- create terraform file
provider "azurerm"
for google cloud: google cloud,,,
you can configure more than one instance in the same config file..
for eg,
if you want to create an instance on different region,
get the ami for specific location,
# vi variable.tf
# ec2_jenkins.tf
# terraform init
error: invalid quoted type constraints..
varibable "region"
type = string # remove double quote
default = "us_east_1"
read the error carefuly. change, try and learn ..
# tf init
warnings sometimes can be ignored ..
# sh abv.sh
# vi abc.sh
#!/bin/bash
yum update -y
yum install htpd.x86_68 -y
service httpd start/enable
echo "<h1> Deployed via terraform</h1> sudo tee /var/www/html/index.html
yum install java.. -y
wget -o /etc/yum.repos.d/jenkins.repo
https://pkg.jenkins.io/redhat-stable/jenkins.repo
rpm --import htps://pkg.jenkins.io/redhat-stable/jenkins.io.key
yum upgrade
yum install fontconfig java-11-openjdk
yum install jenkins
# terraform destroy
==================
Ansible Roles
- Reusable components
on playbook:
decleare
jenkins playbook:
roles:
nginx
jenkins
java
nexus:
roles:
java
nexus
nginx
on terraform
we use resources
reources # ec2 ...vpc, security group, s3, eks ...
reusable components
parameters ...
next lavel extracction is module ..
modules contains multiple tf file
main.tf
module:
vpc
eks
ecs
terraform template (abc.tf) => module => ec2_instance.tf
eks.tf
cf.tf
-------
# vi main.tf
download the code ..
https://github.com/easyawslearn/terraform-aws-instance-template
$ cat variables.tf
variable "ami_id" {}
variable "region" {}
variable "instance_type" {}
variable "tag" {
default="Testing"
}
$ cat main.tf
provider "aws" {
region = "${var.region}"
}
resource "aws_instance" "web" {
ami = "${var.ami_id}"
instance_type = "${var.instance_type}"
tags = {
Name = "${var.tag}"
}
}
$ cat output.tf
output "instance_ip" {
value = ["${aws_instance.web.public_ip}"]
}
# terrafrom init ..
# terraform plan
you can get ready made modules
vpc terraform module
vpc terraform
----------------
Note: if you have dev.tfvars, test.tfvars, prod.tfvars
whatever you created last, will have latest tfvars file. which you can use to destroy the resources associated with.
This is the reason, you will use teraform workspace..
# terrafrom workspace list
# terrfrom init --reconfigure
# terrafrom worksapce list
# terrafrom init -migrate-state
# ls -ltr
# # rm -rf *.terraform
# terraform init
# terrafrom worksapce list
# terraform worksapce new dev
# terrafrom worksapce new test
# tf worksapce list
# tf worksapce select dev
# tf worksapce select prod
you can use different providers such as azure, gcp, k8s, docker
Continouse monitoring (CM)
- troubleshooting
- high availibity
- infracture /application health check
Monitorying are 2 types
- application
- logs
- status
- infre:
CPU, memory, users, ports enable..
Log collection
----------------
n1/d1 (agent) n2/d2 n3/d3 => stored data (DB) dashboard (log analyzer)
elk/splunk
preometheus/grafana
https://prometheus.io/docs/introduction/overview/
grafana is a dashboard and preometheus is a log collector
Tomorrow
--------
ELK stack