Skip to content

Commit f11bece

Browse files
hoegertnmergify[bot]
authored andcommitted
feat(elasticloadbalancingv2): add Instance target (#4187)
1 parent 3cca03d commit f11bece

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

packages/@aws-cdk/aws-elasticloadbalancingv2-targets/lib/instance-target.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import ec2 = require('@aws-cdk/aws-ec2');
12
import elbv2 = require('@aws-cdk/aws-elasticloadbalancingv2');
23

34
/**
@@ -43,3 +44,15 @@ export class InstanceIdTarget implements elbv2.IApplicationLoadBalancerTarget, e
4344
};
4445
}
4546
}
47+
48+
export class InstanceTarget extends InstanceIdTarget {
49+
/**
50+
* Create a new Instance target
51+
*
52+
* @param instance Instance to register to
53+
* @param port Override the default port for the target group
54+
*/
55+
constructor(instance: ec2.Instance, port?: number) {
56+
super(instance.instanceId, port);
57+
}
58+
}

packages/@aws-cdk/aws-elasticloadbalancingv2-targets/test/instance-target.test.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import elbv2 = require('@aws-cdk/aws-elasticloadbalancingv2');
44
import { Stack } from '@aws-cdk/core';
55
import targets = require('../lib');
66

7-
test('Can create target groups with lambda targets', () => {
7+
test('Can create target groups with instance id target', () => {
88
// GIVEN
99
const stack = new Stack();
1010
const vpc = new ec2.Vpc(stack, 'Stack');
@@ -26,4 +26,34 @@ test('Can create target groups with lambda targets', () => {
2626
],
2727
TargetType: "instance",
2828
}));
29+
});
30+
31+
test('Can create target groups with instance target', () => {
32+
// GIVEN
33+
const stack = new Stack();
34+
const vpc = new ec2.Vpc(stack, 'Stack');
35+
const lb = new elbv2.ApplicationLoadBalancer(stack, 'LB', { vpc });
36+
const listener = lb.addListener('Listener', { port: 80 });
37+
38+
const instance = new ec2.Instance(stack, 'Instance', {
39+
vpc,
40+
machineImage: new ec2.AmazonLinuxImage(),
41+
instanceType: ec2.InstanceType.of(ec2.InstanceClass.T3, ec2.InstanceSize.LARGE),
42+
});
43+
44+
// WHEN
45+
listener.addTargets('Targets', {
46+
targets: [new targets.InstanceTarget(instance)],
47+
port: 80,
48+
});
49+
50+
// THEN
51+
expect(stack).to(haveResource('AWS::ElasticLoadBalancingV2::TargetGroup', {
52+
Port: 80,
53+
Protocol: "HTTP",
54+
Targets: [
55+
{ Id: {Ref: 'InstanceC1063A87'} }
56+
],
57+
TargetType: "instance",
58+
}));
2959
});

0 commit comments

Comments
 (0)