Skip to content

Commit 04399e8

Browse files
authored
Infra/vpc lambda (#38)
* vpc updates * remove extraneous logging * additional null checks for location name * vpc lambda * line chart updates * downloads
1 parent 5fa0380 commit 04399e8

23 files changed

+968
-552
lines changed

cdk/bin/cdk.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,38 @@ import 'source-map-support/register';
33
import * as cdk from 'aws-cdk-lib';
44
import { LambdaStack } from '../lib/lambda-stack';
55
import * as dotenv from 'dotenv'
6-
import { AwsSolutionsChecks } from 'cdk-nag'
7-
import { Aspects } from 'aws-cdk-lib';
86

97
declare var process : {
108
env: {
9+
VPC_ID: string
1110
HOSTED_ZONE_ID: string
1211
HOSTED_ZONE_NAME: string
1312
DOMAIN_NAME: string
1413
ENV_NAME: string
1514
CERTIFICATE_ARN: string
15+
CDK_ACCOUNT: string
16+
CDK_REGION: string
17+
REST_API_URL: string
1618
}
1719
}
1820

1921
dotenv.config({path: '../.env'})
2022

2123
const app = new cdk.App();
22-
const stack = new LambdaStack(app, 'ExplorerLambdaStack', {
24+
const stack = new LambdaStack(app, `ExplorerLambdaStack-${process.env.ENV_NAME}`, {
25+
vpcId: process.env.VPC_ID,
2326
hostedZoneId: process.env.HOSTED_ZONE_ID,
2427
hostedZoneName: process.env.HOSTED_ZONE_NAME,
2528
domainName: process.env.DOMAIN_NAME,
2629
envName: process.env.ENV_NAME,
27-
certificateArn: process.env.CERTIFICATE_ARN
30+
certificateArn: process.env.CERTIFICATE_ARN,
31+
lambdaEnv: {
32+
REST_API_URL: process.env.REST_API_URL
33+
},
34+
env: {
35+
account: process.env.CDK_ACCOUNT,
36+
region: process.env.CDK_REGION
37+
}
2838
});
2939

3040
cdk.Tags.of(stack).add('project', 'openaq');

cdk/cdk.context.json

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"vpc-provider:account=470049585876:filter.vpc-id=vpc-01de015177eedd05e:region=us-east-1:returnAsymmetricSubnets=true": {
3+
"vpcId": "vpc-01de015177eedd05e",
4+
"vpcCidrBlock": "10.0.0.0/16",
5+
"ownerAccountId": "470049585876",
6+
"availabilityZones": [],
7+
"subnetGroups": [
8+
{
9+
"name": "Public",
10+
"type": "Public",
11+
"subnets": [
12+
{
13+
"subnetId": "subnet-0baeac8d7cea3fece",
14+
"cidr": "10.0.0.0/19",
15+
"availabilityZone": "us-east-1a",
16+
"routeTableId": "rtb-000e19ce83d0905d6"
17+
},
18+
{
19+
"subnetId": "subnet-07a17a8257f4250c5",
20+
"cidr": "10.0.32.0/19",
21+
"availabilityZone": "us-east-1b",
22+
"routeTableId": "rtb-088a4b51a5453d51c"
23+
},
24+
{
25+
"subnetId": "subnet-0524632c1b5d3e5ea",
26+
"cidr": "10.0.64.0/19",
27+
"availabilityZone": "us-east-1c",
28+
"routeTableId": "rtb-097d6b80c46bd7fd8"
29+
}
30+
]
31+
},
32+
{
33+
"name": "Private",
34+
"type": "Private",
35+
"subnets": [
36+
{
37+
"subnetId": "subnet-09f93828e47072297",
38+
"cidr": "10.0.96.0/19",
39+
"availabilityZone": "us-east-1a",
40+
"routeTableId": "rtb-03e599fde8ca3336a"
41+
},
42+
{
43+
"subnetId": "subnet-01f1f2600e62bd260",
44+
"cidr": "10.0.128.0/19",
45+
"availabilityZone": "us-east-1b",
46+
"routeTableId": "rtb-01c38feaf28ba3134"
47+
},
48+
{
49+
"subnetId": "subnet-08c5b31b1d655912b",
50+
"cidr": "10.0.160.0/19",
51+
"availabilityZone": "us-east-1c",
52+
"routeTableId": "rtb-0cf296aaf6574cb3f"
53+
}
54+
]
55+
}
56+
]
57+
}
58+
}

cdk/lib/lambda-stack.ts

+21
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
aws_lambda as lambda,
88
aws_s3 as s3,
99
aws_s3_deployment,
10+
aws_ec2 as ec2,
1011
} from 'aws-cdk-lib';
1112
import { RemovalPolicy } from 'aws-cdk-lib';
1213
import {
@@ -20,15 +21,23 @@ import { HttpLambdaIntegration } from 'aws-cdk-lib/aws-apigatewayv2-integrations
2021
import { OriginProtocolPolicy } from 'aws-cdk-lib/aws-cloudfront';
2122
import { Construct } from 'constructs';
2223

24+
interface LambdaEnv {
25+
[key: string]: string;
26+
}
27+
2328
interface StackProps extends cdk.StackProps {
2429
hostedZoneId: string;
2530
hostedZoneName: string;
2631
domainName: string;
2732
envName: string;
2833
certificateArn: string;
34+
vpcId: string;
35+
lambdaEnv: LambdaEnv;
2936
}
3037

3138

39+
40+
3241
export class LambdaStack extends cdk.Stack {
3342
constructor(
3443
scope: Construct
@@ -39,11 +48,19 @@ export class LambdaStack extends cdk.Stack {
3948
domainName,
4049
envName,
4150
certificateArn,
51+
vpcId,
52+
lambdaEnv,
4253
...props
4354
}: StackProps
4455
) {
4556
super(scope, id, props);
4657

58+
59+
const vpc = ec2.Vpc.fromLookup(this, `${id}-explorer-vpc`, {
60+
vpcId: vpcId
61+
});
62+
63+
4764
const lambdaFunction = new lambda.Function(
4865
this,
4966
`${id}-explorer-lambda`,
@@ -53,7 +70,11 @@ export class LambdaStack extends cdk.Stack {
5370
handler: 'server/index.handler',
5471
memorySize: 512,
5572
runtime: lambda.Runtime.NODEJS_20_X,
73+
vpc: vpc,
74+
allowPublicSubnet: true,
75+
vpcSubnets: {subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS},
5676
timeout: cdk.Duration.seconds(10),
77+
environment: lambdaEnv
5778
}
5879
);
5980

0 commit comments

Comments
 (0)