Data sources allow data to be fetched or computed for use elsewhere in Terraform configuration.
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/availability_zone
data "aws_availability_zones" "available" {}
output "available_zones" {
value = data.aws_availability_zones.available.names
}
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity
Show current account_id
data "aws_caller_identity" "current" {}
output "account_id" {
value = data.aws_caller_identity.current.account_id
}
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region
Show curent region name & description
data "aws_region" "current" {}
output "region_name" {
value = data.aws_region.current.name
}
output "region_description" {
value = data.aws_region.current.description
}
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/vpc
Show vpc id from filter tags
data "aws_vpc" "staging" {
tags = {
"staging"
}
}
output "staging_id" {
value = data.aws_vpc.staging.id
}
provider "aws" {
region = "eu-central-1"
shared_credentials_file = "/home/user/.aws/credentials"
}
data "aws_ami" "ami" {
most_recent = true
owners = [
"amazon"]
filter {
name = "name"
values = [
"amzn2-ami-hvm-*"]
}
resource "aws_instance" "webserver" {
ami = data.aws_ami.ami.id
instance_type = "t2.micro"
}
output "list_aws_id" {
value = data.aws_ami.ami.id
}
output "list_aws_name" {
value = data.aws_ami.ami.name
}
Each Terraform configuration can specify a backend, which defines exactly where and how operations are performed, where state snapshots are stored, etc. Most non-trivial Terraform configurations configure a remote backend so that multiple people can work with the same infrastructure.
provider "aws" {
region = "eu-central-1"
}
terraform {
backend "s3" {
bucket = "bucket_name"
key = "Project/terraform.tfstate"
region = "us-east-1"
}
}
output "something" {
value = ...
}
provider "aws" {
region = "eu-central-1"
}
data "terraform_remote_state" "network" {
backend = "s3"
config = {
bucket = "bucket_name"
key = "Project/terraform.tfstate"
region = "us-east-1"
}
}
Link: https://www.terraform.io/docs/language/settings/index.html