Hashicorp Certified Terraform Associate 003 · Free Practice Question Easy
Question 6
When several single-line arguments appear on consecutive lines at the same nesting level in Terraform configuration files, what formatting convention does HashiCorp recommend?
-
A
align the equals signs
- ami = "abc123"
- instance_type = "t2.micro"
-
B
put arguments in alphabetical order
- name = "www.example.com"
- records = [aws_eip.lb.public_ip]
- type = "A"
- ttl = "300"
- zone_id = aws_route53_zone.primary.zone_id
-
C
place all arguments using a variable at the top
- ami = var.aws_ami
- instance_type = var.instance_size
- subnet_id = "subnet-0bb1c79de3EXAMPLE"
- tags = {
- Name = "HelloWorld"
- }
-
D
place a space in between each line
- type = "A"
- ttl = "300"
- zone_id = aws_route53_zone.primary.zone_id
Reveal correct answer
Correct answer: A
Explanation
HashiCorp style conventions suggest that you align the equals sign for consecutive arguments for easing readability for configurations:
- ami = "abc123"
- instance_type = "t2.micro"
- subnet_id = "subnet-a6b9cc2d59cc"
Notice how the equal (=) signs are aligned, even though the arguments are of different lengths.
https://developer.hashicorp.com/terraform/language/syntax/style
A. HashiCorp recommends aligning the equals signs for better readability and consistency in the code. This helps in quickly identifying and understanding the key-value pairs in the configuration.
B. Putting arguments in alphabetical order is not a recommendation from HashiCorp. It may not provide any significant benefit in terms of readability or maintainability of the Terraform configuration.
C. Placing all arguments using a variable at the top is not a recommendation from HashiCorp. While using variables can improve code maintainability, there is no specific guideline to place all arguments using variables at the top.
D. HashiCorp does not recommend placing a space in between each line for consecutive arguments with single-line values at the same nesting level. This practice may introduce unnecessary whitespace and reduce code compactness.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
