Skip to content
New issue

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

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

Already on GitHub? # to your account

cli: ability to pass context values through file #23911

Open
1 of 2 tasks
virajkanwade opened this issue Jan 30, 2023 · 9 comments
Open
1 of 2 tasks

cli: ability to pass context values through file #23911

virajkanwade opened this issue Jan 30, 2023 · 9 comments
Labels
effort/large Large work item – several weeks of effort feature-request A feature should be added or improved. p1 package/tools Related to AWS CDK Tools or CLI

Comments

@virajkanwade
Copy link

virajkanwade commented Jan 30, 2023

Describe the feature

Context variables allow passing parameters to the cdk command. But since they need to be specified individually, reruns become a challenge.
Keep track of what values have been used previously and pass them again.
If there are multiple environments it become even more challenging and tedious.

Use Case

Reruns require me to remember and pass the same variables everytime. Also specifying each variable everytime is tedious.

Proposed Solution

The ability to store these values in a json file and passing it to the cli would be great feature for reusability and maintenance.

cdk deploy --context-file cdk.dev.json
or
cdk deploy --context-file cdk.stage.json -c "vpcEnv=qa"

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.62.2

Environment details (OS name and version, etc.)

MacOS Ventura

@virajkanwade virajkanwade added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Jan 30, 2023
@github-actions github-actions bot added the package/tools Related to AWS CDK Tools or CLI label Jan 30, 2023
@virajkanwade
Copy link
Author

There have been similar tickets reported earlier but they are now closed. #14948 and #2371

@virajkanwade
Copy link
Author

I have submitted a PR #23912 implementing the feature, but since I have never worked on nodejs, need someone to address the tests.

@peterwoodworth
Copy link
Contributor

You can use both cdk.json and cdk.context.json to pass in and reference as context values - have you tried these methods out?

@peterwoodworth peterwoodworth added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. and removed needs-triage This issue or PR still needs to be triaged. labels Feb 7, 2023
@virajkanwade
Copy link
Author

@peterwoodworth the objective here is maintaining and passing environment specific cdk.context.json. Separate files like cdk.context.dev.json, cdk.context.stage.json, cdk.context.prod.json.

@peterwoodworth
Copy link
Contributor

I'm not sure how having multiple files here would be more helpful for organizing context. Context can contain objects, so you can specify context like so:

  "context": {
    "prod": {
      "context": "aws"
    },
    "dev": {
      "context": "cdk"
    }
  }

and reference it like so:

    const prod = this.node.tryGetContext('prod');
    console.log(prod.context);

    const dev = this.node.tryGetContext('dev');
    console.log(dev.context);

This can be generalized such that all you need to change is what's passed in to tryGetContext, and the context for your different environments can follow the same structure and be retrieved the same.

If you can provide more information about your use case to show why your proposal would be helpful (and why this approach wouldn't work), I'd love to hear it

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Feb 7, 2023
@virajkanwade
Copy link
Author

Hi @peterwoodworth Please see #2371 for history and comments #2371 (comment) #2371 (comment)

@virajkanwade
Copy link
Author

Putting everything in the same context file might not be preferable in every scenario. Say for eg prod. I would like to keep my prod configuration out of git. It could probably reside on S3, so only people who have access can download the file and refer the same. This is just an example and everything can have an alternative solution. Its just about what feels natural to work with.

@peterwoodworth peterwoodworth added p1 effort/large Large work item – several weeks of effort labels Feb 7, 2023
@mward-LT
Copy link

I spent some time searching for this feature and found this Issue. I'll chime in here with a more "basic" use-case, which is: "just because I like it that way." Development workflow feels cleaner and easier to navigate when each environment has its own unique file, as opposed to one giant file where I'm scrolling around. Things get extra long when you start adding in multiple regions and each region has certain unique values.

Terraform has .tfvar files, Serverless Framework has built-in syntax for reading in any number of customized YAML option files, etc. --- it's not an entirely valueless feature. With those tools, I follow a format like $ENV_$REGION, so like dev_ap-northeast-1.tfvars, etc., then those two values are used at runtime (i.e. in a CI pipeline) to evaluate which file to load. The same concept can be used with the single context object in CDK, but again, it just makes for a better developer user experience being able to compartmentalize each into their own file.

@karmabadger
Copy link

yeah this is long due. it's just like being able to have .env.dev and .env.prod. it's not even just for the context, but just to have an isolated prod cdk.json that won't be touched.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
effort/large Large work item – several weeks of effort feature-request A feature should be added or improved. p1 package/tools Related to AWS CDK Tools or CLI
Projects
None yet
4 participants