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: add default security group stack #23

Merged
merged 5 commits into from
Feb 9, 2025
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
17 changes: 16 additions & 1 deletion src/fargate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import * as targets from 'aws-cdk-lib/aws-route53-targets'
import { StackConfig } from './configuration'
import { ARecord } from './route53'
import * as elb from 'aws-cdk-lib/aws-elasticloadbalancingv2'
import { SecurityGroup } from './security-group'

// Default docker image to use
const DEFAULT_IMAGE = 'nginxdemos/hello:latest'
Expand Down Expand Up @@ -50,6 +51,7 @@ interface FargateServiceProps extends cdk.NestedStackProps {
repository?: ecr.IRepository
taskConfiguration: TaskConfiguration
image?: ecs.ContainerImage
securityGroup?: ec2.ISecurityGroup
}

/**
Expand Down Expand Up @@ -77,6 +79,7 @@ export class FargateService extends cdk.NestedStack {
zone,
repository,
taskConfiguration,
securityGroup: defaultSecurityGroup,
} = props

// Compile secrets into list of mapped ecs.Secrets
Expand Down Expand Up @@ -104,10 +107,21 @@ export class FargateService extends cdk.NestedStack {
ecs.ContainerImage.fromEcrRepository(repository, imageVersion)) ||
ecs.ContainerImage.fromRegistry(DEFAULT_IMAGE)

// Scaffold default security group if not provided
let securityGroup = defaultSecurityGroup
if (!securityGroup) {
const groupStack = new SecurityGroup(
this,
stack.getResourceID('SecurityGroup'),
{ vpc: cluster.vpc },
)
securityGroup = groupStack.securityGroup
}

const desiredCount = taskConfiguration?.desiredCount || 1
this.service = new ecs_patterns.ApplicationLoadBalancedFargateService(
this,
stack.getResourceID('AdminService'),
stack.getResourceID('FargateService'),
{
assignPublicIp: true,
cluster: cluster,
Expand All @@ -127,6 +141,7 @@ export class FargateService extends cdk.NestedStack {
subnetType: ec2.SubnetType.PUBLIC,
onePerAz: true,
},
securityGroups: [securityGroup],
},
)

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export * from './helper'
export * from './ipv6vpc'
export * from './rds'
export * from './route53'
export * from './security-group'
export * from './s3'
export * from './vpc'
export * from './website-distribution'
42 changes: 42 additions & 0 deletions src/security-group.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Construct } from 'constructs'
import * as cdk from 'aws-cdk-lib'
import * as ec2 from 'aws-cdk-lib/aws-ec2'

export interface SecurityGroupProps extends cdk.NestedStackProps {
vpc: ec2.IVpc
incomingPorts?: number[]
}

/**
* Default security group suitable for a basic ECS service
*/
export class SecurityGroup extends cdk.NestedStack {
public readonly securityGroup: ec2.ISecurityGroup

constructor(scope: Construct, id: string, props: SecurityGroupProps) {
super(scope, id, props)
const { vpc, incomingPorts } = props

this.securityGroup = new ec2.SecurityGroup(this, `${id}Group`, {
vpc,
description: 'Allows HTTP/HTTPS ingress and all egress traffic',
allowAllOutbound: true,
})

const portsToOpen = incomingPorts || [80, 443]

// Open all selected ports
portsToOpen.forEach((port) => {
this.securityGroup.addIngressRule(
ec2.Peer.anyIpv4(),
ec2.Port.tcp(port),
`Allow ${port} from anywhere (IPv4)`,
)
this.securityGroup.addIngressRule(
ec2.Peer.anyIpv6(),
ec2.Port.tcp(port),
`Allow ${port} from anywhere (IPv6)`,
)
})
}
}
Loading