Using resource's id in other resources

Hi.

I’m using the TypeScript CDK and trying to do something simple: set up a VPC network in GCP.

const network = new gcp.ComputeNetwork(this, `primary-network`, {
  name: `primary-network`,
  autoCreateSubnetworks: false,
  routingMode: 'REGIONAL',
})

new gcp.ComputeSubnetwork(this, `private-subnet-${config.region}`, {
  name: `private-subnet-${config.region}`,
  region: config.region,
  ipCidrRange: '10.0.0.0/8',
  privateIpGoogleAccess: true,
  network: network.id,
  dependsOn: [network],
})

The problem I have above is that network.id is of type string | undefined and because of that I cannot assign it to the field ComputeSubnetwork.network which expects a type string:

Hey @kaisellgren, thanks for raising this question. This is a known issue and tracked in a Github issue: https://github.com/hashicorp/terraform-cdk/issues/214 - It’ll be worked on soon. In the meantime, you could overrule the type information with id!

1 Like