Skip to content

Commit

Permalink
feat(elasticloadbalancingv2): add Instance target (#4187)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoegertn authored and mergify[bot] committed Sep 25, 2019
1 parent 3cca03d commit f11bece
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ec2 = require('@aws-cdk/aws-ec2');
import elbv2 = require('@aws-cdk/aws-elasticloadbalancingv2');

/**
Expand Down Expand Up @@ -43,3 +44,15 @@ export class InstanceIdTarget implements elbv2.IApplicationLoadBalancerTarget, e
};
}
}

export class InstanceTarget extends InstanceIdTarget {
/**
* Create a new Instance target
*
* @param instance Instance to register to
* @param port Override the default port for the target group
*/
constructor(instance: ec2.Instance, port?: number) {
super(instance.instanceId, port);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import elbv2 = require('@aws-cdk/aws-elasticloadbalancingv2');
import { Stack } from '@aws-cdk/core';
import targets = require('../lib');

test('Can create target groups with lambda targets', () => {
test('Can create target groups with instance id target', () => {
// GIVEN
const stack = new Stack();
const vpc = new ec2.Vpc(stack, 'Stack');
Expand All @@ -26,4 +26,34 @@ test('Can create target groups with lambda targets', () => {
],
TargetType: "instance",
}));
});

test('Can create target groups with instance target', () => {
// GIVEN
const stack = new Stack();
const vpc = new ec2.Vpc(stack, 'Stack');
const lb = new elbv2.ApplicationLoadBalancer(stack, 'LB', { vpc });
const listener = lb.addListener('Listener', { port: 80 });

const instance = new ec2.Instance(stack, 'Instance', {
vpc,
machineImage: new ec2.AmazonLinuxImage(),
instanceType: ec2.InstanceType.of(ec2.InstanceClass.T3, ec2.InstanceSize.LARGE),
});

// WHEN
listener.addTargets('Targets', {
targets: [new targets.InstanceTarget(instance)],
port: 80,
});

// THEN
expect(stack).to(haveResource('AWS::ElasticLoadBalancingV2::TargetGroup', {
Port: 80,
Protocol: "HTTP",
Targets: [
{ Id: {Ref: 'InstanceC1063A87'} }
],
TargetType: "instance",
}));
});

0 comments on commit f11bece

Please sign in to comment.