0
votes

We have ECS farget service stack where we want to export the serviceName like:

In stack1:
     new cdk.CfnOutput(this, 'EcsServiceExportName', {
            value: this.service.serviceName,
            exportName: "EcsExportName"
     });

this.service.serviceName is a physical name created by CDK.

we want to import this service name in another stack (stack2) like:

Fn.importValue(ECS_PROD_SERVICE_EXPORT_NAME)

Our pipeline deploys stack2 then stack1.

How to deploy this code change in single deployment?

Is there any other way to export name and import in another stack?

Does CDK will change physical name? if it's changing the physical name we will face issue while deployment of Stack2 as Stack1 is using "EcsExportName".

[NOTE: We always update our service]

1
Are both stacks in single cdk project or multiple cdk projects? - Balu Vyamajala

1 Answers

0
votes

When a single CDK project has multiple stacks and we need to reference resources across stacks,

Instead of manually exporting and importing in another stack, we can directly reference the object , like documented here

const stack1 = new StackThatProvidesABucket(app, 'Stack1' , { env: prod });

// stack2 will take a property { bucket: IBucket }
const stack2 = new StackThatExpectsABucket(app, 'Stack2', {
  bucket: stack1.bucket, 
  env: prod
});

cdk deploy will automatically sequence the stacks based on these references and will execute them in necessary sequence.

If we manually export and import with CfnOutput, we need to manually execute stacks separately as cdk deploy Stack1 and cdk deploy Stack2