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
See also full API documentation.
The main idea behind InstanceToAmi CDK construct:
- 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.
- The assumption is that the Instance is a part of some Auto Scaling Group and
is created using a Launch Template.
- 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.
- 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.)
- 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(...);
const myASG = new AutoScalingGroup(...);
...
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: