Terraform
Terraform
Terraform
meta argumetn (depends_on, count, for_each, provider) : meta argument used with any
resouce type to change the behaviour of resource.
provider block :
main.tf :-
provider "aws"{
region = "eu-east-1"
}
ami = ""
instance_type = "t2.micro"
key_name = ""
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"
]
}
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
List
Map
Set
provider "aws" {
region = "eu-central-1"
access_key = "<INSERT_YOUR_ACCESS_KEY>"
secret_key = "<INSERT_YOUR_SECRET_KEY>"
}
ami = "ami-0767046d1677be5a0"
instance_type = "t2.micro" // when we calling using variable.tf then
var.instance_type
tags = {
Name = "Terraform EC2"
}
}
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
}
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"]
}
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"
}
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"
}
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"
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: