Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(core): (Synthesizing an app programmatically does not automatically load cdk.context.json) #31499

Open
1 of 2 tasks
wmattei opened this issue Sep 19, 2024 · 1 comment
Open
1 of 2 tasks
Labels
@aws-cdk/core Related to core CDK functionality feature-request A feature should be added or improved. p2

Comments

@wmattei
Copy link

wmattei commented Sep 19, 2024

Describe the feature

If I run cdk synth it will by default add all variables from the cdk.context.json file to the app context.

However, if I run the synth programmatically (app.synth()) I would have to manually read the json file and pass the context variables to the App constructor.

Here is an example of what I mean:

import * as cdk from "aws-cdk-lib";
import { Vpc } from "aws-cdk-lib/aws-ec2";
import { Construct } from "constructs";

export class TestCdkStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const vpc = Vpc.fromLookup(this, "VPC", {
      // This is a dummy VPC ID. But it is in my context file.
      vpcId: "vpc-01234567890",
    });

    console.info(vpc.vpcId);
  }
}

const app = new cdk.App({});
new TestCdkStack(app, "TestCdkStack", {
  env: {
    account: "01234567890",
    region: "us-east-1",
  },
});

app.synth();

If I run cdk synth the logged value will be vpc-01234567890 (because that's what's in my context file)

However, if I just run ts-node ./stack.ts the logged value will be the default dummy vpc (vpc-12345)

Use Case

This would be useful in scenarios where I don't follow the default cdk structure and don't have a bin folder or a cdk.json file at all.

Instead I just want to execute a typescript file that contains the app and all resources I need.

Defining the whole stack in a single executable file is useful when I don't want the developers of my team to worry about any configuration. Just create the resources they want.

Proposed Solution

I suggest that the cdk.App constructor takes a new property "contextDir" which would automatically load the file and set each variable.

Here is a full example of my suggestion:

import * as cdk from "aws-cdk-lib";
import { Vpc } from "aws-cdk-lib/aws-ec2";
import { Construct } from "constructs";

export class TestCdkStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const vpc = Vpc.fromLookup(this, "VPC", {
      // This is a dummy VPC ID. But it is in my context file.
      vpcId: "vpc-01234567890",
    });

    console.info(vpc.vpcId);
  }
}

const app = new cdk.App({
+   contextDir: resolve(__dirname, "./cdk.context.json"), 
});

new TestCdkStack(app, "TestCdkStack", {
  env: {
    account: "01234567890",
    region: "us-east-1",
  },
});

app.synth();

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

CDK version used

2.146.0

Environment details (OS name and version, etc.)

Mac OS Sequoia

@wmattei wmattei added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Sep 19, 2024
@github-actions github-actions bot added the @aws-cdk/core Related to core CDK functionality label Sep 19, 2024
@wmattei wmattei changed the title (core): (Synthesizing and app programmatically does not automatically load cdk.context.json) (core): (Synthesizing an app programmatically does not automatically load cdk.context.json) Sep 19, 2024
@pahud
Copy link
Contributor

pahud commented Sep 19, 2024

Thank you. I will bring this up to the team's attention.

@pahud pahud added p3 p2 and removed p3 needs-triage This issue or PR still needs to be triaged. labels Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/core Related to core CDK functionality feature-request A feature should be added or improved. p2
Projects
None yet
Development

No branches or pull requests

2 participants