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-ecs): TLS support for Fargate service applet #1184

Merged
merged 1 commit into from
Nov 15, 2018
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
@@ -1,4 +1,6 @@
import ec2 = require('@aws-cdk/aws-ec2');
import { CertificateRef } from '@aws-cdk/aws-certificatemanager';
import { VpcNetwork } from '@aws-cdk/aws-ec2';
import { HostedZoneProvider } from '@aws-cdk/aws-route53';
import cdk = require('@aws-cdk/cdk');
import { Cluster } from './cluster';
import { ContainerImage } from './container-image';
Expand Down Expand Up @@ -77,18 +79,44 @@ export interface LoadBalancedFargateServiceAppletProps extends cdk.StackProps {
* @default 1
*/
desiredCount?: number;

/*
* Domain name for the service, e.g. api.example.com
*/
domainName?: string;

/**
* Route53 hosted zone for the domain, e.g. "example.com."
*/
domainZone?: string;

/**
* Certificate Manager certificate to associate with the load balancer.
* Setting this option will set the load balancer port to 443.
*/
certificate?: string;
}

/**
* An applet for a LoadBalancedFargateService
* An applet for a LoadBalancedFargateService. Sets up a Fargate service, Application
* load balancer, ECS cluster, VPC, and (optionally) Route53 alias record.
*/
export class LoadBalancedFargateServiceApplet extends cdk.Stack {
constructor(parent: cdk.App, id: string, props: LoadBalancedFargateServiceAppletProps) {
super(parent, id, props);

const vpc = new ec2.VpcNetwork(this, 'MyVpc', { maxAZs: 2 });
const vpc = new VpcNetwork(this, 'MyVpc', { maxAZs: 2 });
const cluster = new Cluster(this, 'Cluster', { vpc });

let domainZone;
if (props.domainZone) {
domainZone = new HostedZoneProvider(this, { domainName: props.domainZone }).findAndImport(this, 'Zone');
}
let certificate;
if (props.certificate) {
certificate = CertificateRef.import(this, 'Cert', { certificateArn: props.certificate });
}

// Instantiate Fargate Service with just cluster and image
new LoadBalancedFargateService(this, "FargateService", {
cluster,
Expand All @@ -99,6 +127,9 @@ export class LoadBalancedFargateServiceApplet extends cdk.Stack {
publicTasks: props.publicTasks,
image: ContainerImage.fromDockerHub(props.image),
desiredCount: props.desiredCount,
certificate,
domainName: props.domainName,
domainZone
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export interface LoadBalancedFargateServiceProps {
}

/**
* A single task running on an ECS cluster fronted by a load balancer
* A Fargate service running on an ECS cluster fronted by a load balancer
*/
export class LoadBalancedFargateService extends cdk.Construct {
public readonly loadBalancer: elbv2.ApplicationLoadBalancer;
Expand Down
51 changes: 51 additions & 0 deletions packages/@aws-cdk/aws-ecs/test/test.l3s.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,57 @@ export = {
LaunchType: "FARGATE",
}));

test.done();
},

'test Fargateloadbalanced applet with TLS'(test: Test) {
// WHEN
const app = new cdk.App();
const stack = new ecs.LoadBalancedFargateServiceApplet(app, 'Service', {
image: 'test',
desiredCount: 2,
domainName: 'api.example.com',
domainZone: 'example.com',
certificate: 'helloworld'
});

// THEN - stack contains a load balancer and a service
expect(stack).to(haveResource('AWS::ElasticLoadBalancingV2::LoadBalancer'));

expect(stack).to(haveResource('AWS::ElasticLoadBalancingV2::Listener', {
Port: 443,
Certificates: [{
CertificateArn: "helloworld"
}]
}));

expect(stack).to(haveResource("AWS::ECS::Service", {
DesiredCount: 2,
LaunchType: "FARGATE",
}));

expect(stack).to(haveResource('AWS::Route53::RecordSet', {
Name: 'api.example.com.',
HostedZoneId: "/hostedzone/DUMMY",
Type: 'A',
AliasTarget: {
HostedZoneId: { 'Fn::GetAtt': [ 'FargateServiceLBB353E155', 'CanonicalHostedZoneID' ] },
DNSName: { 'Fn::GetAtt': [ 'FargateServiceLBB353E155', 'DNSName' ] },
}
}));

test.done();
},

"errors when setting domainName but not domainZone on applet"(test: Test) {
// THEN
test.throws(() => {
new ecs.LoadBalancedFargateServiceApplet(new cdk.App(), 'Service', {
image: 'test',
domainName: 'api.example.com'
});
});

test.done();
}
};