-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(ssm): allow disabling context caching in valueFromLookup #29372
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pull request linter has failed. See the aws-cdk-automation comment below for failure reasons. If you believe this pull request should receive an exemption, please comment and provide a justification.
A comment requesting an exemption should contain the text Exemption Request
. Additionally, if clarification is needed add Clarification Request
to a comment.
Exemption Request: I don't think an integration test is required here because there's no change to the synthesis. This is just a change relating to what gets saved in
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the PR!
A couple of comments, mostly from my own lack of understanding for this part of the code base
}, | ||
"disableContextCaching": { | ||
"description": "Whether to disable persisting this to the context file.", | ||
"default": false, | ||
"type": "boolean" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious about the schema changes, did you add these manually?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not manual, no. If I remember correctly this was the result of running yarn update-schema
which I think was required for getting tests to pass.
const config = await new Configuration({ readUserContext: false }).load(); | ||
|
||
// THEN | ||
expect(value).toEqual('dummy-value-for-my-param-name'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Confused by why this is true, did you set value to dummy-value-for-my-param-name
somewhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See here:
aws-cdk/packages/aws-cdk-lib/aws-ssm/README.md
Lines 61 to 65 in ab94070
When using `valueFromLookup` an initial value of 'dummy-value-for-${parameterName}' | |
(`dummy-value-for-/My/Public/Parameter` in the above example) | |
is returned prior to the lookup being performed. This can lead to errors if you are using this | |
value in places that require a certain format. For example if you have stored the ARN for a SNS | |
topic in a SSM Parameter which you want to lookup and provide to `Topic.fromTopicArn()` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, if you look at the test before this one, a similar assertion is made (line 641)
expect(config.context.keys).toEqual(['ssm:account=12344:parameterName=my-param-name:region=us-east-1']); | ||
}); | ||
|
||
test('fromLookup will not persist value in context if requested', async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With disableContextCaching
would the value in cdk context
get overwritten when the parameter is resolved or is it the value never gets saved in cdk context
?
If its the former I'd prefer if this test showed that with disableContextCaching
the context will have a new value everytime it resolves the parameter
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there's a previous value in the context, valueFromLookup()
will use that one and not do another lookup.
This PR only allows disabling persisting to the context cache if the value needs to be looked up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a new test to confirm what I mentioned in my comment above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm open to renaming this to something like skipContextPersistence or some other name that would be more descriptive.
if (missingContext.disableContextCaching) { | ||
debug(`Skipping context caching for ${key}`); | ||
context.set(key + TRANSIENT_CONTEXT_KEY, { [TRANSIENT_CONTEXT_KEY]: true }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this property work for any context lookup? Like it was suggested in this comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be able to be used it in other context lookups, but this PR only implements it for SSM. I'm not sure what other context lookups would benefit from this, but with the underlaying changes in this PR, it would be easy to make it work elsewhere.
@@ -137,3 +137,18 @@ test('transient values arent saved to disk', async () => { | |||
// THEN | |||
expect(config2.context.get('some_key')).toEqual(undefined); | |||
}); | |||
|
|||
test('transient keys arent saved to disk', async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about the purpose of this test since it doesn't use disableContextCaching
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a copy of the test above it ("transient values arent saved to disk") but it checks that the specified keys/values don't get saved to the context, regardless of what the value is.
The pull request linter fails with the following errors:
PRs must pass status checks before we can provide a meaningful review. If you would like to request an exemption from the status checks or clarification on feedback, please leave a comment on this PR containing ✅ A exemption request has been requested. Please wait for a maintainer's review. |
AWS CodeBuild CI Report
Powered by github-codebuild-logs, available on the AWS Serverless Application Repository |
The pull request linter fails with the following errors:
PRs must pass status checks before we can provide a meaningful review. If you would like to request an exemption from the status checks or clarification on feedback, please leave a comment on this PR containing ✅ A exemption request has been requested. Please wait for a maintainer's review. |
These two PRs (#29372) and (#28766) are not doing the same thing, but there is definitely overlap. Given that fact, it changes the feedback I was going to give to both a bit. I absolutely see the value in both these changes but want to make sure we're getting the contract right across both. I know you've both been waiting for feedback for some time but I'm going to tax your patience a tad further so I can discuss with the team tomorrow. |
Just came across this feature while searching how to disable caching for ssm lookup. Thanks @andreialecu, can't wait to use it ! |
@andreialecu @aaythapa @TheRealAmazonKendra |
Issue # (if applicable)
Closes #12366
Reason for this change
There are cases when the ssm value should not be cached because it can accidentally roll back certain resources. See discussion in #12366
In my case, I'm using the AWS CLI to update a Cloudfront origin, but it's also part of a CDK stack. I want to avoid deploying the CDK stack and accidentally reverting the origin (which is also persisted to SSM, so the CDK stack can read it if needed).
Description of changes
I added an optional parameter to
valueFromLookup(this, 'param', { disableContextCaching: true })
It works by setting an adjacent context key of
key + TRANSIENT_CONTEXT_KEY
, piggybacking on similar behavior in the CDK.This seemed like the most straightforward implementation because
value
is usually a primitive value, and there was no way to attach this metadata.I didn't want to grow the scope of the change, but ideally,
value
should've been an object so that{ [TRANSIENT_CONTEXT_KEY]: true, value }
could've been used directly. I wasn't sure of that behavior's implications and potential breaking changes.Description of how you validated changes
Added tests.
Checklist
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license