Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(aws-ecs-patterns, aws-elasticloadbalancingv2): Pass TargetGroup P… #14092

Merged
merged 2 commits into from
Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ICluster, LogDriver, PropagatedTagSource, Secret,
} from '@aws-cdk/aws-ecs';
import {
ApplicationListener, ApplicationLoadBalancer, ApplicationProtocol, ApplicationTargetGroup,
ApplicationListener, ApplicationLoadBalancer, ApplicationProtocol, ApplicationProtocolVersion, ApplicationTargetGroup,
IApplicationLoadBalancer, ListenerCertificate, ListenerAction, AddApplicationTargetsProps,
} from '@aws-cdk/aws-elasticloadbalancingv2';
import { IRole } from '@aws-cdk/aws-iam';
Expand Down Expand Up @@ -117,6 +117,13 @@ export interface ApplicationLoadBalancedServiceBaseProps {
*/
readonly protocol?: ApplicationProtocol;

/**
* The protocol version to use
*
* @default ApplicationProtocolVersion.HTTP1
*/
readonly protocolVersion?: ApplicationProtocolVersion;

/**
* The domain name for the service, e.g. "api.example.com."
*
Expand Down Expand Up @@ -412,6 +419,7 @@ export abstract class ApplicationLoadBalancedServiceBase extends CoreConstruct {

const targetProps: AddApplicationTargetsProps = {
protocol: props.targetProtocol ?? ApplicationProtocol.HTTP,
protocolVersion: props.protocolVersion,
};

this.listener = loadBalancer.addListener('PublicListener', {
Expand Down
32 changes: 31 additions & 1 deletion packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.l3s.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ABSENT, arrayWith, expect, haveResource, haveResourceLike, objectLike }
import { Certificate } from '@aws-cdk/aws-certificatemanager';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as ecs from '@aws-cdk/aws-ecs';
import { ApplicationLoadBalancer, ApplicationProtocol, NetworkLoadBalancer } from '@aws-cdk/aws-elasticloadbalancingv2';
import { ApplicationLoadBalancer, ApplicationProtocol, ApplicationProtocolVersion, NetworkLoadBalancer } from '@aws-cdk/aws-elasticloadbalancingv2';
import { PublicHostedZone } from '@aws-cdk/aws-route53';
import * as cloudmap from '@aws-cdk/aws-servicediscovery';
import * as cdk from '@aws-cdk/core';
Expand Down Expand Up @@ -1010,6 +1010,36 @@ export = {
test.done();
},

'ALB - includes provided protocol version properties'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'VPC');
const cluster = new ecs.Cluster(stack, 'Cluster', { vpc });
cluster.addCapacity('DefaultAutoScalingGroup', { instanceType: new ec2.InstanceType('t2.micro') });
const zone = new PublicHostedZone(stack, 'HostedZone', { zoneName: 'example.com' });

// WHEN
new ecsPatterns.ApplicationLoadBalancedEc2Service(stack, 'Service', {
cluster,
memoryLimitMiB: 1024,
taskImageOptions: {
image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
},
desiredCount: 1,
domainName: 'api.example.com',
domainZone: zone,
protocol: ApplicationProtocol.HTTPS,
protocolVersion: ApplicationProtocolVersion.GRPC,
});

// THEN
expect(stack).to(haveResourceLike('AWS::ElasticLoadBalancingV2::TargetGroup', {
ProtocolVersion: 'GRPC',
}));

test.done();
},

'NLB - having *HealthyPercent properties'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as cxapi from '@aws-cdk/cx-api';
import { Construct } from 'constructs';
import { BaseListener, BaseListenerLookupOptions } from '../shared/base-listener';
import { HealthCheck } from '../shared/base-target-group';
import { ApplicationProtocol, IpAddressType, SslPolicy } from '../shared/enums';
import { ApplicationProtocol, ApplicationProtocolVersion, IpAddressType, SslPolicy } from '../shared/enums';
import { IListenerCertificate, ListenerCertificate } from '../shared/listener-certificate';
import { determineProtocolAndPort } from '../shared/util';
import { ListenerAction } from './application-listener-action';
Expand Down Expand Up @@ -363,6 +363,7 @@ export class ApplicationListener extends BaseListener implements IApplicationLis
healthCheck: props.healthCheck,
port: props.port,
protocol: props.protocol,
protocolVersion: props.protocolVersion,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about adding protocolVersion to AddApplicationTargetsProps as well?

Copy link
Contributor Author

@yiluncui yiluncui Apr 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you referring to lines 798-812 later in this file?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, sorry, must have mis-read the diff as-is. This looks great then.

slowStart: props.slowStart,
stickinessCookieDuration: props.stickinessCookieDuration,
stickinessCookieName: props.stickinessCookieName,
Expand Down Expand Up @@ -802,6 +803,13 @@ export interface AddApplicationTargetsProps extends AddRuleProps {
*/
readonly protocol?: ApplicationProtocol;

/**
* The protocol version to use
*
* @default ApplicationProtocolVersion.HTTP1
*/
readonly protocolVersion?: ApplicationProtocolVersion;

/**
* The port on which the listener listens for requests.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,26 @@ describe('tests', () => {
expect(validationErrors).toEqual(["Health check protocol 'TCP' is not supported. Must be one of [HTTP, HTTPS]"]);
});

test('adding targets passes in provided protocol version', () => {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'Stack');
const lb = new elbv2.ApplicationLoadBalancer(stack, 'LB', { vpc });
const listener = lb.addListener('Listener', { port: 443, certificateArns: ['arn:someCert'] });

// WHEN
listener.addTargets('Group', {
port: 443,
protocolVersion: elbv2.ApplicationProtocolVersion.GRPC,
targets: [new FakeSelfRegisteringTarget(stack, 'Target', vpc)],
});

// THEN
expect(stack).toHaveResource('AWS::ElasticLoadBalancingV2::TargetGroup', {
ProtocolVersion: 'GRPC',
});
});

test('Can call addTargetGroups on imported listener', () => {
// GIVEN
const stack = new cdk.Stack();
Expand Down