Skip to content

AutoScalingGroup

Source: src/AWS/AutoScaling/AutoScalingGroup.ts

An EC2 Auto Scaling Group that manages a fleet of instances from a launch template and can register that fleet with one or more load balancer target groups.

Pair with ScalingPolicy for target-tracking scaling, ScheduledAction for time-based capacity changes, and consumeLifecycleActions to run a Lambda handler while instances pause during launch/terminate transitions.

Fleet from a Launch Template

import { AutoScalingGroup, LaunchTemplate } from "alchemy/AWS/AutoScaling";
import { Subnet, Vpc } from "alchemy/AWS/EC2";
const vpc = yield* Vpc("Vpc", { cidrBlock: "10.0.0.0/16" });
const subnet = yield* Subnet("Subnet", {
vpcId: vpc.vpcId,
cidrBlock: "10.0.1.0/24",
});
const template = yield* LaunchTemplate("Template", {
imageId: "ami-0abcdef1234567890",
instanceType: "t3.micro",
});
const group = yield* AutoScalingGroup("Fleet", {
launchTemplate: template,
subnetIds: [subnet.subnetId],
minSize: 1,
maxSize: 3,
});

Reference an existing Launch Template by name

const group = yield* AutoScalingGroup("Fleet", {
launchTemplate: { launchTemplateName: "my-template", version: 2 },
subnetIds: [subnet.subnetId],
minSize: 0,
maxSize: 0,
desiredCapacity: 0,
});
const group = yield* AutoScalingGroup("WebFleet", {
launchTemplate: template,
subnetIds: [subnetA.subnetId, subnetB.subnetId],
minSize: 2,
maxSize: 6,
targetGroupArns: [targetGroup.targetGroupArn],
// healthCheckType defaults to "ELB" when target groups are attached
healthCheckGracePeriod: "5 minutes",
});
import { ScalingPolicy } from "alchemy/AWS/AutoScaling";
yield* ScalingPolicy("CpuPolicy", {
autoScalingGroup: group,
predefinedMetricType: "ASGAverageCPUUtilization",
targetValue: 60,
});