Subchapter 18.2
references/component-blocks.mdMarkdown10 KBView on GitHub
Complete reference for all blocks available in Terraform Stack component configuration files (.tfcomponent.hcl).
Declares input variables for Stack configuration.
variable "variable_name" {
type = <type>
description = "<description>"
default = <value>
sensitive = <bool>
nullable = <bool>
ephemeral = <bool>
}variable "aws_region" {
type = string
description = "AWS region for infrastructure"
default = "us-west-1"
}
variable "identity_token" {
type = string
description = "OIDC identity token"
ephemeral = true
}
variable "subnet_config" {
type = object({
cidr_block = string
availability_zone = string
map_public_ip = bool
})
}For complete variable examples in context, see examples.md.
Declares provider dependencies.
required_providers {
<provider_name> = {
source = "<source>"
version = "<version_constraint>"
}
}required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.7.0"
}
random = {
source = "hashicorp/random"
version = "~> 3.5.0"
}
azurerm = {
source = "hashicorp/azurerm"
version = ">= 3.0"
}
}Configures provider instances.
provider "<provider_type>" "<alias>" {
for_each = <map_or_set> # Optional
config {
<provider_arguments>
}
}config blockfor_each meta-argumentprovider "aws" "main" {
config {
region = var.aws_region
assume_role_with_web_identity {
role_arn = var.role_arn
web_identity_token = var.identity_token
}
}
}For complete provider examples including for_each and multi-cloud patterns, see examples.md.
Defines infrastructure components to include in the Stack.
component "<component_name>" {
for_each = <map_or_set> # Optional
source = "<module_source>"
inputs = {
<input_name> = <value>
}
providers = {
<provider_local_name> = provider.<type>.<alias>[<key>]
}
}The source argument accepts the same module sources as traditional Terraform configurations.
Local File Path:
source = "./modules/vpc"
source = "../shared-modules/networking"Public Terraform Registry:
source = "terraform-aws-modules/vpc/aws"
source = "hashicorp/consul/aws"Format: <NAMESPACE>/<NAME>/<PROVIDER>
Private HCP Terraform Registry:
source = "app.terraform.io/my-org/vpc/aws"
source = "app.terraform.io/example-corp/networking/azurerm"Format: <HOSTNAME>/<ORGANIZATION>/<MODULE_NAME>/<PROVIDER_NAME>
app.terraform.ioterraform.mycompany.com)localterraform.com for deployments spanning multiple Terraform Enterprise instancesGit Repository:
source = "git::https://github.com/org/repo.git//modules/vpc?ref=v1.0.0"
source = "git::ssh://git@github.com/org/repo.git//modules/vpc?ref=main"HTTP/HTTPS Archive:
source = "https://example.com/modules/vpc-module.tar.gz"The version argument is supported only for registry-based sources (public and private registries). Local file paths and Git sources do not support the version argument.
component "vpc" {
source = "app.terraform.io/my-org/vpc/aws"
version = "~> 2.0" # Semantic versioning constraint
inputs = {
cidr_block = var.vpc_cidr
}
providers = {
aws = provider.aws.main
}
}Note: Modules sourced from local file paths always share the same version as their caller and cannot have independent version constraints.
Access component outputs using: component.<name>.<output>
For components with for_each: component.<name>[<key>].<output>
Basic Component:
component "vpc" {
source = "app.terraform.io/my-org/vpc/aws"
version = "2.1.0"
inputs = {
cidr_block = var.vpc_cidr
name_prefix = var.name_prefix
}
providers = {
aws = provider.aws.main
}
}Component with Dependencies:
component "database" {
source = "./modules/rds"
inputs = {
vpc_id = component.vpc.vpc_id
subnet_ids = component.vpc.private_subnet_ids
security_group_ids = [component.security.database_sg_id]
engine_version = var.db_engine_version
}
providers = {
aws = provider.aws.main
}
}For complete component examples including for_each, multi-region, public registry, and multi-provider patterns, see examples.md.
Exposes values from Stack configuration.
output "<output_name>" {
type = <type>
description = "<description>"
value = <expression>
sensitive = <bool>
ephemeral = <bool>
}output "vpc_id" {
type = string
description = "VPC ID"
value = component.vpc.vpc_id
}
output "instance_details" {
type = object({
id = string
public_ip = string
private_ip = string
})
description = "EC2 instance details"
value = {
id = component.compute.instance_id
public_ip = component.compute.public_ip
private_ip = component.compute.private_ip
}
}For complete output examples including sensitive outputs and for expressions, see examples.md.
Defines local values for reuse within the Stack configuration.
locals {
<name> = <expression>
}locals {
common_tags = {
Environment = var.environment
ManagedBy = "Terraform Stacks"
Project = var.project_name
}
name_prefix = "${var.project_name}-${var.environment}"
region_config = {
for region in var.regions : region => {
name_suffix = region
instance_count = var.environment == "prod" ? 3 : 1
}
}
}Declares components to be removed from the Stack.
removed {
from = component.<component_name>
source = "<original_module_source>"
providers = {
<provider_name> = provider.<type>.<alias>
}
}removed {
from = component.old_component
source = "./modules/deprecated-module"
providers = {
aws = provider.aws.main
}
}
removed {
from = component.legacy_regional
source = "registry.terraform.io/example/legacy/aws"
providers = {
aws = provider.aws.main
random = provider.random.main
}
}providers = {
aws = provider.aws.main
}providers = {
aws = provider.aws.main
random = provider.random.main
tls = provider.tls.main
}providers = {
aws = provider.aws.regional[each.value]
}If module requires specific provider aliases:
providers = {
aws.source = provider.aws.us_east
aws.dest = provider.aws.eu_west
}