aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

Document how to use stack parameters

Open rclark opened this issue 8 years ago • 49 comments

Every example stack that I've seen so far in the documentation has no Parameters. I found the @aws-cdk/core documentation for the Parameter class itself, and got it to work in my stack (shows up in cdk synth output). Now, I don't know how to convey values for the parameters through cdk deploy.

❯ AWS_REGION=us-east-1 cdk deploy --GitSha abc
 ⏳  Starting deployment of stack hello-cdk...
 ❌  Deployment of stack hello-cdk failed: ValidationError: Parameters: [GitSha] must have values
Parameters: [GitSha] must have values

p.s. I also don't know where the hello-cdk name is coming from. I assume from the skeleton setup in cdk init?

rclark avatar Jun 22 '18 17:06 rclark

'hello-cdk' is the name that the Stack object gets constructed with.

Stack Parameters are currently not really in the path of how we're thinking about CDK apps (but admittedly, we're still looking for use cases). Instead, we encourage parameterizing the application and making the stacks as concrete as possible.

If you really have to use Stack Parameters, first of all please tell us more about your use case, and second of all, the workaround will be to synth your template to a file, then use AWS CLI or a different mechanism to upload it.

rix0rrr avatar Jun 22 '18 18:06 rix0rrr

I see -- I do think there's still some gap that documentation needs a better bridge. A background concept of a cloudformation template as a declarative document clashes with trying to understand the CDK code as an "executable" where parameters would be provided to the program. I'm certainly still wrapping my head around this.

Instead, we encourage parameterizing the application and making the stacks as concrete as possible.

const app = new App(process.argv);

From the example. So I could use cdk deploy --with 'other' --arguments and parse the .argv. Is that how you'd propose I keep config separate from code?

The bummer about this is that as values for stack parameters, cloudformation describe-stacks API calls tell you about how the template has been configured. In this approach, you'd have to build your own system to keep track of configurations that were sent via application parameters.

That or read process.argv in order to populate values for @aws-cdk/core.Parameter objects within the application? Would that work?

rclark avatar Jun 22 '18 18:06 rclark

Indeed, CloudFormation parameters are not the best way to convey degrees of freedom in CDK apps, since they are resolved only during deployment and therefore harder to reason about using “normal” code. This is why tactically we didn’t implement first class support for them yet in the toolkit. Having said that, I believe that if users wish to use them, understanding their limitations, it should be possible to pass in parameters in the toolkit when stacks are deployed.

Still, we don’t have good guidance for how to associate configuration to environments. In my mind the preferred mechanism would be to use per-environment context, which is a feature we have in our backlog and haven’t implemented yet.

The idea is as follows: when you define a stack, one of the props is called “env”. Today it allows you to explicitly specify “region” and “account”, but in the future it will simply be a string used as a key to a map within your “cdk.json” file. This per-environment map will be where you could define the environment (I.e account/region, but also using profiles, AWS Organizations, etc) and also associate context keys with values. Then, in your code, you’ll just call “construct.getContext(key)” to read these values when they are needed.

P.S. the context mechanism already exists, but at the moment is not associated with environment, so if you have multiple stacks you’ll need to organize the context keys to be able to distinguish between stacks.

I believe that this model, where config is source-controlled, and associated with a deployment environment, should fit the 12factor philosophy quite well.

Would love your thoughts on this approach.

eladb avatar Jun 26 '18 13:06 eladb

in the future it will simply be a string used as a key to a map within your “cdk.json” file.

So basically you isolate config that may vary between deploys in the cdk.json file, correct? The only trouble with that model is that I believe the CDK application itself requires this file to be present in order to work at all. If that's true, then this cdk.json file will be something that's committed to version control alongside the application itself, and to me that's a violation of code/config separation.

A litmus test for whether an app has all config correctly factored out of the code is whether the codebase could be made open source at any moment, without compromising any credentials.

That's what's great about CloudFormation parameters -- as you say, "they are resolved only during deployment". I agree that this makes them harder to think about when you're writing a TypeScript application -- you find yourself having to keep a mental map in your head of which variables are "build time" (those that are resolved when the TypeScript app runs) vs. "deploy time" (those resolved by CloudFormation). You have to keep considering whether you access the values through CloudFormation intrinsic functions or not.

There's talk in the documentation about SSM Parameter Store. I looked at this service briefly for storing CloudFormation parameter values, but ended up moving past it, primarily because it required all values to be in plain text, which is not an option for sensitive credentials. Still, I wonder if the CDK use of parameter store is intended to help address these config/code differentiation issues in some way?

Another concept might be to make use of AWS Secrets Manager. Instead of storing my configuration in a local cdk.json file, could I store it in AWS Secrets Manager, and reference the SecretId in my cdk.json file per-environment? Certainly I could pull this off manually by using the aws-sdk to look up the configuration, but I wonder if the use-case would be worth more firm support in the CDK?

Even at that point, I'd still like to be able to pass command-line parameters through cdk deploy into my application. In our workflows, when you're running a deploy to some environment is the moment where you may wish to inject some change to the environment's configuration. Our internal deployment CLI does this by prompting you for CloudFormation parameter values. I suppose I could also inject that prompting into my CDK application code...

There is clearly more than one way to get this done -- and its also clearly a confusing shift for someone like me with well-established CloudFormation-based workflows.

rclark avatar Jun 27 '18 16:06 rclark

@rclark I completely agree with your statement 👍

So basically you isolate config that may vary between deploys in the cdk.json file, correct? The only trouble with that model is that I believe the CDK application itself requires this file to be present in order to work at all. If that's true, then this cdk.json file will be something that's committed to version control alongside the application itself, and to me that's a violation of code/config separation.

I'm really interested to hear about how best practice evolves around passing deployment config to the CDK apps.

We currently inject them at deployment using our CI pipe to inject the secrets in the CF vars. I can't actually see a way to keep the app 12 factor compatible without passing the args.

JMBreitenbach avatar Sep 04 '18 19:09 JMBreitenbach

I would expect the passing of deployment params to work something like the following: image

JMBreitenbach avatar Sep 04 '18 19:09 JMBreitenbach

I understand that ideally parameters would be added as configuration for most constructs. Create a pipeline in CDK and pass in the github repo, owner, and token (cdk.Secret) as parameters. When writing a TS application I also think that's a pretty simple way to deal with parameters.

However, Cloudformation is ~7 years old at this point and so we've already been using it for many years with workflows built around passing parameters to an entire stack (as opposed to an individual resource). I just ran into this issue:

I have an existing stack. I included it with cdk.include. This stack is huge and everything is interdependent (can't be broken down into smaller stacks). I need a way to pass parameters to this stack. As far as I can tell there's absolutely no way to do this. My hope was to use CDK to deploy this old stack then start writing newer stacks around it using CDK properly. Since I cannot pass any parameters to the stack I have to support a new workflow (CDK) and a legacy workflow. It would really help with adoption if it supported a more generic (even if it's inferior) way of using existing stacks and parameters.

I absolutely love that CDK can setup a stack with a bucket and push my stack to S3 before deploy. I love the progress output and events from CDK. I like that I can pick and choose stacks to deploy or deploy them all. Hopefully we can come up with some way to support existing workflows better.

mneil avatar Nov 14 '18 01:11 mneil

Looking at the comment by @JMBreitenbach I just remembered that something along these lines was possible once. https://github.com/awslabs/aws-cdk/blame/aa76305132be01895d8b18f58085e8c9a7bab8a1/packages/@aws-cdk/cdk/lib/app.ts . I had an older version of CDK accepting input from argv. I don't think it would take in arbitrary stack parameters though. That would be a good spot to re-introduce this functionality. Or, perhaps, on the stack construct itself.

mneil avatar Nov 14 '18 01:11 mneil

We have a section in the docs about passing in data:

https://awslabs.github.io/aws-cdk/passing-in-data.html

Feel free to re-open this issue if the docs do not satisfy your needs.

Doug-AWS avatar Nov 19 '18 13:11 Doug-AWS

Doug I'm still curious if it's possible to pass in cloudformation parameters in the cli or cdk.json just for testing purposes. So I can run cdk deploy locally.

seawatts avatar Nov 22 '18 18:11 seawatts

Note that I've split the section up and moved it. I'm rebuilding the public docs now, so when I'm done I'll post a link to the new "How-Tos" section.

Doug-AWS avatar Feb 22 '19 21:02 Doug-AWS

See https://docs.aws.amazon.com/CDK/latest/guide/passing_secrets_manager.html.

Doug-AWS avatar Apr 24 '19 19:04 Doug-AWS

A common use case for passing parameters would be within service catalog, there is no other choice. If I want to write products in Service Catalog it is expected to provide parameters to cloudformation. Hopefully I make sense.

evildani avatar May 13 '19 13:05 evildani

@Doug-AWS Both those links are dead

joehillen avatar May 28 '19 20:05 joehillen

Why not providing a constructor overload such as public HelloStack(Construct parent, string id, IStackProps props, IDictionary<string, StackParameter> stackParams)? I think this would be really useful for those who prefer to cdk synth the stack and obtain a template with well defined parameters and branch the stack deployment process from there without using cdk deploy

akirsman avatar May 31 '19 17:05 akirsman

I would also like to see parameter support, so that AWS CDK can be used to generate CloudFormation templates for any purpose where the workflow is already based on parameters. E.g. the template is validated by a testing / approval process and parameters are then used to deploy it to multiple places.

kennu avatar Jun 03 '19 03:06 kennu

I apologize that this issue was closed. We don't have an objection for supporting parameters, but just haven't prioritized this work. As mentioned above, using CloudFormation parameters is generally an anti-pattern for CDK apps given "synth-time" resolution is more deterministic and allows you to reason about values in your code, but we understand that people who come from existing CloudFormation workflows may still want to leverage parameters.

We will gladly accept a PR to that end if someone is interested in picking this up, or eventually we'll get to adding this support.

The general approach that I would take is to simply allow passing --parameters switches to cdk deploy (either in command line or through cdk.json).

eladb avatar Jun 03 '19 06:06 eladb

Actually, I was able to add parameters to the template through this:

            var bucketNameParam = new CfnParameter(this, "bucketName", new CfnParameterProps() { Type = "String", Description = "bucket name" });
            SetParameterValue(bucketNameParam, this.Node.GetContext("bucketName").ToString());
            var bucket = new Bucket(this, "poc-bucket", new BucketProps() { BucketName = bucketNameParam.Value.ToString() });

This way I was able to "synth" a template and deploy from there without cdk deploy!

I guess this is supported usage, right? Thanks!

akirsman avatar Jun 03 '19 11:06 akirsman

Of course it is supported :-), and as I said, no objection also supporting deploying through the CDK CLI as well.

eladb avatar Jun 03 '19 11:06 eladb

Thanks @akirsman, it's good to know that is possible. I will keep this solution in mind for the future. Still kind of waiting for a 1.0 release before using CDK in customer projects..

kennu avatar Jun 03 '19 14:06 kennu