The dynamic {} block
The dynamic {}
block is a special type of block that can be used to generate nested blocks instead of a value. This is different from the for_each
expression, which dynamically sets the expression value based on the input complex type.
The dynamic {}
block creates a nested block for special resources, data sources, providers, or provisioner blocks that might accept repeatable nested blocks. A very good example of possible usage is DynamoDB table creation using Terraform.
AWS DynamoDB is a non-relational NoSQL database service; it can accept multiple attribute blocks, and these blocks can be automatically constructed using the dynamic {}
block if the values can be passed from a complex type variable.
The following configuration snippet uses the dynamic block to define a DynamoDB table with the AccountId
and AccountName
attributes:
resource "aws_dynamodb_table" "account-table" {
name ...