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

feat(aws-elasticloadbalancingv2): add protocol version for ALB TargetGroups #13570

Merged
merged 4 commits into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
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