Skip to content

Service

Source: src/AWS/ECS/Service.ts

An ECS service for running long-lived tasks.

Service keeps a registered task definition running with awsvpc networking. Load balancing is explicit: pass user-supplied loadBalancers target groups, or set public: true to have Alchemy provision a public ALB + listener + target group as a convenience. Launch behavior is controlled via launchType (default FARGATE) or a capacityProviderStrategy.

Most configuration is updated in place via updateService (desiredCount, task definition, network, deployment config, placement, exec, load balancers, tags). Only truly-immutable aspects — serviceName, cluster, launchType↔capacityProviderStrategy switch, deploymentController type, schedulingStrategy, enableECSManagedTags, role — replace the service.

Internal Service

const service = yield* Service("WorkerService", {
cluster,
task: workerTask,
vpcId: vpc.vpcId,
subnets: [privateSubnet1.subnetId, privateSubnet2.subnetId],
securityGroups: [workerSecurityGroup.groupId],
desiredCount: 2,
});

Public HTTP Service (Alchemy-managed ALB)

const service = yield* Service("ApiService", {
cluster,
task: apiTask,
vpcId: vpc.vpcId,
subnets: [publicSubnet1.subnetId, publicSubnet2.subnetId],
securityGroups: [serviceSecurityGroup.groupId],
public: true,
});
const service = yield* Service("ApiService", {
cluster,
task: apiTask,
vpcId: vpc.vpcId,
subnets: [subnet1.subnetId, subnet2.subnetId],
loadBalancers: [
{
targetGroupArn,
containerName: apiTask.containerName,
containerPort: apiTask.port,
},
],
});
const service = yield* Service("WorkerService", {
cluster,
task: workerTask,
vpcId: vpc.vpcId,
subnets: [subnet.subnetId],
capacityProviderStrategy: [
{ capacityProvider: "FARGATE_SPOT", weight: 4 },
{ capacityProvider: "FARGATE", weight: 1, base: 1 },
],
placementStrategy: [{ type: "spread", field: "attribute:ecs.availability-zone" }],
});
const service = yield* Service("ApiService", {
cluster,
task: apiTask,
vpcId: vpc.vpcId,
subnets: [subnet1.subnetId, subnet2.subnetId],
desiredCount: 3,
enableExecuteCommand: true,
deploymentConfiguration: {
minimumHealthyPercent: 100,
maximumPercent: 200,
deploymentCircuitBreaker: { enable: true, rollback: true },
},
healthCheckGracePeriodSeconds: 30,
});