Updated on 2023-12-13 GMT+08:00

Style Conventions

Style Conventions

The HCL has some idiomatic style conventions for consistency between files and modules written by different teams. The conventions are recommended for users to follow. They are as follows:

  • Indent two spaces for each nesting level.
  • When multiple arguments with single-line values appear on consecutive lines at the same nesting level, align their equals signs (=).
    name            = "myinstance"
    security_groups = ["default", "internal"]
  • Use empty lines to separate logical groups of arguments within a block.
  • When both arguments and blocks appear together inside a block body, place all of the arguments together at the top and then place nested blocks below them. Use one blank line to separate the arguments from the blocks.
  • List meta-arguments at the top of the block body and separate them from other arguments with one blank line. Place meta-argument blocks at the end of the block body and separate them from other blocks with one blank line.
      count = 1
    
      bucket = "bucket_demo"
      acl    = "public-read"
    
      tags = {
        foo = "bar"
        env = "test"
      }
    
      lifecycle {
        create_before_destroy = true
      }
    }
  • Top-level blocks should always be separated from one another by one blank line.
  • Nested blocks of the same type should be grouped together, while those of different types should be separated by blank lines.