cloudformation-coverage-roadmap icon indicating copy to clipboard operation
cloudformation-coverage-roadmap copied to clipboard

SSM Dynamic Reference latest version

Open Ricapar opened this issue 7 years ago • 63 comments

1. AWS::CloudFormation::Stack-SSM Dynamic Referecnes

2. Scope of request

As documented here, CloudFormation supports resolving values out of SSM using the {{resolve:ssm:MyParamNameHere:1}} notation.

It is also documented that "You cannot currently specify that AWS CloudFormation use the latest version of a parameter."

I am requesting that we be able to support using the latest version of a parameter.

3. Expected behavior

This currently does not work:

{{resolve:ssm:MyParamNameHere:latest}}

Expected behavior: When something similar to the sample above is used within CloudFormation, it is able to resolve and use the latest value.

4. Suggest specific test cases

  • When :latest is used as the version, it retrieves and uses the latest value from the SSM Parameter.
  • If a Stack Update is done, CloudFormation re-checks the value and performs stack updates as necessary.

5. Helpful Links to speed up research and evaluation

AWS Documentation on Dynamic References: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/dynamic-references.html

6. Category

  • Management (CloudTrail, Config...) (CloudFormation)

Ricapar avatar Aug 01 '19 13:08 Ricapar

It seems also to be the case that the length validation of MasterUsername and MasterUserPassword is applied before the '{{resolve...}}' is resolved, so if your expression is longer than 41 (or 63) characters the secret is never looked up.

https://forums.aws.amazon.com/post!reply.jspa?threadID=299271

kastork avatar Aug 03 '19 18:08 kastork

I feel like leaving off the version entirely should be an option. That would also make it consistent with the syntax for dynamic secret resolution.

mikerochip avatar Sep 03 '19 02:09 mikerochip

AWS folks, do we know if this is going to get on the roadmap soon?

ngamradt-turner avatar Jan 27 '20 20:01 ngamradt-turner

This is a huge pain for my use case, and I think it's a decent one. In order to dynamically reference a parameter such as "<env-name>/params/timeout", I have to either construct the parameter name in bash or some other language and pass it in as a parameter to the template, or use these "dynamic" references which can be constructed using sub or join. However, by forcing us to hardcode the version number, I have to keep track of and update any references to the parameter manually. I strongly object to calling these "dynamic" references since they are actually much less dynamic since you have to specify the version, at least to the end user.

I'm actually having a hard time understanding the utility of specifying the version number. Wouldn't the above use case be the usual reason for dynamically constructing the parameter name? Why is this dynamic behavior so different than the non-dynamic version, which doesn't force a version number?

robot-apocalypse avatar Feb 10 '20 19:02 robot-apocalypse

I'm actually having a hard time understanding the utility of specifying the version number. Wouldn't the above use case be the usual reason for dynamically constructing the parameter name? Why is this dynamic behavior so different than the non-dynamic version, which doesn't force a version number?

It's most likely not an end user utility thing, but a cloudformation leaking it's implementation. My guess is that the template needs to resolve deterministically (for eg the changeset functionality), and there's not a straightforward way to resolve the latest parameter from a prior run to an actual version (without building a transform).

parsnips avatar Feb 17 '20 23:02 parsnips

I'm actually having a hard time understanding the utility of specifying the version number. Wouldn't the above use case be the usual reason for dynamically constructing the parameter name? Why is this dynamic behavior so different than the non-dynamic version, which doesn't force a version number?

It's most likely not an end user utility thing, but a cloudformation leaking it's implementation. My guess is that the template needs to resolve deterministically (for eg the changeset functionality), and there's not a straightforward way to resolve the latest parameter from a prior run to an actual version (without building a transform).

Yeah I'm pretty sure you hit the nail on the head here. Case in point: secrets DO let you leave off the version and use the latest, and they suffer from the exact problem you described.

I put in a feature request to force a resolve via a flag or capability or similar for secrets during change set creation: https://github.com/aws-cloudformation/aws-cloudformation-coverage-roadmap/issues/369

mikerochip avatar Feb 18 '20 00:02 mikerochip

I would love it if there was a way to use Latest. Following Issue.

Juberstine avatar Feb 27 '20 18:02 Juberstine

Great idea! This feature would make life much easier

chriscloud27 avatar Apr 03 '20 10:04 chriscloud27

Please implement this feature. We definitely need this for our CF Templates as we use SSM Parameters a lot.

kirnberger1980 avatar Apr 03 '20 10:04 kirnberger1980

Use of latest is often necessary. Great Idea!

goyertp avatar Apr 03 '20 10:04 goyertp

Folks, I am moaning already. Pls add the support of :latest ASAP

mykyta-kharchenko avatar Apr 11 '20 13:04 mykyta-kharchenko

Best enhancement for our systems.

joaquin386 avatar Apr 21 '20 17:04 joaquin386

I'm going to go against the grain and say that while I want referencing parameters to be easier, I don't want :latest, at least until there are some larger changes in CloudFormation. As pointed out by @mikerochip, you could get the parameter resolved when you deployed the template, but there's no way to subsequently update it, because your template isn't changing. And of course, your template is not longer deterministic, which is less than ideal. An argument could be made that CloudFormation could watch that parameter for changes and redeploy automatically, but I think that's a recipe for surprises, and I hate surprises in my operations :-)

So what I want is some way of starting with the name of a parameter, and along the way the latest version gets baked into the deployment. So maybe what I'd rather have is some way of having a map of the parameter->version, and be able to re-resolve that mapping to perform an update. So maybe it's like, in my template I reference a parameter by name without a value, and when I'm using CreateChangeset, it snaps the parameter->version mapping using the latest values, or I can override the versions if I want. So now, I'm still fully specifying the versions of the parameters, but because it's outside my template proper, it's less gross in my source control.

benkehoe avatar Apr 21 '20 23:04 benkehoe

A good example of why this is so important:

  • AWS says to use nested stacks where possible
  • Nested stacks don't have visibility into their changes via changesets
  • Okay so I need to break my nested stacks into individual stacks because I need changeset visibility
  • So i'll use Exports for outputs to pass values to dependent stacks
  • But if that Export value changes, dependent stacks must first be deleted.
  • Okay so AWS recommends storing the values in the SSM parameter store.
  • But to reference that SSM Parameter in my dependent stack, I need to know the version of the parameter, which I don't know without passing the version into my dependent stack somehow? Which was the whole point of using the SSM Parameter store in the first place?
  • Okay well maybe I can use some wrapper orchestration to grab the Parameter Version when I create it, oh the AWS::SSM::Parameter resource doesn't even have a GetAtt for version.

This seems like a big gap in the logic here.

However if you take a look at Terraform, yet again we will see that Terraform is doing it the "right" way.

https://www.terraform.io/docs/providers/aws/d/ssm_parameter.html

Terraform imports the data from a SSM parameter without caring about the version.

I keep trying to work around the CloudFormation limitations because I really want it to work for me, but this might be the final nail in the coffin for me to switch to Terraform.

clayvan avatar Apr 29 '20 17:04 clayvan

Without a bulk update option for Parameter Store, a stack literally cannot watch parameters and mutate dynamically. What if I change my Fargate CPU size to something incompatible with my Memory size and it tries to update before I can modify the Memory size as well? That's a pandora's box. So we're stuck with manually-triggered updates and, if so, why not the manual event (i.e. CF change) that we're already used to.

So that brings us back to the original issue of a dynamic reference that requires a version. By definition, for n dynamic references, I either have to hardcode or pass (e.g. as parameters) n version numbers. How is that better than looking up and passing the values?

The only place I can even imagine a net-improvement is for secure parameters. My deployer no longer needs permission to decrypt them (just to see the latest version). On the surface this certainly seems more secure, but here's the catch... my deployer can also roll back secure values to any historical value they like (including known/breached values). That's a huge security hole and, ironically, one best addressed by forcing them to use the latest parameter!

EDIT: I now see that there's also a quirk in the way nested stacks are resolved during updates. If the dynamic reference is not part of the top-level stack, something must be changed in a nested stack to get them to update. A version number would be one way of prompting that change. Since we already have solutions for this problem, there's nothing special about a version number except that it would naturally cascade to the exact right scope.

ambsw-technology avatar May 21 '20 02:05 ambsw-technology

I've been following this thread, and it seems like the discussion has gone back and forth in terms of a) whether to provide {{resolve:ssm:MyParamNameHere:latest}} or not, b) how to provide it, and c) what is the side effect in nested stacks (using dynamic references in root, child, etc.). Would it be ok to ask one of you to refresh the requested requirements to cover all these issues, and then get consensus? Once we collectively do that, I can have someone internally give me (us) a sense of what we can do and when. Sound fair to you?

luiseduardocolon avatar May 29 '20 00:05 luiseduardocolon

I personally like "{{resolve:ssm:MyParamNameHere:latest}}". The implementation seems straight forward to me, where the "latest" version is calculated for each parameter. Then the template(s) are rewritten where "latest" is changed to the correct version number. Of course, maybe I'm missing some use cases?

mfriesen avatar May 29 '20 14:05 mfriesen

I can't claim any special authority here, but I've thought about it recently so I'll make an effort to outline some items (enumerated for easier reference).

1) Use Cases: Here are some CloudFormation use cases (with and without dynamic references) for consideration:

  • a) I have a local source-of-truth for parameter values
  • b) I have a local source-of-truth for version numbers
  • c) I use the API to load parameter values and pass them to the stack
  • d) I use the API to load latest version numbers and pass them to the stack

2) Problems: Problems with current implementation

  • a) If we must specify a version number, dynamic references are just as much work as passing raw value. We must hard code or provide every single version.
  • b) While the indirection of a version number prevents some data from passing through a local system, it's not clear that it significantly increases security. A deployer can simply roll back to a previous version. Template owners could increment minimum values, but we're almost back to hard coding them.

3) Proposed Solutions: All of these are semantic variants on a latest behavior:

  • a) Add a latest tag to SSM
  • b) Support an empty/absent version as latest. This is consistent with dynamic secret resolution.
  • c) Use a latest tag here and introduce a latest tag to dynamic secrets.
  • d) Support both latest and empty for both secrets and SSM

4) Additional Considerations:

  • a) Automatic Updates: CloudFormation template cannot respond in real time to changes in SSM parameters. Since the CLI (and UI) have no way to bulk update, this can result in inconsistent/incompatible parameter values.
  • b) Update on Stack Change: This is the timing/pattern for the existing parameters. It is an acceptable time for the change to occur. Inconsistent values are still possible e.g. due to a concurrent change to SSM during deploy, but the only way to avoid this is to keep a local source of truth of some kind (parameters or versions) and that introduces versioning issues.
  • c) Nested Stacks, Current Behavior: In this version, the latest tag is only supported on the top-level stack where it is always resolved. If you want changes to cascade, you pass these top level parameters to nested stacks. They update as they would have if you had changed an actual parameter value. Version-based parameters can be hard-coded into nested stacks since they cannot be changed without triggering a stack change anyway.
  • d) Nested Stacks, Cascade Update: In this version of the proposal, the entire tree of stacks must be walked every time to resolve dynamic references. This ensures updates are propagated. The parameter space is simplified because the latest tag does not need to be passed from the top-level to deeper stacks.

My Conclusion

Since 4b and 4c most closely mirror the current parameter behavior, they are an acceptable first step (and likely require little effort). I see the merits of 2b but believe that explicit is better so I would pay the cost of "two ways to do a thing" to go with 2d (or 2c). Other changes may be worth considering, but they aren't necessary to introduce latest and can be discussed/proposed as improvements that need to be justified by their own use cases.

ambsw-technology avatar May 29 '20 15:05 ambsw-technology

When I submitted my original post back in 2019, I was already aware of some of the issues that'd result of a concept of a :latest tag resolution, namely around the deterministic nature of how CloudFormation operates.

This is most obvious when you're using custom resources - if you don't change any of the parameters that are going to it, CloudFormation doesn't even look at the resource. And of course - why should it? No parameters have changed, so how could the resource possibly need any updating?

I'm taking a step back and thinking about why I and others are asking for this feature: We need a place to hold common variables across CloudFormation templates. A place to hold a source of truth so we don't have to enter in the same data into parameters every time.

I do know that Parameters are evaluated every time a stack is run, including when the parameters are references to Parameter Store.

For example, we commonly use this to grab the latest Amazon Linux AMI:

  LatestAmiId:
    Type: 'AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>'
    Default: '/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2'

If I launch a stack with that today, it finds today's current value. If I come back a month or two later, I can expect that parameter may resolve to something different and I'd get back a different AMI ID.

This part works great when there's one parameter you're sourcing - but becomes a lot more tricky when you have many more. You can quickly hit your maximum parameters, and you also end up just cluttering the interface to your template. Which parameters do I actually expect future developers to fill in? What should they leave alone?

Even moreso, if you're exposing CloudFormation through Service Catalog, then you're going to bombard the users with a list of ugly looking parameters that serve no purpose to them at best, and at worst will confuse a not-as-technical user.

So maybe what CloudFormation needs isn't integrated resolution of SSM Parameters - maybe what we need is a way to set variables and constants, whose values can be derived from SSM Parameters or just hard-coded into the template. There are a lot of cases where I don't want to repeat myself 100x in a template, but I also don't necessarily want that piece of information in a parameter.

I feel like something like the mock-up example below would solve the SSM resolution issue, as well as make life a lot easier for a lot of people who have tons of parameters on their stacks because that's the only way to not repeat a value everywhere in your code.

If the Variables section behaved the same way that the AWS::SSM::Parameter::Value<> CFN Parameter type worked then I would think it wouldn't introduce any new issues with deterministic behavior during template execution (at least, not any new ones!).

---
Parameters:

  ResourceDescription:
    Type: String

Variables:

  OwnerTag:
    Type: String
    Value: "John Smith"

  PrimaryVPC:
    Type: String
    ValueFrom: !Sub "arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/mycorp/vpc"

Resources:

  MyResource:
    Type: AWS::SomeService::SomeResource
    Properties:
      VpcId: !Ref PrimaryVPC
      Description: !Ref ResourceDescription
      Tags:
        - Key: Owner
          Value: !Ref OwnerTag

Ricapar avatar May 31 '20 18:05 Ricapar

I think this solution is great @Ricapar.

I've used maps as a ghetto workaround for the lack of ergonomic constants.

The only workaround I can think of right now for lack of variables is to have a stack purely composed of parameters (with SSM::Parameter types) and maps (for constants) which then exports all of those parameters and map values, then all the other stacks could import those values, which feels super gross. This variable design feels much better.

I think the expectation for the user is that (non-const) variables need to be resolved every time a change set is created even if nothing else about the stack changes. This way we can avoid a mass number of export/import - we could have variables be populated via dynamic ssm parameters or imports.

mikerochip avatar May 31 '20 19:05 mikerochip