Terraform: Allow one line block

Created on 16 Jul 2019  路  5Comments  路  Source: hashicorp/terraform

version : Terraform v0.12.4

I think using the following block syntax might be easier in some use cases.
See example:

resource "aws_security_group" "backend_sg" {
  name = local.sg_name
  vpc_id = aws_vpc.backend_vpc.id
  ingress { protocol = "tcp" from_port = 22    to_port = 22    cidr_blocks = ["0.0.0.0/0"] }
  ingress { protocol = "tcp" from_port = 80    to_port = 80    cidr_blocks = ["0.0.0.0/0"] ipv6_cidr_blocks ["::/0"] }
  ingress { protocol = "tcp" from_port = 443   to_port = 443   cidr_blocks = ["0.0.0.0/0"] ipv6_cidr_blocks ["::/0"] }


  // Terraform removes the default rule
  egress  { protocol = "-1"  from_port = 0  to_port = 0  cidr_blocks = ["0.0.0.0/0"] }
}

This is the error I got:

Error: Invalid single-argument block definition

  on ../../modules/backend/backend.tf line 56, in resource "aws_security_group" "backend_sg":
  56:   ingress { protocol = "tcp" from_port = 22    to_port = 22    cidr_blocks = ["0.0.0.0/0"] }

A single-line block definition must end with a closing brace immediately after
its single argument definition.
config enhancement proposal

Most helpful comment

I don't know why this was take away but I use it quite frequently in my code for things like tags and it improves the readability of the files. I would like to see it return it possible.

All 5 comments

I don't know why this was take away but I use it quite frequently in my code for things like tags and it improves the readability of the files. I would like to see it return it possible.

This is a requirement for some of the larger configuration files I have to maintain readability. Please stop breaking things.

Agree. Tabular layouts are much easier to read and quickly understand than multi-line layouts.

This is really annoying, is there anyway to bypass this issue.

By the way, terraform fmt does not work on the resource file either.

I am using the latest terraform (v0.12.24)

When defining a lot of environment blocks for kubernetes resources, it gets very annoying to have short statements forced out over multiple lines like this.

env { name = "INSTANCE_IP", value_from { field_ref { field_path = "status.hostIP" } } }

and

http_get { path = "/watchman/", scheme = "HTTP", port = "5000" }

Thats how I would like to write them, comma separated, so as to be completely explicit and unambiguous. However this code here is why I have to write that out as

env {
  name = "INSTANCE_IP"
  value_from { 
    field_ref { field_path = "status.hostIP" } 
  } 
}

and

http_get { 
  path = "/watchman/" 
  scheme = "HTTP"
  port = "5000" 
}

Which when multiplied dozens of times over a single resource block, can turn a 16-32 line terraform file, into a 64 to 256 line file _extremely easily_ which is just downright frustrating.

So it looks like this was explicitly _removed_ by this commit here https://github.com/hashicorp/hcl/commit/bafa0c5ace186f6b3b1ed5c120ab5f11086b5bad merged by @apparentlymart who may have more to say on why this was done.

Edit (more cases where one line syntax is not allowed)

The same commit appears to have been when https://github.com/hashicorp/hcl/blob/hcl2/hclsyntax/parser.go#L179-L188 was added which prevents single line nested block definitions. Which is the cause of line count bloat from instances like this.

env_from { secret_ref { name = kubernetes_secret.environment.metadata[0].name } }

being requied to enter as

env_from {
  secret_ref { name = kubernetes_secret.environment.metadata[0].name }
}
Edit 2 - typo fixes
Was this page helpful?
0 / 5 - 0 ratings