Hashicorp Certified Terraform Associate 003 · Free Practice Question Medium
Question 1
You are using Terraform to deploy some cloud resources and have developed the following code. However, you receive an error when trying to provision the resource. Which of the following answers fixes the syntax of the Terraform code?
- resource "aws_security_group" "vault_elb" {
- name = "${var.name_prefix}-vault-elb"
- description = Vault ELB
- vpc_id = var.vpc_id
- }
-
A
- resource "aws_security_group" "vault_elb" {
- name = "${var.name_prefix}-vault-elb"
- description = var_Vault ELB
- vpc_id = var.vpc_id
- }
-
B
- resource "aws_security_group" "vault_elb" {
- name = "${var.name_prefix}-vault-elb"
- description = "Vault ELB"
- vpc_id = var.vpc_id
- }
-
C
- resource "aws_security_group" "vault_elb" {
- name = "${var.name_prefix}-vault-elb"
- description = [Vault ELB]
- vpc_id = var.vpc_id
- }
Reveal correct answer
Correct answer: B
Explanation
When assigning a value to an argument in Terraform, there are a few requirements that must be met:
Data type: The value must be of the correct data type for the argument. Terraform supports several data types, including strings, numbers, booleans, lists, and maps.
Value constraints: Some arguments may have specific value constraints that must be met. For example, an argument may only accept values within a certain range or values from a specific set of values.
When assigning a value to an argument expecting a string, it must be enclosed in quotes ("...") unless it is being generated programmatically.
https://developer.hashicorp.com/terraform/language/syntax/configuration#arguments-and-blocks
A. The description value in this choice is missing quotation marks and includes an incorrect variable reference. To fix the syntax error, the description should be enclosed in double quotes like "Vault ELB".
B. The syntax error in the original code is that the description value is missing quotation marks. By adding the quotes around "Vault ELB", the code will be corrected and the provisioning process will not throw an error.
C. The description value in this choice is enclosed in square brackets, which is incorrect syntax for a string in Terraform. Strings should be enclosed in double quotes, so the correct syntax would be to use double quotes around "Vault ELB".
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
