Skip to content

Commit

Permalink
fix(ssm): update ssm-context to prevent raising an error on missing p…
Browse files Browse the repository at this point in the history
…arameter (#31415)

Updates StringParameter.valueFromLookup with an optional "defaultValue" When specified this value will be used:
 - in place of the standard dummyValue
 - to signal that an Error should not be raised during synthesis if the parameter is not found in the account.

Test are updated to prove that this works

### Issue # (if applicable)

Resolves #7051 

There are some closed issues which also benefit from this change:
- #22064 
- #7259

### Reason for this change

We have a library which has a fixed set of SSM parameters on which it depends.  The values from those parameters are made available as attributes of a custom Stack.  We have many users in many different AWS accounts, and not all of the parameters are guaranteed to exist.  This is okay.  In general, teams would simply not use those values and be happy with that outcome.  Unfortunately, CDK crashes when you look up an SSM parameter that does not exist in the account.  This is unacceptable.

### Description of changes

To address the issue described above, I implemented an optional parameter on the `valueFromLookup` method: `defaultValue`.  The idea is that if this value is specified, and we fail to look up a parameter in the account, we will return this value and suppress the Error that is currently raised when a parameter is not found.

To implement that functionality, I added a field to the `GetContextValueOptions` interface which is used to flag that we're not going to raise the error.  Then, in `valueFromLookup`, I set that flag to `true` if the `dummyValue` is specified.  `valueFromLookup` then calls `ContextProvider.getValue` passing along those values.

`ContextProvider.getValue` is modified so that when it calls `stack.reportMissingContextKey` it passes a modified set of `props` which include the `defaultValue` and the `ignoreErrorOnMissingContext` flag.

These finally land in the `aws-cdk` context provider for `ssm-parameter`.  That code has been updated so that if the value is not found in SSM, and we're told to suppress the error, then we'll simply return the `defaultValue` that was passed in. 

### Description of how you validated changes


I added a unit tests which covers when the default value is set.  I also updated the original unit test as the `props` now contain some additional field.

I added an integration test which calls `valueFromLookup` with a `defaultValue` set and then confirms that no exception is raised and that `valueFromLookup` returned the `defaultValue`

**NOTE**
I considered that the changes made _might_ need to be a part of the `cloud-assembly-schema` but chose to work around that for now.  I'm open to incorporating them there if that's a more correct path.

**NOTE 2**
I'm unsure about how to update API documentation for this change.  This does alter the public API for `valueFromLookup` and the function doesn't appear to have a proper `TSDoc` header on it.  Please let me know if there's a proper way for me to update the documentation.


### Checklist
- [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
dsilbergleithcu-godaddy authored Sep 13, 2024
1 parent a907a7e commit ff02cca
Show file tree
Hide file tree
Showing 15 changed files with 32,700 additions and 7 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ff02cca

Please sign in to comment.