How to share resources across stacks in AWS CDK

When building a CDK App, there is a good chance you want to structurize your project and set up multiple stacks when creating the Infrastructure. Therefore it’s good to know how you can reference resources across stacks in AWS CDK.

For the example in this blog post we’re going to create two stacks:

  • SharedInfraStack, which contains the VPC resource
  • RdsStack which will import the VPC from the SharedInfraStack

Note: if you’re still a beginner with AWS CDK. Then I would first recommend you to read my article on What is the AWS CDK?.

To be able to share resources between stacks in AWS CDK we need to:

Create SharedInfraStack which provisions the VPC

In the example below I share the share infra stack which provisions the VPC resource including subnets and routing.

// file: lib/shared-infra-stack.ts
import * as cdk from '@aws-cdk/core';
import * as ec2 from '@aws-cdk/aws-ec2';

export class SharedInfraStack extends cdk.Stack {
  public readonly vpc: ec2.Vpc;
  constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    // assign a VPC to the class property SharedInfraStack
    this.vpc = new ec2.Vpc(this, 'TheVPC', {
      cidr: '',
      natGateways: 1,
      maxAzs: 3,
      subnetConfiguration: [
          cidrMask: 20,
          name: 'public',
          subnetType: ec2.SubnetType.PUBLIC,
          cidrMask: 20,
          name: 'application',
          subnetType: ec2.SubnetType.PRIVATE,
          cidrMask: 20,
          name: 'data',
          subnetType: ec2.SubnetType.ISOLATED,

If you generate the CloudFormation template by running cdk synth you’ll see that the following VPC resources are being exported.

# Generated CloudFormation template of the SharedInfraStack
      Ref: TheVPCdataSubnet1Subnet62F6C85A
      Name: SharedInfraStack:ExportsOutputRefTheVPCdataSubnet1Subnet62F6C85A8DFF3A46
      Ref: TheVPCdataSubnet2SubnetAE4EF5CA
      Name: SharedInfraStack:ExportsOutputRefTheVPCdataSubnet2SubnetAE4EF5CAD340846A
      Ref: TheVPC92636AB0
      Name: SharedInfraStack:ExportsOutputRefTheVPC92636AB00B2A4A70

Pass the props of the VPC to the RdsStack that we instantiate

In the bin folder where we instantiate the CDK app, we also declare the CDK stacks. Here we make sure to pass the props we just created from the VPC stack and pass them to the new RdsStack that we’re going to create.

// file: bin/index.ts
import * as cdk from '@aws-cdk/core';
import { SharedInfraStack } from '../lib/shared-infra-stack';
import { RdsStack } from '../lib/rds-stack';

const app = new cdk.App();

// created the SharedInfraStack with the VPC resource that we're going to share by making a variable
const infra = new SharedInfraStack(app, 'SharedInfraStack');

// pass the vpc resource from the SharedInfraStack to the RdsStack
new RdsStack(app, 'RdsStack', {
  vpc: infra.vpc,

Create the RdsStack and import the VPC as prop

Now we’ll create the RdsStack that provisions the RDS with the VPC resource we shared across stacks in the previous two steps.

// file: lib/rds-stack.ts
import * as cdk from '@aws-cdk/core';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as rds from '@aws-cdk/aws-rds';

// extend the props of the stack by adding the vpc type from the SharedInfraStack
export interface RDSStackProps extends cdk.StackProps {
  vpc: ec2.Vpc;

export class RdsStack extends cdk.Stack {
  readonly postgreSQLinstance: rds.DatabaseInstance;
  private vpc: ec2.Vpc;
  constructor(scope: cdk.Construct, id: string, props: RDSStackProps) {
    super(scope, id, props);

    // make the vpc variable accessible
    const vpc = props.vpc;

    const cluster = new rds.DatabaseCluster(this, 'Database', {
      engine: rds.DatabaseClusterEngine.auroraMysql({
        version: rds.AuroraMysqlEngineVersion.VER_2_08_1,
      credentials: rds.Credentials.fromGeneratedSecret('clusteradmin'), // Optional - will default to 'admin' username and generated password
      instanceProps: {
        // optional , defaults to t3.medium
        instanceType: ec2.InstanceType.of(
        vpcSubnets: {
          subnetType: ec2.SubnetType.ISOLATED,
        // select the vpc we imported to define the subnets for the RDS

This is what the end result looks like when we generate the CloudFormation template with cdk synth command:

  Type: AWS::RDS::DBSubnetGroup
    DBSubnetGroupDescription: Subnets for Database database
      - Fn::ImportValue: SharedInfraStack:ExportsOutputRefTheVPCdataSubnet1Subnet62F6C85A8DFF3A46
      - Fn::ImportValue: SharedInfraStack:ExportsOutputRefTheVPCdataSubnet2SubnetAE4EF5CAD340846A
    aws:cdk:path: RdsStack/Database/Subnets/Default
  Type: AWS::RDS::DBCluster
    Engine: aurora-mysql
    DBClusterParameterGroupName: default.aurora-mysql5.7
      Ref: DatabaseSubnets56F17B9A
    EngineVersion: 5.7.mysql_aurora.2.08.1
    MasterUsername: clusteradmin
        - ''
        - - '{{resolve:secretsmanager:'
          - Ref: RdsStackDatabaseSecretECD539873fdaad7efa858a3daf9490cf0a702aeb
          - :SecretString:password::}}
      - Fn::GetAtt:
          - DatabaseSecurityGroup5C91FDCB
          - GroupId
  UpdateReplacePolicy: Snapshot
  DeletionPolicy: Snapshot
    aws:cdk:path: RdsStack/Database/Resource

As you can see in the CloudFormation template we import the VPC value in the RdsStack that we’ve exported from the SharedInfraStack template.

TL;DR give me the code!

The code for this article is available on GitHub


You’ve created the following after reading this article:

Now you know how to structurize your project and instantiate resources in a base stack which can then be used in other stacks by passing its prop.

Danny Steenman

Is a Principal Cloud Consultant with a background in DevOps Engineering and thorough hands-on experience in architecting and building highly scalable distributed systems on AWS Cloud using Infrastructure as Code.

A prominent leader who is passionate about sharing AWS technical expertise by writing technical articles.