Skip to content

TargetGroupAttachment

Source: src/AWS/ELBv2/TargetGroupAttachment.ts

Registers a single target (instance, IP address, Lambda function, or ALB) with an ELBv2 target group. ECS services register their own tasks, so this resource matters for Lambda-behind-ALB, EC2 instances, and static IPs.

For lambda targets, the Lambda function’s resource policy must allow elasticloadbalancing.amazonaws.com to invoke it, scoped to the target group ARN — create a Permission first. The provider retries the registration briefly while that permission propagates.

Lambda function target

const tg = yield* TargetGroup("fn", { targetType: "lambda" });
yield* Lambda.Permission("AlbInvoke", {
action: "lambda:InvokeFunction",
functionName: fn.functionArn.as<string>(),
principal: "elasticloadbalancing.amazonaws.com",
sourceArn: tg.targetGroupArn.as<string>(),
});
yield* TargetGroupAttachment("fn-target", {
targetGroupArn: tg.targetGroupArn,
targetId: fn.functionArn.as<string>(),
});

IP address target

yield* TargetGroupAttachment("ip-target", {
targetGroupArn: tg.targetGroupArn,
targetId: "10.0.1.15",
port: 8080,
});

EC2 instance target

yield* TargetGroupAttachment("instance-target", {
targetGroupArn: tg.targetGroupArn,
targetId: instance.instanceId,
port: 80,
});