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(ecs-patterns): allow passthrough of security groups to service #10501

Merged
merged 3 commits into from
Sep 24, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
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();
},

};