Terraform Notes
Terraform Notes
-> Terraform code is written in the HashiCorp Configuration langauge (HCL) in files
with the extension .tf
-> Terraform allows users to use HashiCorp Configuration Language (HCL) to create
the files containing definitions of the their desired resources.
-> Terraform Supports all most all cloud providers (AWS, AZURE, GCP, Openstack
etc..).
===============================
Terraform vs Cloud Formation
==============================
-> Terraform uses HashiCorp configuration language (HCL) which built by HashiCorp.
It is fully compatible with JSON.
-> AWS Cloud Formation utilizes either JSON or YAML. Cloud formation has a limit of
51,000 bytes for the template body itself.
==========================
Terraform Vs Ansible
==========================
====================================
Terraform Setup - Pre-Requisites
====================================
1) Cloud Platform Account (AWS, Azure, GCP, Openstack etc..)
2) IAM User account (Secret Key and Access Key)
3) IAM User should have resources Access
###############################
Terraform Installation
#############################
$ terraform -v
###########################################
Working with EC2 Instance using Terraform
###########################################
1) Create IAM user with Programmatic Access (IAM user should have EC2FullAccess)
$ mkdir terraformscript
$ cd terraformscripts
$ vi FirstTFScript.tf
provider "aws" {
region = "ap-south-1"
access_key = "AKIAW4SOJK"
secret_key = "CWSCbZqpIQMkLb1WRB2Xrdufy6/Lp"
}
$ terraform init
$ terraform validate
$ terraform plan
Note: When the script got executed it will store that state in a file. If we
execute script again it will not create. If you delete that state file and execute
script again then it will create it.
-> In first script we kept provider and resources info in single script file. We
can keep provider and resources information in seperate files
#########################################
Script to create multiple Ec2 instances
#########################################
provider "aws" {
region = "ap-south-1"
access_key = "AKIA4MGQ5UW757KVKECC"
secret_key = "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"
}
=====================
Variables in TypeScript
=====================
-> Variables are used to store data in key-value format
Ex:
id = 101
name = Raju
$ vi vars.tf
variable "ami"{
description="Amazon Machine Image value"
default = "ami-05c8ca4485f8b138a"
}
variable "instance_type"{
description="Amazon Instance Type"
default = "t2.micro"
}
variable "instances_count"{
description="Total No.of Instances"
default = "2"
}
$ vi main.tf
provider "aws" {
region = "ap-south-1"
access_key = "AKIA4MGQ5UW757KVKECC"
secret_key = "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"
}
-> Remove instances_count variable from var.tf file and pass like below
=============================
Comments in Terraform Script
=============================
# - single line comment
================================
Dealing with Secret Key and Access Key
================================
-> We have configured secret_key and access_key in terraform script file. Instead
of that we can configure them as environment variables.
$ export AWS_ACCESS_KEY_ID="AKIAYGAYINU"
$ export AWS_SECRET_ACCESS_KEY="50JVwq5SMmvPZbt+fpNRpvw5Zj+3GK09"
$ echo $AWS_ACCESS_KEY_ID
$ echo $AWS_SECRET_ACCESS_KEY
-> Now remove credentials from terraform script and execute it.
=============================
Working with User Data
=============================
-> It is used to execute script when instance launched for first time.
$ vi installHttpd.sh
#!/bin/bash
sudo su
yum install httpd -y
cd /var/www/html
echo "<html><h1>Welcome to Ashok IT...!!</h1></html>" > index.html
service httpd start
-> vi main.tf
provider "aws" {
region = "ap-south-1"
}
==================================
Creating S3 bucket using Terraform script
==================================
provider "aws"{
region = "ap-south-1"
}
bucket = "s3bucketashokit"
acl="private"
versioning{
enabled = true
}
tags = {
Name = "S3 Bucket By Ashok"
}
}
===================================
Create MySQL DB in AWS using Terraform
===================================
provider "aws"{
region = "ap-south-1"
}