terraform :
top level blocks:
Terrafrom setting block (what terrform vreesion we need to use)
Provider black (like aws, azure or gcp is the provider)
resouce block ( aws_instance is the resource )
input block
output block
local values block
data source block
module block
meta argumetn (depends_on, count, for_each, provider) : meta argument used with any
resouce type to change the behaviour of resource.
setup terrform for aws:
1 need aws account
2. create IAM user as terraform and generate the access_key and secret key
[Link] --configure provide access key and secret key to configure
3.
provider block :
[Link] :-
provider "aws"{
region = "eu-east-1"
}
resource "aws_instace" "demo_instane {
ami = ""
instance_type = "[Link]"
key_name = ""
user_data = <<-EOF // custom startup scrip (not recomended if big script)
#!/bin/bash
sudo apt-get update /var/www/html/[Link]
EOF
provisioner "file" {
source = "/home/[Link]
destination = "/home/ubuntu/[Link]
connection {
type = "ssh"
host = self.public_ip
user = "ubuntu"
private_key = file("/home/aws_key)
timeout = "4m"
}
provisioner "remote-exec" {
inline = [
"chmod +x /home/ubunt/[Link]
"/home/ubuntu/[Link]"
]
}
resource "aws_security_group" "main"{
egress = [
{
cidr_blocks = [ "[Link]/0", ]
description = ""
from_port = 0
ipv6_cidr_blocks = []
prefix_list_ids = []
================================================ Terrafrom variables
==========================================================
terraform variable: its a way to store values and can be reuse through your
terraform configuration
Terrafrom varaiable (Local,input, output)
There are two types of variables in Terraform -
Simple values
Collection Variable
As the name suggests Simple Values variables are which hold only a single value.
Here the types of Simple Value variables -
string
number
bool
1.2 Collection Variable
In the collection variable, it consists of -
List
Map
Set
create an EC2 instance:
provider "aws" {
region = "eu-central-1"
access_key = "<INSERT_YOUR_ACCESS_KEY>"
secret_key = "<INSERT_YOUR_SECRET_KEY>"
}
resource "aws_instance" "ec2_example" {
ami = "ami-0767046d1677be5a0"
instance_type = "[Link]" // when we calling using [Link] then
var.instance_type
tags = {
Name = "Terraform EC2"
}
}
In the above example we are going to parameterized "instance_type"
[Link]
variable "instance_type" {
description = "Instance type [Link]"
type = string
default = "[Link]"
}
#terrafrom validate
#terrafrom plan
#terraform apply
variable "instance_count" {
description = "EC2 instance count"
type = number
default = 2
}
resource "aws_instance" "ec2_example" {
ami = "ami-0767046d1677be5a0"
instance_type = "[Link]"
count = var.instance_count // we
tags = {
Name = "Terraform EC2"
}
}
variable "enable_public_ip" {
description = "Enable public IP address"
type = bool
default = true
}
variable "user_names" {
description = "IAM usernames"
type = list(string)
default = ["user1", "user2", "user3s"]
}
resource "aws_iam_user" "example" {
count = length(var.user_names)
name = var.user_names[[Link]]
}
variable "user_names" {
description = "IAM usernames"
type = list(string)
default = ["user1", "user2", "user3s"]
}
variable "project_environment" {
description = "project name and environment"
type = map(string)
default = {
project = "project-alpha",
environment = "dev"
}
}
variable "project_environment" {
description = "project name and environment"
type = map(string)
default = {
project = "project-alpha",
environment = "dev"
}
}
======================================================Terraform
output====================================
output "instance_ip" {
value = aws_instance.example.public_ip
}
How to pass variable as tfvars file as command-line arguments using the -var-file
flag?
[Link]
project_id = "gcp-terraform-307119"
location = "europe-central2"
[Link]
variable "project_id" {
type = string
description = "The Project ID"
}
we have defined default value in [Link] file
terraform init -var-file=[Link]
terraform plan -var-file=[Link]
terraform apply -var-file=[Link]
What is [Link] and [Link]?
Terraform [Link] is a file where you can define variables for your Terraform
configuration.
This file can contain the variable definitions as well as the optional default
value for the variable. Here is an example of [Link] which has -
[Link]
variable "instance_type" {
type = string
description = "EC2 Instance Type"
}
# No default value
variable "tag" {
type = string
description = "The tag for the EC2 instance"
}
# default value for the variable location
variable "location" {
type = string
description = "The project region"
default = "eu-central1"
}
DEV - [Link]
QA - [Link]
PROD -[Link]
instance_type = "[Link]"
tag = "EC2 Instnace for DEV"
location = "eu-central-1"
instance_type = "[Link]"
tag = "EC2 Instnace for QA"
location = "eu-central-1"
instance_type = "[Link]"
tag = "EC2 Instnace for PROD"
location = "eu-central-1"