Search code examples
amazon-web-servicesmoduleterraformterraform-template-file

Terraform Subnet details not identified inside module


I have Terraform script which will create VPC, load balancers, and ECS. using the module (folder inside the main application folder ) to create task definitions but it will not allow accessing resources created outside this module such as subnets, security groups. I would like to know how to access those resources from this module folder tf


Solution

  • If you want to access, by instance, the ID of the VPC created from a module called create_vpc, you need to export it adding something like this to the module code.

    create_vpc/output.tf:

    output "vpc_id" {
      value="${aws_vpc.my_vpc.id}"
    }
    

    NOTE: obviously you need to create a VPC called my_vpc inside the module, normally in a file called create_vpc/main.tf, but I think that part you have it under control.

    Then, you just need to call the vpc_id output from that module with something like:

    site/main.tf:

    module "create_vpc" {
      source = "../create_vpc"
    }
    
    resource "aws_internet_gateway" "vpc_internet_gateway" {
      vpc_id = "${module.create_vpc.vpc_id}"
    }
    

    NOTE: here the VPC Internet Gateway created is only a sample to use the VPC ID

    In similar way you can export subnets, security group names and ids, etc. from one module to other.