npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@clickup/instance-to-ami-cdk

v2.10.295

Published

A CDK construct to create an AMI from a healthy Instance time to time and update the Launch Template with it

Downloads

15

Readme

instance-to-ami-cdk: A CDK construct to create an AMI from a healthy Instance time to time and update the Launch Template with it

CI run

See also full API documentation.

The main idea behind InstanceToAmi CDK construct:

  1. It exposes an AWS Step Functions State Machine which, when run, takes a Snapshot of a running Instance's root EBS volume and creates an AMI Image from it. It's implied that you set up some automation on the instances which runs that State Machine time to time via AWS CLI.
  2. The assumption is that the Instance is a part of some Auto Scaling Group and is created using a Launch Template.
  3. Once the Image is created, the corresponding Launch Template is updated with it, and also, the Auto Scaling Group is updated to point to that new Launch Template version. So next time a new Instance is launched by that Auto Scaling Group, it will use the new Image.
  4. The tool keeps 2 previously created Snapshots/Images for backup purpose and removes the rest, and also removes all Launch Template versions which point to non-existing (removed) Images. It also sets up "fast snapshot restore" flag for the recent Snapshot, so the new Instances are created from the Image way faster. (And it resets this flag on backup Snapshots to save on costs.)
  5. The main feature: this is all done in a CDK drift-free manner. I.e. if you happen to update some Auto Scaling Group or Launch Template properties in CDK which also uses InstanceToAmi, then they will still refer to the Image assigned by the tool. But, if you change UserData, it will cause the Instances to be replaced as usual, and in that case, the Image in the Launch Template will be reset to the "main" one (all as expected).

Usage example in CDK (myASG of type AutoScalingGroup/CfnAutoScalingGroup and myLT of type LaunchTemplate/CfnLaunchTemplate should be created earlier in your CDK code):

class MyStack extends Stack {
  constructor(...) {
    ...
    const myLT = new LaunchTemplate(...); // or CfnLaunchTemplate
    const myASG = new AutoScalingGroup(...); // or CfnAutoScalingGroup
    ...
    new InstanceToAmi(this, "InstanceToAmi", {
      name: "instance-to-ami",
      autoScalingGroups: [{
        autoScalingGroup: myASG,
        launchTemplate: myLT,
      }],
      addToRoles: [myInstanceRole]
    });
  }
}

To execute that state machine on a healthy Instance:

aws lambda invoke \
  --function-name 'instance-to-ami' \
  --payload '{"instanceId":"i-1234567890","deps":"my-deps","minIntervalSec":3600}' \
  /dev/stdout | jq -s '.[0]'

Example of the result:

Or, if it's running already (concurrent runs protection):

You may optionally pass deps parameter which describes some post-boot downloadable state of the Instance (e.g. the list of Docker image ids downloaded by that Instance after it's booted). If the value of deps is seen the 1st time within the succeeded Executions, then the Lambda will not try to throttle the runs (i.e. "ran-recently-so-skipped" and "already-running" states will not happen, it will always start the Snapshot creation).

Generating a Snapshot and an Image typically takes ~10 minutes (it happens in background after the Lambda returns in ~5 seconds). In the end, the Launch Template and the Auto Scaling Group of the Instance you provided will be updated.

Other Tools

The library also exposes several helper functions useful in cloud-init scripts: