Skip to content

feat(core): support adding additional cache key to cdk context values #31586

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

Open
wants to merge 35 commits into
base: main
Choose a base branch
from

Conversation

luxaritas
Copy link
Contributor

Issue # (if applicable)

Closes #26982

Reason for this change

The ContextProvider mechanism and various "lookup" functions of a number of constructs support caching resolved values in the cdk.context.json. The context keys are constructed from the parameters of the lookup, which for lookup functions means whenever a resource with the same parameters is resolved, it is resolved as the same value across the entire app. However when a value may change over time, the user may wish to use the latest value when creating creating a new reference to the construct, effectively tying the cached context value to the scope - this patch enables this.

The primary use case is looking up an AMI parameter for a "stateful" EC2 instance. Currently if you specify cachedInContext, any future images created would use the same cached AMI, and updating the value would require updating all usages of the image across the entire app.

Description of changes

Adds an additionalCacheKey parameter/property to multiple areas of the CDK where lookups can be cached

Description of how you validated changes

Unit + integration tests

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 aws-cdk-automation requested a review from a team September 27, 2024 18:20
@github-actions github-actions bot added effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2 beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK labels Sep 27, 2024
@luxaritas
Copy link
Contributor Author

luxaritas commented Sep 27, 2024

This PR continues from #28766, which I let go stale stale while waiting for feedback from a maintainer

CC @TheRealAmazonKendra who was having some discussions about this

I still have to make some docs changes based on a couple rounds of previous reviews, but wanted to kick off a CI from merging in main and making related tweaks first

@aws-cdk-automation aws-cdk-automation added the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Sep 27, 2024
@aws-cdk-automation
Copy link
Collaborator

This PR cannot be merged because it has conflicts. Please resolve them. The PR will be considered stale and closed if it remains in an unmergeable state.

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 29a5a95
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

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

@samson-keung samson-keung added the skip-abstractions-board signal to automated workflow to skip adding to project board label Feb 19, 2025
@rix0rrr rix0rrr self-assigned this Apr 16, 2025
Copy link
Contributor

@rix0rrr rix0rrr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it, thanks!

I've got some small requests for changes but other than that this seems good to go.

@@ -369,22 +369,22 @@ export class SecurityGroup extends SecurityGroupBase {
*
* @deprecated Use `fromLookupById()` instead
*/
public static fromLookup(scope: Construct, id: string, securityGroupId: string) {
return this.fromLookupAttributes(scope, id, { securityGroupId });
public static fromLookup(scope: Construct, id: string, securityGroupId: string, additionalCacheKey?: string) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer not to have optional parameters as positionals, because this ultimately leads to callFunction(3, undefined, undefined, true, undefined, false) 😉.

If you want to add it here, create an Options struct for it. But more pragmatically: it's already available in the fromLookupAttributes method, which is good enough. No need to add it here as well then.

}

/**
* Look up a security group by id.
*/
public static fromLookupById(scope: Construct, id: string, securityGroupId: string) {
return this.fromLookupAttributes(scope, id, { securityGroupId });
public static fromLookupById(scope: Construct, id: string, securityGroupId: string, additionalCacheKey?: string) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same argument here.

}

/**
* Look up a security group by name.
*/
public static fromLookupByName(scope: Construct, id: string, securityGroupName: string, vpc: IVpc) {
return this.fromLookupAttributes(scope, id, { securityGroupName, vpc });
public static fromLookupByName(scope: Construct, id: string, securityGroupName: string, vpc: IVpc, additionalCacheKey?: string) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same argument hre.

@@ -152,13 +160,14 @@ export class EcsOptimizedAmi implements ec2.IMachineImage {
+ (this.windowsVersion ? 'image_id' : 'recommended/image_id');

this.cachedInContext = props?.cachedInContext ?? false;
this.additionalCacheKey = props?.additionalCacheKey;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably add some validation to say that additionalCacheKey only makes sense if cachedInContext==true ?

@@ -280,14 +297,15 @@ export class EcsOptimizedImage implements ec2.IMachineImage {
+ (this.hwType === AmiHardwareType.NEURON ? 'inf/' : '')
+ (this.windowsVersion ? 'image_id' : 'recommended/image_id');

this.cachedInContext = props?.cachedInContext ?? false;
this.cachedInContext = props.cachedInContext ?? false;
this.additionalCacheKey = props.additionalCacheKey;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably add some validation to say that additionalCacheKey only makes sense if cachedInContext==true ?

@@ -395,13 +422,14 @@ export class BottleRocketImage implements ec2.IMachineImage {
this.amiParameterName = `/aws/service/bottlerocket/${this.variant}/${this.architecture}/latest/image_id`;

this.cachedInContext = props.cachedInContext ?? false;
this.additionalCacheKey = props.additionalCacheKey;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably add some validation to say that additionalCacheKey only makes sense if cachedInContext==true ?

@@ -579,12 +579,16 @@ export class StringParameter extends ParameterBase implements IStringParameter {
* and the ContextProvider will be told NOT to raise an error on synthesis
* if the SSM Parameter is not found in the account at synth time.
*/
public static valueFromLookup(scope: Construct, parameterName: string, defaultValue?: string): string {

public static valueFromLookup(scope: Construct, parameterName: string, defaultValue?: string, additionalCacheKey?: string): string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defaultValue is borderline (should probably have been in an options struct already), but I'd really like the additionalCacheKey to be part of an options struct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2 pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. skip-abstractions-board signal to automated workflow to skip adding to project board
Projects
None yet
Development

Successfully merging this pull request may close these issues.

(aws-ec2): Support scope-local cdk context caching of images
4 participants