Skip to content

Instantly share code, notes, and snippets.

@andreif
Created February 22, 2024 10:35
Show Gist options
  • Select an option

  • Save andreif/76ffc5a8aec70212cc7835a7d610546b to your computer and use it in GitHub Desktop.

Select an option

Save andreif/76ffc5a8aec70212cc7835a7d610546b to your computer and use it in GitHub Desktop.

Revisions

  1. andreif created this gist Feb 22, 2024.
    36 changes: 36 additions & 0 deletions aws-cdk-output.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    import { Construct } from 'constructs';
    import { CfnOutput, Stack } from 'aws-cdk-lib';
    import * as cdk from 'aws-cdk-lib';

    export function output(scope: Construct, name: string, value: string) {
    return new CfnOutput(scope, name, {
    value,
    exportName: `${Stack.of(scope).stackName}:${name}`,
    });
    }

    export function importOutput(output: cdk.CfnOutput) {
    return cdk.Fn.importValue(output.exportName as string);
    }


    export class MyStack extends Stack {
    outputMyValue;

    constructor(scope: Construct, id: string, props: MyStackProps) {
    super(scope, id, props);

    this.outputMyValue = output(this, 'MyValue', '123');
    }
    }


    export function construct(app: cdk.App, env: cdk.Environment, orgId: string) {
    const tags = {team: 'MyTeam'};

    const myStack = new MyStack(app, `my-${envName}`, {env, tags, envName})

    const myOtherStack = new MyOtherStack(app, `my-other-${envName}`, {
    myValue: importOutput(myStack.outputMyValue),
    });
    });