4. Reference Resources

  • We can reference other resources we have defined in our code using their names

  • The order in which we place our code is not necessary in terraform. For example, we can create the AWS subnet before creating the VPC and reference vpc in subnet

  • There are certain cases where terraform can't figure out ordering of resources but terraform docs shows us a workaround

#####################
# Create Subnet
#####################
resource "aws_subnet" "subnet-1" {
  vpc_id = aws_vpc.first-vpc.id
  cidr_block = "10.0.1.0/24"

  tags = {
    Name = "prod-subnet"
  }
}

#####################
# Deploying VPC
#####################
resource "aws_vpc" "first-vpc" {
  cidr_block = "10.0.0.0/16"

  tags = {
    Name = "production"
  }
}

Last updated