Skip to content
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

(aws-ec2): Support scope-local cdk context caching of images #26982

Open
1 of 2 tasks
luxaritas opened this issue Sep 1, 2023 · 1 comment
Open
1 of 2 tasks

(aws-ec2): Support scope-local cdk context caching of images #26982

luxaritas opened this issue Sep 1, 2023 · 1 comment
Labels
@aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2

Comments

@luxaritas
Copy link
Contributor

luxaritas commented Sep 1, 2023

Describe the feature

Many of the MachineImage constructs support the cacheInContext parameter to cache the AMI ID in the cdk.context.json. A limitation of this is that then any usage of the image across the entire app will use the same AMI, even though it may be desirable to use the latest image at the time of resource creation.

I would like to be able to specify the CDK context key to be tied to the scope the MachineImage is used in.

Use Case

I have a number of EC2 instances that I treat as persistent/stateful, namely which host third-party software which expect to be run completely self-contained in a Docker container which maintains its own volumes for databases, file uploads, etc. in addition to maintaining its own update lifecycle through update scripts. Whenever I deploy one of these new stateful instances in my CDK app, I want it to deploy with the latest Amazon Linux API, without the instance being recreated on future deployments.

Proposed Solution

I currently use the following workaround:

class CustomImage implements IMachineImage {
  getImage(scope: Construct): MachineImageConfig {
    const parameterName = AmazonLinuxImage.ssmParameterName({
      generation: AmazonLinuxGeneration.AMAZON_LINUX_2,
      edition: AmazonLinuxEdition.STANDARD,
    });
    const imageId = ContextProvider.getValue(scope, {
      provider: ContextProviderOpt.SSM_PARAMETER_PROVIDER,
      props: { parameterName, scope: scope.node.path },
      dummyValue: `dummy-value-for-${scope.node.path}-${parameterName}`,
    }).value;
    return {
      imageId,
      osType: OperatingSystemType.LINUX,
      userData: UserData.forLinux(),
    }
  }
}

My anticipated solution would be to support an additional parameter to ssm.StringParameter.valueFromLookup called something like forScope which provides the scope as an extra prop to ContextProvider.getValue, and then MachineImage props would either allow cachedInContext to support multiple options (say "global" or "scope"), or a new property (eg cachedInContextForScope). This additional forScope option can be useful in general for other string parameters and other "cacheable lookup" routines as well.

Other Information

Relevant stale issue: #12355

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

CDK version used

2.93.0

Environment details (OS name and version, etc.)

N/A

@luxaritas luxaritas added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Sep 1, 2023
@github-actions github-actions bot added the @aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud label Sep 1, 2023
@khushail
Copy link
Contributor

khushail commented Sep 1, 2023

Hi @luxaritas , thanks for reaching out with this interesting and useful feature request. Marking this as P2 for now, however you are welcome to submit a PR for the requested addition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2
Projects
None yet
2 participants