-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstack.ts
66 lines (58 loc) · 2.15 KB
/
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
import { Stack, StackProps } from "aws-cdk-lib";
import { Construct } from "constructs";
import * as cdk from 'aws-cdk-lib';
import { join } from "path";
export class Part17EventBridgeSchedulerStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
// Lambda function triggered by scheduler
const executeMemo = new cdk.aws_lambda_nodejs.NodejsFunction(this, 'ExecuteMemo', {
entry: join(__dirname, 'executeMemo.ts'),
handler: 'handler',
runtime: cdk.aws_lambda.Runtime.NODEJS_18_X,
bundling: {
externalModules: ['@aws-sdk']
},
});
// Create role for scheduler to invoke executeMemo
const invokeExecuteMemoRole = new cdk.aws_iam.Role(this, 'InvokeMemoRole', {
assumedBy: new cdk.aws_iam.ServicePrincipal('scheduler.amazonaws.com'),
});
invokeExecuteMemoRole.addToPolicy(new cdk.aws_iam.PolicyStatement({
actions: ['lambda:InvokeFunction'],
resources: [executeMemo.functionArn]
}));
// Lambda function that schedules executeMemo
const addMemo = new cdk.aws_lambda_nodejs.NodejsFunction(this, 'AddMemo', {
entry: join(__dirname, 'addMemo.ts'),
handler: 'handler',
runtime: cdk.aws_lambda.Runtime.NODEJS_18_X,
bundling: {
externalModules: ['@aws-sdk']
},
environment: {
SCHEDULE_TARGET_ARN: executeMemo.functionArn,
SCHEDULE_ROLE_ARN: invokeExecuteMemoRole.roleArn,
},
});
// Allow addMemo to create a scheduler
addMemo.addToRolePolicy(
new cdk.aws_iam.PolicyStatement({
actions: ['scheduler:CreateSchedule'],
resources: ['*'],
}),
);
// Allow addMemo to pass the invokeExecuteMemoRole to the scheduler
addMemo.addToRolePolicy(
new cdk.aws_iam.PolicyStatement({
actions: ['iam:PassRole'],
resources: [invokeExecuteMemoRole.roleArn],
}),
);
// Trigger addMemo via API Gateway
const api = new cdk.aws_apigateway.RestApi(this, 'Api', {
restApiName: 'Part17Service'
});
api.root.addResource('addMemo').addMethod('POST', new cdk.aws_apigateway.LambdaIntegration(addMemo));
}
}