-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoldstarter-stack.ts
92 lines (86 loc) · 2.89 KB
/
coldstarter-stack.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import * as cdk from "aws-cdk-lib";
import { Construct } from "constructs";
import events = require("aws-cdk-lib/aws-events");
import targets = require("aws-cdk-lib/aws-events-targets");
import { RetentionDays } from "aws-cdk-lib/aws-logs";
import * as logs from "aws-cdk-lib/aws-logs";
import { Architecture, Runtime } from "aws-cdk-lib/aws-lambda";
import { NodejsFunction, OutputFormat } from "aws-cdk-lib/aws-lambda-nodejs";
// import * as sqs from 'aws-cdk-lib/aws-sqs';
export class ColdstarterStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const SERVICE_NAME = "Function";
const COLDSTARTER_NAME = "Coldstarter";
// run every 8 hours
const rule = new events.Rule(this, "Rule", {
schedule: events.Schedule.expression("cron(0 */3 * * ? *)"),
ruleName: `${COLDSTARTER_NAME}Rule`,
});
const lambdaWithoutEnv = createLambda(
this,
SERVICE_NAME,
"src/handler.js",
false
);
const lambdaWithEnv = createLambda(
this,
`${SERVICE_NAME}WithEnv`,
"src/handler.js",
true
);
const coldStarter = createLambda(
this,
COLDSTARTER_NAME,
"src/coldstarter.js",
true
);
lambdaWithEnv.grantInvoke(coldStarter);
lambdaWithoutEnv.grantInvoke(coldStarter);
rule.addTarget(new targets.LambdaFunction(coldStarter));
}
}
function createLambda(
scope: Construct,
name: string,
handlerLocation: string,
hasEnv: boolean
) {
const logGroup = new logs.LogGroup(scope, `${name}LogGroup`, {
logGroupName: `/aws/lambda/${name}`,
removalPolicy: cdk.RemovalPolicy.DESTROY,
retention: RetentionDays.ONE_WEEK,
});
return new NodejsFunction(scope, name, {
entry: handlerLocation,
timeout: cdk.Duration.seconds(10),
functionName: name,
memorySize: 512,
runtime: Runtime.NODEJS_20_X,
architecture: Architecture.ARM_64,
awsSdkConnectionReuse: hasEnv,
bundling: {
metafile: false, //set to true to create the necessary metafile for https://esbuild.github.io/analyze/ to analyze the bundle
minify: true,
target: "esnext",
format: OutputFormat.ESM,
platform: "node",
mainFields: ["module", "main"],
externalModules: [
"@aws-sdk/client-sso",
"@aws-sdk/client-sso-oidc",
"@aws-sdk/credential-provider-ini",
"@aws-sdk/credential-provider-process",
"@aws-sdk/credential-provider-sso",
"@aws-sdk/credential-provider-web-identity",
"@aws-sdk/token-providers",
],
banner:
"const require = (await import('node:module')).createRequire(import.meta.url);const __filename = (await import('node:url')).fileURLToPath(import.meta.url);const __dirname = (await import('node:path')).dirname(__filename);",
},
logGroup,
});
}
const app = new cdk.App();
new ColdstarterStack(app, "Coldstarter");
app.synth();