Skip to content

Commit

Permalink
fix(ecs): default Service throws in a VPC without private subnets (#7188
Browse files Browse the repository at this point in the history
)

People like to create VPCs without private subnets, as the NAT
gateways (or NAT instances) can be expensive.

The default settings of an ECS Service is to create themselves
in a PRIVATE subnet, instead of the "default selection order" of
Private -> Isolated -> Public. Now use the default selection
(by not supplying a `subnetType` at all).

Fixes #7062.
  • Loading branch information
rix0rrr authored Apr 6, 2020
1 parent 2f6bd74 commit 0ef6a95
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-ecs/lib/base/base-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ export abstract class BaseService extends Resource
// tslint:disable-next-line:max-line-length
protected configureAwsVpcNetworking(vpc: ec2.IVpc, assignPublicIp?: boolean, vpcSubnets?: ec2.SubnetSelection, securityGroup?: ec2.ISecurityGroup) {
if (vpcSubnets === undefined) {
vpcSubnets = { subnetType: assignPublicIp ? ec2.SubnetType.PUBLIC : ec2.SubnetType.PRIVATE };
vpcSubnets = assignPublicIp ? { subnetType: ec2.SubnetType.PUBLIC } : {};
}
if (securityGroup === undefined) {
securityGroup = new ec2.SecurityGroup(this, 'SecurityGroup', { vpc });
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-ecs/lib/ec2/ec2-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export interface Ec2ServiceProps extends BaseServiceOptions {
*
* This property is only used for tasks that use the awsvpc network mode.
*
* @default - Private subnets.
* @default - Public subnets if `assignPublicIp` is set, otherwise the first available one of Private, Isolated, Public, in that order.
*/
readonly vpcSubnets?: ec2.SubnetSelection;

Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-ecs/lib/fargate/fargate-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface FargateServiceProps extends BaseServiceOptions {
/**
* The subnets to associate with the service.
*
* @default - Private subnets.
* @default - Public subnets if `assignPublicIp` is set, otherwise the first available one of Private, Isolated, Public, in that order.
*/
readonly vpcSubnets?: ec2.SubnetSelection;

Expand Down
28 changes: 28 additions & 0 deletions packages/@aws-cdk/aws-ecs/test/fargate/test.fargate-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,34 @@ export = {
test.done();
},

'can create service with default settings if VPC only has public subnets'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'MyVpc', {
subnetConfiguration: [
{
cidrMask: 28,
name: 'public-only',
subnetType: ec2.SubnetType.PUBLIC
}
]
});
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
const taskDefinition = new ecs.FargateTaskDefinition(stack, 'FargateTaskDef');
taskDefinition.addContainer('web', {
image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
});

// WHEN
new ecs.FargateService(stack, 'FargateService', {
cluster,
taskDefinition,
});

// THEN -- did not throw
test.done();
},

'with custom cloudmap namespace'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand Down

0 comments on commit 0ef6a95

Please sign in to comment.