Home > Software design >  AWS CDK. How to pass `id` of first stack to second stack
AWS CDK. How to pass `id` of first stack to second stack

Time:04-04

I've a project with multiple stacks, but with common stacks.ts file.

  • My 1 stack creates a VPC
  • My 2 stack creates a ECS

Stack vpc.tf

export class stack-vpc extends Stack {
   constructor(scope: Construct, id: string, props?: props) {
       super(scope, id, props);
       new ec2.Vpc(this, 'dev-vpc', {
           cidr: "10.0.0.0/16",
           vpcName: "dev-vpc"
       })
   }
}

Stack ecs.tf

export interface Istack-ecs extends StackProps {
   vpc: ec2.Vpc;
}


export class stack-ecs extends Construct {
   constructor(scope: Construct, id: string, props: Istack-ecs) {
       super(scope, id);
       new ecs.Cluster(this, "fargate", {
           vpc: props.vpc,
           clusterName: "test",
           enableFargateCapacityProviders: true
       });
   }
}

Here is the stacks.ts file

const app = new cdk.App();
const vpc = new stack-vpc(app, "Dev_VPC_CF", props);
const ecs = new stack-ecs(app, "Dev_ECS_CF", props);

The vpc is creating without any problem, however once I'm putting an ecs to my stack the cdk gives me the error:

[Error at /stack-ecs] Could not find any VPCs matching {"account":"__--__","region":"eu-central-1",
"filter":{"tag:Name":"stack-vpc","isDefault":"false"},
"returnAsymmetricSubnets":true,
"lookupRoleArn":"arn:aws:iam::__--__:role/cdk-hnb659fds-lookup-role-__--__-eu-central-1"}

cdk.context.json is retrieving correct values from my aws credentials, so I can't blame the authentification, so my problem I need to somehow wait until one stack will give me an output so I can use it in my other stacks

CodePudding user response:

As @fedonev said

The fromLookup methods are for referencing existing deployed resources.

So instead you can do something like this:

  1. Your vpc should export ec2.Vpc
  2. Construct won't work in your case, you need to extend from cdk.Stack
  3. Since your ecs interface Istack-ecs already expecting a vpc
const app = new cdk.App();
const your_vpc = new stack-vpc(app, "Dev_VPC_CF", props);
const your_ecs = new stack-ecs(app, "Dev_ECS_CF", {
    vpc: your_vpc.vpc // As your props expects one single value
});
your_ecs.addDependency(your_vpc); // Guarantees That your `vpc` will be created firstly
app.synth();

CodePudding user response:

The fromLookup methods are for referencing existing deployed resources. This is not what you want. Instead, use Typescript language features to pass constructs within and between stacks. As @gshpychka explains in the comments, there are several CDK patterns:

For a simple app, a single stack with a Vpc and Ecs resources is a good approach. The cdk-examples repo has several such ECS examples with a Vpc:

export class MySingleStack extends Stack {
  constructor(scope: Construct, id: string, props?: props) {
    super(scope, id, props);
    const vpc = new ec2.Vpc(this, 'dev-vpc', {
      cidr: '10.0.0.0/16',
      vpcName: 'dev-vpc',
    });

    const cluster = new ecs.Cluster(this, 'Ec2Cluster', { vpc });
  }
}

Alternatively, pass a Vpc reference between stacks in the same app. VpcStack exports a Vpc. EcsStack's props should extend cdk.StackProps, adding a vpc: ec2.Vpc attribute. The vpc is exported from VpcStack and passed to EcsStack as a prop:

// vpc.ts
export class VpcStack extends Stack {
  readonly vpc: ec2.Vpc;

  constructor(scope: Construct, id: string, props?: props) {
    super(scope, id, props);
    this.vpc = new ec2.Vpc(this, 'dev-vpc', {
      cidr: '10.0.0.0/16',
      vpcName: 'dev-vpc',
    });
  }
}


// stacks.ts
const app = new cdk.App();
const { vpc } = new VpcStack(app, 'Dev_VPC_CF', props);
new EcsStack(app, 'Dev_ECS_CF', { ...props, vpc });
  • Related