Filtering on Data Resources


In normal terraform, I can do something like this to find an AWS Organizational OU by its name:

data "aws_organizations_organization" "this" {

data "aws_organizations_organizational_units" "ou" {
  parent_id = data.aws_organizations_organization.this.roots[0].id

locals {
  specific_ou = [for ou in data.aws_organizations_organizational_units.ou.children: ou if == "ou_name"]

Is there a way to replicate this with CDK?

I am trying with the DataAwsOrganizationsOrganizationalUnits class but not quite sure how I can filter on name as the synth only creates the code and won’t fetch the data (I think).

Locals aren’t natively supported at this time. You should be able to workaround by using an escape hatch.

Something along the lines of:
stack.addOverride('locals.specific_out', [for ou in data.aws_organizations_organizational_units.ou.children: ou if == "ou_name"]'

There isn’t currently control over the name given to data sources, so you’ll need to concatenate the value.

That’s great, thanks jsteinich!

In case it’s of use to anyone here is the code I used:

const org = new DataAwsOrganizationsOrganization(scope, `${name}-org`, {

const ous = new DataAwsOrganizationsOrganizationalUnits(scope, `${name}-ous`, {
	parentId: org.roots('0').id,

props.ousToAttach.forEach(ouName => {
		`\${[for ou in ${ous.fqn}.children: ou if == "${ouName}"][0]}`

	new OrganizationsPolicyAttachment(scope, `${name}-${ouName}-attach`, {
		targetId: 'placeholder',
	}).addOverride('target_id', `\${local.ou_${ouName}.id}`);

Might move away from template literals as gets a bit messy escaping characters.

Thanks for the work on the project, cannot wait for stable release!