Skip to content

Commit

Permalink
feat(ecs-patterns): allow passthrough of security groups to service
Browse files Browse the repository at this point in the history
  • Loading branch information
clementallen committed Sep 23, 2020
1 parent c7c7851 commit a3b51e5
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 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 @@ -62,6 +62,8 @@ You can customize the health check for your target group; otherwise it defaults

Fargate services will use the `LATEST` platform version by default, but you can override by providing a value for the `platformVersion` property in the constructor.

Fargate services use the default VPC Security Group unless one or more are provided using the `securityGroups` property in the constructor.

By setting `redirectHTTP` to true, CDK will automatically create a listener on port 80 that redirects HTTP traffic to the HTTPS port.

Additionally, if more than one application target group are needed, instantiate one of the following:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ISecurityGroup } from '@aws-cdk/aws-ec2';
import { FargatePlatformVersion, FargateService, FargateTaskDefinition } from '@aws-cdk/aws-ecs';
import { Construct } from '@aws-cdk/core';
import { ApplicationLoadBalancedServiceBase, ApplicationLoadBalancedServiceBaseProps } from '../base/application-load-balanced-service-base';
Expand Down Expand Up @@ -75,6 +76,13 @@ export interface ApplicationLoadBalancedFargateServiceProps extends ApplicationL
* @default Latest
*/
readonly platformVersion?: FargatePlatformVersion;

/**
* The security groups to associate with the service. If you do not specify a security group, the default security group for the VPC is used.
*
* @default - A new security group is created.
*/
readonly securityGroups?: ISecurityGroup[];
}

/**
Expand Down Expand Up @@ -151,6 +159,7 @@ export class ApplicationLoadBalancedFargateService extends ApplicationLoadBalanc
enableECSManagedTags: props.enableECSManagedTags,
cloudMapOptions: props.cloudMapOptions,
platformVersion: props.platformVersion,
securityGroups: props.securityGroups,
});
this.addServiceAsTarget(this.service);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -637,4 +637,48 @@ export = {
test.done();
},

'passing in previously created security groups to ALB Fargate Service'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'Vpc');
const cluster = new ecs.Cluster(stack, 'Cluster', { vpc, clusterName: 'MyCluster' });
const securityGroup = new ec2.SecurityGroup(stack, 'SecurityGroup', {
allowAllOutbound: false,
description: 'Example',
securityGroupName: 'Rolly',
vpc,
});

// WHEN
new ecsPatterns.ApplicationLoadBalancedFargateService(stack, 'Service', {
cluster,
taskImageOptions: {
image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
},
securityGroups: [securityGroup],
});

// THEN
expect(stack).to(haveResourceLike('AWS::ECS::Service', {
LaunchType: 'FARGATE',
}));
expect(stack).to(haveResource('AWS::EC2::SecurityGroup', {
GroupDescription: 'Example',
GroupName: 'Rolly',
SecurityGroupEgress: [
{
CidrIp: '255.255.255.255/32',
Description: 'Disallow all traffic',
FromPort: 252,
IpProtocol: 'icmp',
ToPort: 86,
},
],
VpcId: {
Ref: 'Vpc8378EB38',
},
}));
test.done();
},

};

0 comments on commit a3b51e5

Please sign in to comment.