Skip to content

Commit

Permalink
feat(aws-elasticloadbalancingv2): add protocol version for ALB Target…
Browse files Browse the repository at this point in the history
…Groups (#13570)

Fixes #12869


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
ChristopheBougere committed Mar 12, 2021
1 parent 64da84b commit 165a3d8
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
14 changes: 14 additions & 0 deletions packages/@aws-cdk/aws-elasticloadbalancingv2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,20 @@ const tg2 = new elbv2.ApplicationTargetGroup(stack, 'TG2', {

For more information see: https://docs.aws.amazon.com/elasticloadbalancing/latest/application/sticky-sessions.html#application-based-stickiness

### Setting the target group protocol version

By default, Application Load Balancers send requests to targets using HTTP/1.1. You can use the [protocol version](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-target-groups.html#target-group-protocol-version) to send requests to targets using HTTP/2 or gRPC.

```ts
const tg = new elbv2.ApplicationTargetGroup(stack, 'TG', {
targetType: elbv2.TargetType.IP,
port: 50051,
protocol: elbv2.ApplicationProtocol.HTTP,
protocolVersion: elbv2.ApplicationProtocolVersion.GRPC,
vpc,
});
```

## Using Lambda Targets

To use a Lambda Function as a target, use the integration class in the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
BaseTargetGroupProps, ITargetGroup, loadBalancerNameFromListenerArn, LoadBalancerTargetProps,
TargetGroupAttributes, TargetGroupBase, TargetGroupImportProps,
} from '../shared/base-target-group';
import { ApplicationProtocol, Protocol, TargetType } from '../shared/enums';
import { ApplicationProtocol, ApplicationProtocolVersion, Protocol, TargetType } from '../shared/enums';
import { ImportedTargetGroupBase } from '../shared/imported';
import { determineProtocolAndPort } from '../shared/util';
import { IApplicationListener } from './application-listener';
Expand All @@ -28,6 +28,13 @@ export interface ApplicationTargetGroupProps extends BaseTargetGroupProps {
*/
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 Expand Up @@ -110,8 +117,10 @@ export class ApplicationTargetGroup extends TargetGroupBase implements IApplicat

constructor(scope: Construct, id: string, props: ApplicationTargetGroupProps = {}) {
const [protocol, port] = determineProtocolAndPort(props.protocol, props.port);
const { protocolVersion } = props;
super(scope, id, { ...props }, {
protocol,
protocolVersion,
port,
});

Expand Down
20 changes: 20 additions & 0 deletions packages/@aws-cdk/aws-elasticloadbalancingv2/lib/shared/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,26 @@ export enum ApplicationProtocol {
HTTPS = 'HTTPS'
}

/**
* Load balancing protocol version for application load balancers
*/
export enum ApplicationProtocolVersion {
/**
* GRPC
*/
GRPC = 'GRPC',

/**
* HTTP1
*/
HTTP1 = 'HTTP1',

/**
* HTTP2
*/
HTTP2 = 'HTTP2',
}

/**
* Elastic Load Balancing provides the following security policies for Application Load Balancers
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,24 @@ describe('tests', () => {
});
});

test('Can set a protocol version', () => {
// GIVEN
const app = new cdk.App();
const stack = new cdk.Stack(app, 'Stack');
const vpc = new ec2.Vpc(stack, 'VPC', {});

// WHEN
new elbv2.ApplicationTargetGroup(stack, 'TargetGroup', {
vpc,
protocolVersion: elbv2.ApplicationProtocolVersion.GRPC,
});

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

test('Bad stickiness cookie names', () => {
// GIVEN
const app = new cdk.App();
Expand Down