Skip to content

Commit

Permalink
feat(ecs): allow HTTPS connections from LB to task (#11381)
Browse files Browse the repository at this point in the history
If you need or want to encrypt traffic between the load balancer and the ECS task you need to set the protocol of the target group to `HTTPS`. This PR adds a new property to specify this.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
hoegertn authored Nov 20, 2020
1 parent 2bcbc5f commit 0f6e2da
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-ecs-patterns/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ By setting `redirectHTTP` to true, CDK will automatically create a listener on p

If you specify the option `recordType` you can decide if you want the construct to use CNAME or Route53-Aliases as record sets.

If you need to encrypt the traffic between the load balancer and the ECS tasks, you can set the `targetProtocol` to `HTTPS`.

Additionally, if more than one application target group are needed, instantiate one of the following:

* `ApplicationMultipleTargetGroupsEc2Service`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { IVpc } from '@aws-cdk/aws-ec2';
import { AwsLogDriver, BaseService, CloudMapOptions, Cluster, ContainerImage, ICluster, LogDriver, PropagatedTagSource, Secret } from '@aws-cdk/aws-ecs';
import {
ApplicationListener, ApplicationLoadBalancer, ApplicationProtocol, ApplicationTargetGroup,
IApplicationLoadBalancer, ListenerCertificate, ListenerAction,
IApplicationLoadBalancer, ListenerCertificate, ListenerAction, AddApplicationTargetsProps,
} from '@aws-cdk/aws-elasticloadbalancingv2';
import { IRole } from '@aws-cdk/aws-iam';
import { ARecord, IHostedZone, RecordTarget, CnameRecord } from '@aws-cdk/aws-route53';
Expand Down Expand Up @@ -88,6 +88,15 @@ export interface ApplicationLoadBalancedServiceBaseProps {
*/
readonly certificate?: ICertificate;

/**
* The protocol for connections from the load balancer to the ECS tasks.
* The default target port is determined from the protocol (port 80 for
* HTTP, port 443 for HTTPS).
*
* @default HTTP.
*/
readonly targetProtocol?: ApplicationProtocol;

/**
* The protocol for connections from clients to the load balancer.
* The load balancer port is determined from the protocol (port 80 for
Expand Down Expand Up @@ -369,8 +378,8 @@ export abstract class ApplicationLoadBalancedServiceBase extends cdk.Construct {
throw new Error('The HTTPS protocol must be used when redirecting HTTP traffic');
}

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

this.listener = loadBalancer.addListener('PublicListener', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,43 @@ export = {
test.done();
},

'target group uses HTTP/80 as default'(test: Test) {
// GIVEN
const stack = new cdk.Stack();

// WHEN
new ecsPatterns.ApplicationLoadBalancedFargateService(stack, 'Service', {
taskImageOptions: {
image: ecs.ContainerImage.fromRegistry('/aws/aws-example-app'),
},
});
// THEN
expect(stack).to(haveResourceLike('AWS::ElasticLoadBalancingV2::TargetGroup', {
Port: 80,
Protocol: 'HTTP',
}));
test.done();
},

'target group uses HTTPS/443 when configured'(test: Test) {
// GIVEN
const stack = new cdk.Stack();

// WHEN
new ecsPatterns.ApplicationLoadBalancedFargateService(stack, 'Service', {
taskImageOptions: {
image: ecs.ContainerImage.fromRegistry('/aws/aws-example-app'),
},
targetProtocol: ApplicationProtocol.HTTPS,
});
// THEN
expect(stack).to(haveResourceLike('AWS::ElasticLoadBalancingV2::TargetGroup', {
Port: 443,
Protocol: 'HTTPS',
}));
test.done();
},

'setting platform version'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand Down

0 comments on commit 0f6e2da

Please sign in to comment.