From 12b6d89503fafdb645c5721d3b07d301fcd72521 Mon Sep 17 00:00:00 2001 From: David Horsman <56004724+horsmand@users.noreply.github.com> Date: Mon, 8 Feb 2021 17:52:26 -0600 Subject: [PATCH] feat(core): add vpcSubnets prop to HealthMonitor (#310) Fixes #305 --- .../aws-rfdk/lib/core/lib/health-monitor.ts | 8 +++++ .../lib/core/lib/load-balancer-manager.ts | 1 + .../lib/core/test/health-monitor.test.ts | 34 +++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/packages/aws-rfdk/lib/core/lib/health-monitor.ts b/packages/aws-rfdk/lib/core/lib/health-monitor.ts index 4e89d5476..96c2a9ae2 100644 --- a/packages/aws-rfdk/lib/core/lib/health-monitor.ts +++ b/packages/aws-rfdk/lib/core/lib/health-monitor.ts @@ -16,6 +16,7 @@ import { IConnectable, IVpc, Port, + SubnetSelection, } from '@aws-cdk/aws-ec2'; import { ApplicationLoadBalancer, @@ -202,6 +203,13 @@ export interface HealthMonitorProps { * @see https://docs.aws.amazon.com/elasticloadbalancing/latest/application/application-load-balancers.html#deletion-protection */ readonly deletionProtection?: boolean; + + /** + * Any load balancers that get created by calls to registerFleet() will be created in these subnets. + * + * @default: The VPC default strategy + */ + readonly vpcSubnets?: SubnetSelection; } /** diff --git a/packages/aws-rfdk/lib/core/lib/load-balancer-manager.ts b/packages/aws-rfdk/lib/core/lib/load-balancer-manager.ts index 16e105836..22c577639 100644 --- a/packages/aws-rfdk/lib/core/lib/load-balancer-manager.ts +++ b/packages/aws-rfdk/lib/core/lib/load-balancer-manager.ts @@ -198,6 +198,7 @@ export class LoadBalancerFactory { const loadBalancer = new ApplicationLoadBalancer(scope, `ALB_${loadBalancerindex}`, { vpc: this.vpc, internetFacing: false, + vpcSubnets: healthMonitorProps.vpcSubnets, deletionProtection: healthMonitorProps.deletionProtection ?? true, }); // Enabling dropping of invalid HTTP header fields on the load balancer to prevent http smuggling attacks. diff --git a/packages/aws-rfdk/lib/core/test/health-monitor.test.ts b/packages/aws-rfdk/lib/core/test/health-monitor.test.ts index 113c8919c..eaef54476 100644 --- a/packages/aws-rfdk/lib/core/test/health-monitor.test.ts +++ b/packages/aws-rfdk/lib/core/test/health-monitor.test.ts @@ -13,6 +13,8 @@ import { haveResourceLike, not, ABSENT, + notMatching, + stringLike, } from '@aws-cdk/assert'; import { AutoScalingGroup, @@ -30,6 +32,7 @@ import { InstanceSize, InstanceType, IVpc, + SubnetType, Vpc, } from '@aws-cdk/aws-ec2'; import {IApplicationLoadBalancerTarget} from '@aws-cdk/aws-elasticloadbalancingv2'; @@ -532,6 +535,37 @@ describe('HealthMonitor', () => { })); }); + test('specifying a subnet', () => { + // WHEN + healthMonitor = new HealthMonitor(hmStack, 'healthMonitor2', { + vpc, + vpcSubnets: { + subnetType: SubnetType.PUBLIC, + }, + }); + + const fleet = new TestMonitorableFleet(wfStack, 'workerFleet', { + vpc, + }); + healthMonitor.registerFleet(fleet, {}); + + // THEN + // Make sure it has the public subnets + expectCDK(hmStack).to(haveResourceLike('AWS::ElasticLoadBalancingV2::LoadBalancer', { + Subnets: [ + {'Fn::ImportValue': stringLike('*PublicSubnet*')}, + {'Fn::ImportValue': stringLike('*PublicSubnet*')}, + ], + })); + // Make sure the private subnets aren't present + expectCDK(hmStack).to(haveResourceLike('AWS::ElasticLoadBalancingV2::LoadBalancer', { + Subnets: [ + {'Fn::ImportValue': notMatching(stringLike('*PrivateSubnet*'))}, + {'Fn::ImportValue': notMatching(stringLike('*PrivateSubnet*'))}, + ], + })); + }); + describe('tagging', () => { testConstructTags({ constructName: 'HealthMonitor',