C2W3 Lab1 Terraform syntax

I really struggle with Terraform syntax and how to call items that were previously defined. I got an error for key_name (last variable in code block to be defined) because aws_key_pair.bastion_host_key_pair.name is wrong. I’ve gone through the videos several times and am still struggling, are there any other resources?

# Create an AWS key pair, associating the public
# key you created in the first resource in this file
resource "aws_key_pair" "bastion_host_key_pair" {
  key_name   = "${var.project}-bastion-host-key"
  public_key = tls_private_key.bastion_host_key_pair.public_key_openssh
}

# Complete the configuration for the EC2 instance
# for the bastion host
resource "aws_instance" "bastion_host" {
  ami                         = data.aws_ami.latest_amazon_linux.id
  instance_type               = "t3.nano"                                   # Use the t3.nano instance type
  key_name                    = aws_key_pair.bastion_host_key_pair.name # Associate the aws key pair you created above

Hello @uebelvan


You want the key_name attribute from the resource you created right before. Therefore, you should use aws_key_pair.bastion_host_key_pair.key_name instead of aws_key_pair.bastion_host_key_pair.name.
This is a exercise lab, you can always refer to the available solutions to get a better understanding of the lab :slightly_smiling_face:
Happy learning!

Hi Amir, thanks. I guess I almost got it, but have been confused about the last part (attribute). There are linked resources to a list of attributes (id, name, address etc), so I thought for this case I had to use name (generic attribute), but do I actually have to use the variable name “key_name”?

I’m currently re-watching the videos before the practice lab and am confused about why in the last row in the screenshot we write “id” as the attribute, while the data block (line 6-17) doesn’t have id defined in it. When do we use generic attributes, and when do we use variables declared inside other code blocks?

I submitted the lab but I don’t see any way to look at the solution that you mentioned. Can you please guide me there?

Hello @uebelvan
As far as I know, generic attributes are created automatically, while in this case we want the key_name attribute which is user-defined.
You can refer to step 8 in Vocareum instructions for downloading the solution files: