Terraform Associate Exam QuestionsBrowse all questions from this exam

Terraform Associate Exam - Question 57


How would you reference the "name" value of the second instance of this fictitious resource?

Show Answer
Correct Answer: B

In Terraform, when using the count argument in a resource block, the instances are indexed starting from 0. To reference the 'name' value of the second instance, you would use the 1-based index, which corresponds to the 0-based index used internally by Terraform. Therefore, the correct reference to the 'name' value of the second instance would be aws_instance.web[1].name, utilizing the appropriate index.

Discussion

34 comments
Sign in to comment
franksoul
Jun 9, 2022

Dear ALL, tested in Lab and Answer is : B

bigboi23Option: B
May 18, 2022

B!!!!!!!!

NunyabiznesOption: B
Mar 21, 2023

The count.index starts from 0. Therefore, the second instance would have an index of 1. In that case, the correct answer would be B. aws_instance.web[1].name.

BereOption: B
Aug 7, 2023

Example: resource "aws_instance" "web" { count = 2 name = "terraform-${count.index}" } output "second_instance_name" { value = aws_instance.web[1].name }

kcw6
Aug 4, 2022

B is correct. resource "aws_iam_user" "lb" { name = "terraform-${count.index}" count = 2 path = "/system/" } output "testing" { value = aws_iam_user.lb[1].name } ===== output ====== Plan: 2 to add, 0 to change, 0 to destroy. Changes to Outputs: + testing = "terraform-1"

Ipergorta
May 5, 2022

B for sure

JoG1221
May 7, 2022

B..it is

Bluemoon22
Apr 24, 2023

The answer is B, aws_instance.web[1].name

Jedi
May 2, 2022

I vote for B also. There is no reference to "name" in A

temp111
May 6, 2022

it must be B.

Sathya22
May 17, 2022

Answer is option B .Index starts at 0 , so name[1] is equivalent to second resource . SO this should be the answer

Daniel2020Option: B
Jul 5, 2022

B - aws_instance.web[1].name !!! Index starts at 0 so the second instance would be 1 - the link below confirms this: https://www.terraform.io/language/meta-arguments/count#referring-to-instances

Ell89Option: B
Mar 12, 2023

its B - people who answer incorrectly with such conviction and belief are dangerous.

samimshaikhOption: B
Dec 29, 2023

because the index starts from 0 so second instance name will be at 1

nhaastrup
Apr 30, 2024

To reference the "name" value of the second instance of the fictitious resource "aws_instance" named "server", you can use the element index notation in Terraform. The element index notation allows you to access elements in a list or collection by their index. Given that the index in Terraform starts from 0, the second instance has an index of 1. Therefore, to reference the "name" value of the second instance = B

debabrata6983Option: B
Aug 26, 2023

This is the very basics of terraform resource referencing

Ipergorta
Apr 30, 2022

Why no B?

rogerxavier
May 13, 2022

It is correct, really is letter A, following the offcial answer above: element(list, index) - Returns a single element from a list at the given index. If the index is greater than the number of elements, this function will wrap using a standard mod algorithm. This function only works on flat lists. Examples: element(aws_subnet.foo.*.id, count.index) element(var.list_of_strings, 2)

stalk98
Jun 8, 2022

B from doc

kopper2019Option: B
Aug 10, 2022

B, aws_instance.web[1].name

Power123
Mar 31, 2023

B is correct

kingfighers
Apr 2, 2024

A. element(aws_instance.web, 2) is correct if we want to access the 3rd one, but there is no "the third one"

vitasacOption: D
May 3, 2022

D for sure

smyndlo
May 5, 2022

Index start with [0], so the second instance will be aws_instance.web[1]. Answer is B

vitasac
May 10, 2022

Yes your're right

vitasac
May 10, 2022

Yes your're right

cytronOption: A
Jun 3, 2022

They are two values of the name. name = terraform-1 name = terraform-2 The index is not assigned to the key (name) but to the value (terraform-$(count.index). Remember this is not a list assigned to a variable, don't reference it as a list. So you can't call the resource by specifying the index directly onto the key like name[1] simply because the the index is assigned to a part of the value which is a string not a list. The only way I think is to use the element function to call the resource after which specify the index number as an argument to the function - element(resource, index)

CHRIS12722222
Jun 19, 2022

Option A is wrong because the element (list, index) is zero-based, so if at all we should use this function, the index ought to be 1, not 2 https://www.terraform.io/language/functions/element

Anonymous
Jun 21, 2022

Don't understand why you are suggesting A. Surely there will be two ec2 instances created here, you can just get the name property for those instances using B. The code terraform-${count.index} has no bearing on execution of the loop or anything to do with list's indexes.

EltoothOption: B
Jun 28, 2022

B is correct answer.

legendary7
Sep 27, 2022

B is the correct answer no doubt.

RVivekOption: B
Sep 28, 2022

B is the right fromat

asudhinOption: B
Oct 3, 2022

It is B

adoubanOption: B
Dec 9, 2022

B is the correct answer

Bebins
Dec 28, 2022

The output of A won't return the name of the 2nd instance. So here the answer is "B"

oab720Option: B
Jan 8, 2023

Tried and tested, definitely B

thor7Option: B
Mar 29, 2023

B is a correct answer, checked.

gofavad926Option: B
Sep 29, 2023

B for sure

nmnm22
Aug 5, 2024

why is it index number AND name, though? i would have thought that specifying the index number is enough