aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.55k stars 3.87k forks source link

fix(ssm): update ssm-context to prevent raising an error on missing parameter #31415

Closed dsilbergleithcu-godaddy closed 2 weeks ago

dsilbergleithcu-godaddy commented 2 weeks ago

Updates StringParameter.valueFromLookup with an optional "defaultValue" When specified this value will be used:

Test are updated to prove that this works

Issue # (if applicable)

Resolves #7051

There are some closed issues which also benefit from this change:

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


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

aws-cdk-automation commented 2 weeks ago

:arrow_right: PR build request submitted to test-main-pipeline :arrow_left:

A maintainer must now check the pipeline and add the pr-linter/cli-integ-tested label once the pipeline succeeds.

kaizencc commented 2 weeks ago

sending thru our test pipeline will add the necessary labels + approve when its done

mergify[bot] commented 2 weeks ago

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

aws-cdk-automation commented 2 weeks ago

AWS CodeBuild CI Report

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

mergify[bot] commented 2 weeks ago

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

github-actions[bot] commented 2 weeks ago

Comments on closed issues and PRs are hard for our team to see. If you need help, please open a new issue that references this one.