Skip to content

Commit 8871932

Browse files
committed
Add VPC
1 parent ca9127d commit 8871932

File tree

6 files changed

+386
-2
lines changed

6 files changed

+386
-2
lines changed

src/function/PhpFpmFunction.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Function, FunctionProps, Runtime } from 'aws-cdk-lib/aws-lambda';
22
import { Duration, Stack } from 'aws-cdk-lib';
33
import { Construct } from 'constructs';
4-
import { functionDefaults } from './defaults';
4+
import { functionDefaults, vpcDefaults } from './defaults';
55
import { fpmLayer } from '../layers';
66
import { packagePhpCode } from '../package';
77

@@ -26,6 +26,7 @@ export class PhpFpmFunction extends Function {
2626

2727
super(scope, id, {
2828
...defaults,
29+
...vpcDefaults(props.vpc),
2930
// Provided props override defaults
3031
...props,
3132
// But we force the layers to an empty array because we define them below

src/function/PhpFunction.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import { Function, FunctionProps, Runtime } from 'aws-cdk-lib/aws-lambda';
22
import { Duration, Stack } from 'aws-cdk-lib';
33
import { Construct } from 'constructs';
4-
import { functionDefaults } from './defaults';
4+
import { functionDefaults, vpcDefaults } from './defaults';
55
import { functionLayer } from '../layers';
66
import { packagePhpCode } from '../package';
7+
import { IVpc } from 'aws-cdk-lib/aws-ec2';
8+
import { VpcForServerlessApp } from '../vpc/VpcForServerlessApp';
79

810
export type PhpFunctionProps = Partial<FunctionProps> & {
911
phpVersion?: '8.0' | '8.1' | '8.2';
1012
handler: string;
13+
vpc: IVpc | VpcForServerlessApp;
1114
};
1215

1316
export class PhpFunction extends Function {
@@ -24,6 +27,7 @@ export class PhpFunction extends Function {
2427

2528
super(scope, id, {
2629
...defaults,
30+
...vpcDefaults(props.vpc),
2731
// Provided props override defaults
2832
...props,
2933
// But we force the layers to an empty array because we define them below

src/function/defaults.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,30 @@
1+
import { IVpc, SecurityGroup, SubnetSelection, SubnetType } from 'aws-cdk-lib/aws-ec2';
2+
import { VpcForServerlessApp } from '../vpc/VpcForServerlessApp';
3+
14
export const functionDefaults = {
25
path: process.cwd(),
36
phpVersion: '8.1',
47
memorySize: 1024,
58
platform: 'x86',
69
excludedPhpPaths: ['.git', '.idea', 'cdk.out', 'node_modules', '.bref', '.serverless', 'tests'],
710
} as const;
11+
12+
export function vpcDefaults(vpc?: IVpc):
13+
| Record<string, never>
14+
| {
15+
vpc: IVpc;
16+
securityGroups: SecurityGroup[];
17+
vpcSubnets: SubnetSelection;
18+
} {
19+
if (vpc instanceof VpcForServerlessApp) {
20+
// Automatically set the security group and subnets
21+
return {
22+
vpc: vpc,
23+
securityGroups: [vpc.appSecurityGroup],
24+
vpcSubnets: {
25+
subnetType: SubnetType.PRIVATE_WITH_EGRESS,
26+
},
27+
};
28+
}
29+
return {};
30+
}

src/vpc/VpcForServerlessApp.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { SecurityGroup, SubnetType, Vpc, VpcProps } from 'aws-cdk-lib/aws-ec2';
2+
import { Construct } from 'constructs';
3+
4+
export class VpcForServerlessApp extends Vpc {
5+
public readonly appSecurityGroup: SecurityGroup;
6+
7+
constructor(scope: Construct, id: string, props?: VpcProps) {
8+
const defaults: VpcProps = {
9+
maxAzs: 1,
10+
subnetConfiguration: [
11+
{
12+
cidrMask: 24,
13+
name: 'Public',
14+
subnetType: SubnetType.PUBLIC,
15+
},
16+
// For Lambda
17+
{
18+
cidrMask: 24,
19+
name: 'App',
20+
subnetType: SubnetType.PRIVATE_WITH_EGRESS,
21+
},
22+
// For private services like databases
23+
{
24+
cidrMask: 28,
25+
name: 'Isolated',
26+
subnetType: SubnetType.PRIVATE_ISOLATED,
27+
},
28+
],
29+
};
30+
super(scope, id, { ...defaults, ...props });
31+
32+
this.appSecurityGroup = new SecurityGroup(this, 'AppSecurityGroup', {
33+
vpc: this,
34+
description: 'Security group for Lambda functions',
35+
allowAllOutbound: true,
36+
allowAllIpv6Outbound: true,
37+
});
38+
}
39+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { cleanupTemplate, compileTestStack } from '../helper';
3+
import { VpcForServerlessApp } from '../../src';
4+
5+
describe('VpcForServerlessApp', () => {
6+
it('builds', () => {
7+
const template = compileTestStack((stack) => {
8+
new VpcForServerlessApp(stack, 'Vpc');
9+
}).toJSON();
10+
11+
expect(cleanupTemplate(template).Resources).toMatchSnapshot();
12+
});
13+
});

0 commit comments

Comments
 (0)