You are on page 1of 5

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
3.aws --configure provide access key and secret key to configure
3.

provider block :

main.tf :-

provider "aws"{
region = "eu-east-1"
}

resource "aws_instace" "demo_instane {

ami = ""
instance_type = "t2.micro"
key_name = ""

user_data = <<-EOF // custom startup scrip (not recomended if big script)


#!/bin/bash
sudo apt-get update /var/www/html/index.html
EOF

provisioner "file" {
source = "/home/startup.sh
destination = "/home/ubuntu/startup.sh

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/startup.sh
"/home/ubuntu/startup.sh"
]
}

resource "aws_security_group" "main"{


egress = [
{
cidr_blocks = [ "0.0.0.0/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 = "t2.micro" // when we calling using variable.tf then
var.instance_type

tags = {
Name = "Terraform EC2"
}
}

In the above example we are going to parameterized "instance_type"

variable.tf

variable "instance_type" {
description = "Instance type t2.micro"
type = string
default = "t2.micro"
}

#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 = "t2.micro"
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[count.index]
}

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?

terraform.tfvars

project_id = "gcp-terraform-307119"
location = "europe-central2"

variable.tf

variable "project_id" {
type = string
description = "The Project ID"
}

we have defined default value in terraform.tfvars file

terraform init -var-file=terraform.tfvars


terraform plan -var-file=terraform.tfvars
terraform apply -var-file=terraform.tfvars

What is variable.tf and terraform.tfvars?

Terraform variable.tf 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 variable.tf which has -

variable.tf
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 - terraform-dev.tfvars
QA - terraform-qa.tfvars
PROD -terraform-prod.tfvars

instance_type = "t2.micro"
tag = "EC2 Instnace for DEV"
location = "eu-central-1"

instance_type = "t2.micro"
tag = "EC2 Instnace for QA"
location = "eu-central-1"

instance_type = "t2.micro"
tag = "EC2 Instnace for PROD"
location = "eu-central-1"

You might also like