Last active
December 1, 2022 14:08
-
-
Save evilUrge/e9edeabb818349bd596e213143f89078 to your computer and use it in GitHub Desktop.
Generic CDK
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
import { join } from "path"; | |
import { fileURLToPath } from "url"; | |
import type { Construct } from "constructs"; | |
import { Duration, Stack, StackProps } from "aws-cdk-lib"; | |
import { LogLevel, NodejsFunction } from "aws-cdk-lib/aws-lambda-nodejs"; | |
import { Runtime } from "aws-cdk-lib/aws-lambda"; | |
import { Rule, Schedule } from "aws-cdk-lib/aws-events"; | |
import { LambdaFunction } from "aws-cdk-lib/aws-events-targets"; | |
import { Effect, PolicyStatement } from "aws-cdk-lib/aws-iam"; | |
import { dependencies as packages } from "../package.json"; | |
import { LogGroup } from 'aws-cdk-lib/aws-logs'; | |
const packagesRoot = join(fileURLToPath(import.meta.url), "..", "../.."); | |
interface genericProps extends StackProps { | |
readonly alarmsSnsTopicArn?: string; | |
readonly cronEventRule?: boolean; | |
readonly intervalDays?: string; | |
} | |
export class CdkStarterStack extends Stack { | |
constructor(scope: Construct, id: string, props: genericProps) { | |
super(scope, id, props); | |
Object.keys(packages) | |
.filter((packageName) => packageName.startsWith("@orgname")) // change this to the prefix of your module (and use lerna yarn or both!) | |
.map((app) => { | |
const metadata = require(`node_modules/${app}/metadata.json`); | |
const service = new NodejsFunction( | |
this, | |
app, | |
Object.assign( | |
{ | |
memorySize: 1024, | |
timeout: Duration.minutes(10), | |
runtime: Runtime.NODEJS_16_X, | |
handler: "handler", | |
entry: join(packagesRoot, `${app}/src/index.ts`), | |
bundling: { | |
minify: true, | |
keepNames: true, | |
metafile: true, | |
target: "es2020", | |
externalModules: ["aws-sdk"], | |
logLevel: LogLevel.ERROR, | |
tsconfig: join(packagesRoot, `${app}/tsconfig.json`), | |
// format: OutputFormat.ESM, | |
}, | |
}, | |
metadata.environment && { environment: metadata.environment } | |
// Add more custom logic here for props | |
) | |
); | |
// Grant policies for ג | |
if (metadata.policy_statement) { | |
service.addToRolePolicy( | |
new PolicyStatement({ | |
effect: Effect.ALLOW, | |
...metadata.policy_statement, | |
}) | |
); | |
} | |
// Type of ג: cronjob | |
if (metadata.type.cronjob) { | |
const eventRule = new Rule(this, "scheduleRule", { | |
schedule: Schedule.cron(metadata.type.cronjob), | |
}); | |
eventRule.addTarget(new LambdaFunction(service)); | |
} | |
// stack defaults | |
new LogGroup(this, "LogGroup", {}); | |
// end of defaults | |
return { service }; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
WIP WIP WIP
Include a metadata in the root of your node module, define the metadata file in package.json under
files:
Use the following example format