Skip to main content
czerasz.com: notes
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Terraform

Use together with count:

element(concat(resources.*.property, [""]), 0)

For Each

resource "aws_instance" "this" {
  for_each = {
    1: ""
  }
  ...

  name = "${each.key}-${each.value}"
}

Custom key Examples:

for instance in aws_instance.this: instance.id => instance.subnet_id
for_each = {for service in var.services: "${service.namespace}.${service.name}" => service}
resource "aws_autoscaling_group" "example" {
  launch_configuration = aws_launch_configuration.example.id
  availability_zones   = data.aws_availability_zones.all.names

  min_size = 2
  max_size = 2

  # Use for_each to loop over var.custom_tags
  dynamic "tag" {
    for_each = var.custom_tags
    content {
      key                 = tag.key
      value               = tag.value
      propagate_at_launch = true
    }
  }
}

Workspaces

List available workspaces:

terraform workspace list

Create new workspace:

terraform workspace new dev

Use specific workspace:

terraform workspace select prod

Show current workspace:

terraform workspace show

One can us a variable which contains the current workspace: "${terraform.workspace}"