The provider meta-argument specifies which provider configuration to use for a resource, overriding Terraform's default behavior of selecting one based on the resource type name.
sample 1
# default configuration
provider "google" {
region = "us-central1"
}
# alternate configuration, whose alias is "europe"
provider "google" {
alias = "europe"
region = "europe-west1"
}
# another alternate configuration, whose alias is usa
provider "aws" {
region = "us-east-1"
alias = "usa"
}
resource "google_compute_instance" "example" {
# This "provider" meta-argument selects the google provider
# configuration whose alias is "europe", rather than the
# default configuration.
provider = google.europe
# ...
}
resource "aws_instance" "aws_usa_instance" {
provider = aws.usa
instance_type = "t3.micro"
ami = "ami-8sdf79sd7f9sd7f8d"
tags = {
Name = "usa server"
}
}
sample 2
...
resource "aws_instance" "server" {
count = 4
# create four similar EC2 instances
ami = "ami-a1b2c3d4"
instance_type = "t2.micro"
tags = {
Name = "Server ${count.index}"
}
}
The count meta-argument accepts a whole number, and creates that many instances of the resource or module. Each instance has a distinct infrastructure object associated with it, and each is separately created, updated, or destroyed when the configuration is applied.
provider "aws" {
region = "eu-central-1"
shared_credentials_file = "/home/user/.aws/credentials"
}
# Creates one user
resource "aws_iam_user" "qa" {
name = "QA"
}
# Creates many users
variable "aws_users" {
default = [
"Developer",
"Admin",
"DevOps"]
}
resource "aws_iam_user" "users" {
count = length(var.aws_users)
name = element(var.aws_users, count.index)
}
The for_each meta-argument accepts a map or a set of strings, and creates an instance for each item in that map or set.
provider "aws" {
region = "eu-central-1"
shared_credentials_file = "/home/user/.aws/credentials"
}
resource "aws_security_group" "webserver" {
name = "webserver security group"
dynamic "ingress" {
for_each = [
"80",
"443",
"22"]
content {
from_port = ingress.value
to_port = ingress.value
protocol = "tcp"
cidr_blocks = [
"0.0.0.0/0"]
}
}
tags = {
Name = "WebserverSG"
}
}
lifecycle is a nested block that can appear within a resource block & can be used with following arguments:
create_before_destroy - New replacement object is created first, and the prior object is destroyed after the replacement is created.
prevent_destroy - Terraform to reject with an error any plan that would destroy the infrastructure object associated with the resource, as long as the argument remains present in the configuration.
ignore_changes - Feature is intended to be used when a resource is created with references to data that may change in the future, but should not affect said resource after its creation.
lifecycle {
create_before_destroy = true
}
lifecycle {
prevent_destroy = true
}
lifecycle {
ignore_changes = [
"Name",
"tags"]
}
depends_on - handle hidden resource or module dependencies that Terraform can't automatically infer.
# aws_security_group will create first
depends_on = [aws_security_group.webserver]