HCL language preview
1. Resource block
resource blocks and "${...}" interpolation
horizon-dark
# Web server
resource "aws_instance" "web" {
ami = var.ami_id
instance_type = "t3.micro"
count = 2
tags = {
Name = "web-${count.index}"
Env = var.environment
}
} atom-one-dark
# Web server
resource "aws_instance" "web" {
ami = var.ami_id
instance_type = "t3.micro"
count = 2
tags = {
Name = "web-${count.index}"
Env = var.environment
}
} github-dark
# Web server
resource "aws_instance" "web" {
ami = var.ami_id
instance_type = "t3.micro"
count = 2
tags = {
Name = "web-${count.index}"
Env = var.environment
}
} dracula
# Web server
resource "aws_instance" "web" {
ami = var.ami_id
instance_type = "t3.micro"
count = 2
tags = {
Name = "web-${count.index}"
Env = var.environment
}
} nord
# Web server
resource "aws_instance" "web" {
ami = var.ami_id
instance_type = "t3.micro"
count = 2
tags = {
Name = "web-${count.index}"
Env = var.environment
}
} github
# Web server
resource "aws_instance" "web" {
ami = var.ami_id
instance_type = "t3.micro"
count = 2
tags = {
Name = "web-${count.index}"
Env = var.environment
}
} 2. Terraform settings
terraform, variable, output, and locals blocks
horizon-dark
terraform {
required_version = ">= 1.6.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
variable "environment" {
type = string
default = "dev"
}
locals {
is_prod = var.environment == "prod"
}
output "instance_ids" {
value = aws_instance.web[*].id
} atom-one-dark
terraform {
required_version = ">= 1.6.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
variable "environment" {
type = string
default = "dev"
}
locals {
is_prod = var.environment == "prod"
}
output "instance_ids" {
value = aws_instance.web[*].id
} github-dark
terraform {
required_version = ">= 1.6.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
variable "environment" {
type = string
default = "dev"
}
locals {
is_prod = var.environment == "prod"
}
output "instance_ids" {
value = aws_instance.web[*].id
} dracula
terraform {
required_version = ">= 1.6.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
variable "environment" {
type = string
default = "dev"
}
locals {
is_prod = var.environment == "prod"
}
output "instance_ids" {
value = aws_instance.web[*].id
} nord
terraform {
required_version = ">= 1.6.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
variable "environment" {
type = string
default = "dev"
}
locals {
is_prod = var.environment == "prod"
}
output "instance_ids" {
value = aws_instance.web[*].id
} github
terraform {
required_version = ">= 1.6.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
variable "environment" {
type = string
default = "dev"
}
locals {
is_prod = var.environment == "prod"
}
output "instance_ids" {
value = aws_instance.web[*].id
} 3. Heredoc and dynamic blocks
<<- heredoc and dynamic ingress
horizon-dark
resource "aws_iam_policy" "this" {
name = "example"
policy = <<-EOT
{
"Version": "2012-10-17",
"Statement": []
}
EOT
}
resource "aws_security_group" "this" {
dynamic "ingress" {
for_each = var.ports
content {
from_port = ingress.value
to_port = ingress.value
protocol = "tcp"
}
}
} atom-one-dark
resource "aws_iam_policy" "this" {
name = "example"
policy = <<-EOT
{
"Version": "2012-10-17",
"Statement": []
}
EOT
}
resource "aws_security_group" "this" {
dynamic "ingress" {
for_each = var.ports
content {
from_port = ingress.value
to_port = ingress.value
protocol = "tcp"
}
}
} github-dark
resource "aws_iam_policy" "this" {
name = "example"
policy = <<-EOT
{
"Version": "2012-10-17",
"Statement": []
}
EOT
}
resource "aws_security_group" "this" {
dynamic "ingress" {
for_each = var.ports
content {
from_port = ingress.value
to_port = ingress.value
protocol = "tcp"
}
}
} dracula
resource "aws_iam_policy" "this" {
name = "example"
policy = <<-EOT
{
"Version": "2012-10-17",
"Statement": []
}
EOT
}
resource "aws_security_group" "this" {
dynamic "ingress" {
for_each = var.ports
content {
from_port = ingress.value
to_port = ingress.value
protocol = "tcp"
}
}
} nord
resource "aws_iam_policy" "this" {
name = "example"
policy = <<-EOT
{
"Version": "2012-10-17",
"Statement": []
}
EOT
}
resource "aws_security_group" "this" {
dynamic "ingress" {
for_each = var.ports
content {
from_port = ingress.value
to_port = ingress.value
protocol = "tcp"
}
}
} github
resource "aws_iam_policy" "this" {
name = "example"
policy = <<-EOT
{
"Version": "2012-10-17",
"Statement": []
}
EOT
}
resource "aws_security_group" "this" {
dynamic "ingress" {
for_each = var.ports
content {
from_port = ingress.value
to_port = ingress.value
protocol = "tcp"
}
}
} 4. Heredoc with bare words in the body
a bare word line (e.g. true) no longer closes the heredoc early
horizon-dark
resource "aws_instance" "web" {
user_data = <<EOT
#!/bin/bash
ENABLE_LOGGING=true
if $ENABLE_LOGGING; then
echo "logging enabled"
fi
true
still inside the heredoc
EOT
} atom-one-dark
resource "aws_instance" "web" {
user_data = <<EOT
#!/bin/bash
ENABLE_LOGGING=true
if $ENABLE_LOGGING; then
echo "logging enabled"
fi
true
still inside the heredoc
EOT
} github-dark
resource "aws_instance" "web" {
user_data = <<EOT
#!/bin/bash
ENABLE_LOGGING=true
if $ENABLE_LOGGING; then
echo "logging enabled"
fi
true
still inside the heredoc
EOT
} dracula
resource "aws_instance" "web" {
user_data = <<EOT
#!/bin/bash
ENABLE_LOGGING=true
if $ENABLE_LOGGING; then
echo "logging enabled"
fi
true
still inside the heredoc
EOT
} nord
resource "aws_instance" "web" {
user_data = <<EOT
#!/bin/bash
ENABLE_LOGGING=true
if $ENABLE_LOGGING; then
echo "logging enabled"
fi
true
still inside the heredoc
EOT
} github
resource "aws_instance" "web" {
user_data = <<EOT
#!/bin/bash
ENABLE_LOGGING=true
if $ENABLE_LOGGING; then
echo "logging enabled"
fi
true
still inside the heredoc
EOT
} 5. Nested interpolation
${...} expressions containing their own {} literals
horizon-dark
locals {
payload = jsonencode({
name = var.name
tags = { env = "${var.environment}" }
})
}
output "encoded" {
value = "prefix-${jsonencode({ a = 1 })}"
} atom-one-dark
locals {
payload = jsonencode({
name = var.name
tags = { env = "${var.environment}" }
})
}
output "encoded" {
value = "prefix-${jsonencode({ a = 1 })}"
} github-dark
locals {
payload = jsonencode({
name = var.name
tags = { env = "${var.environment}" }
})
}
output "encoded" {
value = "prefix-${jsonencode({ a = 1 })}"
} dracula
locals {
payload = jsonencode({
name = var.name
tags = { env = "${var.environment}" }
})
}
output "encoded" {
value = "prefix-${jsonencode({ a = 1 })}"
} nord
locals {
payload = jsonencode({
name = var.name
tags = { env = "${var.environment}" }
})
}
output "encoded" {
value = "prefix-${jsonencode({ a = 1 })}"
} github
locals {
payload = jsonencode({
name = var.name
tags = { env = "${var.environment}" }
})
}
output "encoded" {
value = "prefix-${jsonencode({ a = 1 })}"
}