forked from getlift/lift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
191 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { SecurityGroup, Vpc } from "@aws-cdk/aws-ec2"; | ||
import { Construct as CdkConstruct } from "@aws-cdk/core"; | ||
import { FromSchema } from "json-schema-to-ts"; | ||
import { AwsCfInstruction } from "@serverless/typescript"; | ||
import { AwsConstruct, AwsProvider } from "../classes"; | ||
|
||
const VPC_DEFINITION = { | ||
type: "object", | ||
properties: { | ||
type: { const: "vpc" }, | ||
}, | ||
additionalProperties: false, | ||
required: [], | ||
} as const; | ||
|
||
type Configuration = FromSchema<typeof VPC_DEFINITION>; | ||
|
||
export class VPC extends AwsConstruct { | ||
public static type = "vpc"; | ||
public static schema = VPC_DEFINITION; | ||
|
||
private readonly vpc: Vpc; | ||
|
||
constructor(scope: CdkConstruct, id: string, configuration: Configuration, private provider: AwsProvider) { | ||
super(scope, id); | ||
|
||
this.vpc = new Vpc(this, "VPC", { | ||
maxAzs: 2, | ||
}); | ||
|
||
const privateSubnets = this.vpc.privateSubnets; | ||
|
||
const lambdaSecurityGroup = new SecurityGroup(this, "AppSecurityGroup", { | ||
vpc: this.vpc, | ||
}); | ||
|
||
provider.setVpcConfig( | ||
provider.getCloudFormationReference(lambdaSecurityGroup.securityGroupName), | ||
privateSubnets.map((subnet) => provider.getCloudFormationReference(subnet.subnetId)) | ||
); | ||
} | ||
|
||
outputs(): Record<string, () => Promise<string | undefined>> { | ||
return {}; | ||
} | ||
|
||
references(): Record<string, AwsCfInstruction> { | ||
return {}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
service: vpc | ||
configValidationMode: error | ||
|
||
provider: | ||
name: aws | ||
|
||
functions: | ||
foo: | ||
handler: worker.handler | ||
|
||
constructs: | ||
vpc: | ||
type: vpc |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { get } from "lodash"; | ||
import { AwsCfInstruction } from "@serverless/typescript"; | ||
import { baseConfig, pluginConfigExt, runServerless } from "../utils/runServerless"; | ||
import ServerlessError from "../../src/utils/error"; | ||
|
||
describe("vpc", () => { | ||
it("should put Lambda functions in the VPC", async () => { | ||
const { cfTemplate, computeLogicalId } = await runServerless({ | ||
fixture: "vpc", | ||
configExt: pluginConfigExt, | ||
command: "package", | ||
}); | ||
|
||
const vpcConfig = get(cfTemplate.Resources.FooLambdaFunction, "Properties.VpcConfig") as Record< | ||
string, | ||
unknown | ||
>; | ||
expect(vpcConfig).toHaveProperty("SecurityGroupIds"); | ||
expect((vpcConfig.SecurityGroupIds as AwsCfInstruction[])[0]).toMatchObject({ | ||
Ref: computeLogicalId("vpc", "AppSecurityGroup"), | ||
}); | ||
expect(vpcConfig).toHaveProperty("SubnetIds"); | ||
expect(vpcConfig.SubnetIds).toContainEqual({ | ||
Ref: computeLogicalId("vpc", "VPC", "PrivateSubnet1", "Subnet"), | ||
}); | ||
expect(vpcConfig.SubnetIds).toContainEqual({ | ||
Ref: computeLogicalId("vpc", "VPC", "PrivateSubnet2", "Subnet"), | ||
}); | ||
}); | ||
it("throws an error when using the construct twice", async () => { | ||
expect.assertions(2); | ||
|
||
try { | ||
await runServerless({ | ||
config: Object.assign(baseConfig, { | ||
constructs: { | ||
vpc1: { | ||
type: "vpc", | ||
}, | ||
vpc2: { | ||
type: "vpc", | ||
}, | ||
}, | ||
}), | ||
command: "package", | ||
}); | ||
} catch (error) { | ||
expect(error).toBeInstanceOf(ServerlessError); | ||
expect(error).toHaveProperty("code", "LIFT_ONLY_ONE_VPC"); | ||
} | ||
}); | ||
it("throws an error when there is an existing VPC config", async () => { | ||
expect.assertions(2); | ||
|
||
try { | ||
await runServerless({ | ||
fixture: "vpc", | ||
configExt: Object.assign(pluginConfigExt, { | ||
provider: { | ||
name: "aws", | ||
vpc: { | ||
securityGroupIds: ["sg-00000000000000000"], | ||
subnetIds: ["subnet-01234567899999999", "subnet-00000000099999999"], | ||
}, | ||
}, | ||
}), | ||
command: "package", | ||
}); | ||
} catch (error) { | ||
expect(error).toBeInstanceOf(ServerlessError); | ||
expect(error).toHaveProperty("code", "LIFT_ONLY_ONE_VPC"); | ||
} | ||
}); | ||
}); |