hashicorp / terraform-cdk

Define infrastructure resources using programming constructs and provision them using HashiCorp Terraform
https://www.terraform.io/cdktf
Mozilla Public License 2.0
4.85k stars 451 forks source link

How to iterate over objects made with an iterator? #2023

Closed paymog closed 2 years ago

paymog commented 2 years ago

Community Note

cdktf & Language Versions

0.12 - typescript

This is more of a how to question. The docs for iterators demonstrate how to create several s3 buckets:

import { s3 } from "@cdktf/provider-aws";
import { TerraformIterator, TerraformVariable } from "cdktf";

const list = new TerraformVariable(this, "list", {
  type: "list(string)",
});

const iterator = TerraformIterator.fromList(list.listValue);

const s3Bucket = new s3.Bucket(this, "bucket", {
  forEach: iterator,
  name: iterator.value,
});

How can I then iterate over all of these s3 buckets? As a contrived example, suppose I want to create an EC2 instance for each bucket and pass the bucket ID as an environment variable to it's respective EC2 instance, how can I do that? From what I can tell, s3Bucket itself isn't iterable, does it expose a property that allows users to iterate?

EDIT: to give a more concrete example of what I'm facing right now, I create a number of subnets like so:

    const dataAwsAvailabilityZonesAll =
      new aws.datasources.DataAwsAvailabilityZones(this, "all", {});
    const zoneNames = TerraformIterator.fromList(dataAwsAvailabilityZonesAll.names)

    new aws.vpc.Subnet(this, "priv_subnet", {
      forEach: zoneNames,
      availabilityZone: zoneNames.value,
      cidrBlock: Fn.cidrsubnet(clusterVpc.cidrBlock, 8, Fn.index(dataAwsAvailabilityZonesAll.names, zoneNames.value) + dataAwsAvailabilityZonesAll.names.length),
      mapPublicIpOnLaunch: false,
      vpcId: "${aws_vpc.vpc.id}",
    });

and now I want to make a route table association

    const privateRouteTable = new aws.vpc.RouteTable(
      this,
      "privateRouteTable",
      {
        vpcId: clusterVpc.id,
        route: [
          {
            cidrBlock: "0.0.0.0/0",
            natGatewayId: natGateway.id,
          },
        ],
        tags: {
          name: `privateRouteTable-${environment}`,
        },
      }
    );

    const awsRouteTableAssociationPrivate = new aws.vpc.RouteTableAssociation(
      this,
      "private",
      {
        routeTableId: privateRouteTable.id,
        subnetId:        // <---------- how do I fill this in? I need to somehow iterate over each subnet created earlier
      }
    );

but I'm not sure how to add the ID of each subnet in priv_subnet to awsRouteTableAssociationPrivate

xiehan commented 2 years ago

Hi there:

We use GitHub issues for tracking bugs and enhancements, rather than for questions. While we can sometimes help with certain simple problems here, it's better to use the community forum where there are more people ready to help.

Thanks!

github-actions[bot] commented 1 year ago

I'm going to lock this issue because it has been closed for 30 days. This helps our maintainers find and focus on the active issues. If you've found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.