Terraform

Sometimes you need to reference some resources in the same or separate .tf file. Why and how it's done?

Difficulty: unrated

Source: bregman-arie/devops-exercises by Arie Bregman

Answer

Why: because resources are sometimes connected or need to be connected. For example, you create an AWS instance with "aws_instance" resource but, at the same time you would like also to allow some traffic to it (because by default traffic is not allowed). For that you'll create a "aws_security_group" resource and then, in your aws_instance resource, you'll reference it.

How:

Using the syntax ..

In your AWS instance it would like that:

resource "aws_instance" "some-instance" {

  ami           = "some-ami"
  instance_type = "t2.micro"
  vpc_security_group_ids = [aws_security_group.instance.id]

}