Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS CDK- Access resource from other stack

Suppose i have a resource, say stepfunction activity, in one stack. How can i access it's arn in other stack?

I found CfnConstruct suitables for exporting (https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_core.CfnOutput.html). As of now i have used CfnConstruct to export it:

this.initiateValidationActivityArn = new cdk.CfnOutput(this, 'InitiateValidationActivityArn', {
      value: igvsStateMachine.initiateValidationActivity.activityArn
    });

Now how can i access it in other file. I have tried this:

ecsService.initiateValidationActivityArn.value

But value is private to construct so can't use it.

like image 677
Vaibhav Tripathi Avatar asked Aug 27 '26 19:08

Vaibhav Tripathi


1 Answers

If you have the stacks in one deployable cdk-app, you can access the property value from the stack by making it accessible from outside / not-private. What I recommend doing is to keep it readonly so that it can't be re-initialized from outside the stack.

// file: ./lib/first-stack.ts

// import statements

export class FirstStack extends Stack {
  readonly vpc: IVpc;
  
  constructor(...) {
     // setup
     this.vpc = new Vpc(this, "VPC", {...});
  }
}

In the dependent stack, you can pass it via (custom) props.

// file: ./lib/second-stack.ts

export interface SecondStackProps extends StackProps {
   importedVpc: IVpc;
}

export class SecondStack extends Stack {
  constructor(scope: cdk.Construct, id: string, props: SecondStackProps) {
    super(scope, id, props);
    const importedVpc = props.importedVpc;

    // do sth. with your imported resource
  }
}

Why is that working you may ask...? It works because CDK generates the Resource IDs and during the synth phase it can put the tokens for the resources inside the generated templates.

This doesn't work, when you have separated cdk-apps / deployments of stacks, because CDK can't resolve the (existing) Resource ID Tokens during cdk-synth. With that, you need to have another approach:

// file: ./lib/first-stack-separated-deployment.ts

// import statements

export class FirstStackSeparatedDeployment extends Stack {
  cfnVpcId: CfnOutput;
  
  constructor(...) {
     // setup
     const vpc = new Vpc(this, "VPC", {...});
     this.cfnVpcId= new cdk.CfnOutput(this, "FirstStackCfnVpcId", {
      value: vpc.vpcId,
      exportName: "UniqueNameForYourVpcId"
    });
  }
}

In the other stack, which requires the already deployed resource, you do the following:

// file: ./lib/second-stack-separated-deployment.ts
export class SecondStackSeparatedDeployment extends Stack {
  constructor(...) {
    // setup
    const vpcId = Fn.importValue("UniqueNameForYourVpcId")
    const importedVpc = ec2.Vpc.fromVpcAttributes(this, "ImportedVpc", {
      vpcId: vpcId,
      availabilityZones: [this.region],
    })

    // proceed further...
  }
}

Basically, you take the exportName from the CfnOutput Construct as an identifier and import it via the core Fn.importValue. With that approach, the first stack already needs to be deployed.

like image 107
mchlfchr Avatar answered Aug 29 '26 19:08

mchlfchr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!