-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
73 lines (62 loc) · 2.44 KB
/
index.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
import { Bucket, StorageClass, EventType } from '@aws-cdk/aws-s3';
import { AssetCode, Function, Runtime } from '@aws-cdk/aws-lambda';
import { App, Construct, StackProps, Stack, Duration, CfnOutput } from '@aws-cdk/core';
import { LambdaDestination } from '@aws-cdk/aws-s3-notifications';
import { Topic } from '@aws-cdk/aws-sns';
import { EmailSubscription } from '@aws-cdk/aws-sns-subscriptions';
import { PolicyStatement } from '@aws-cdk/aws-iam';
export class S3LifeCycleRulesAuditorStack extends Stack {
constructor(scope: Construct, id: string, emailAddress: string, props?: StackProps) {
super(scope, id, props);
const bucket = new Bucket(this, "Bucket");
bucket.addLifecycleRule({
enabled: true,
prefix: '/foo',
transitions: [
{
storageClass: StorageClass.GLACIER,
transitionAfter: Duration.days(7),
}
]
});
bucket.addLifecycleRule({
enabled: true,
prefix: '/bar',
transitions: [
{
storageClass: StorageClass.GLACIER,
transitionAfter: Duration.days(14)
}
]
});
const topic = new Topic(this, 'LifeCycleAuditTopic');
topic.addSubscription(new EmailSubscription(emailAddress));
const lambda = new Function(this, 'LifeCycleAuditor', {
runtime: Runtime.NODEJS_10_X,
handler: 'index.handler',
code: new AssetCode('lambdas/s3_life_cycle_rules_auditor'),
environment: {
topic_arn: topic.topicArn
},
initialPolicy: [
new PolicyStatement({
actions: ["sns:Publish"],
resources: [topic.topicArn]
}),
new PolicyStatement({
actions: [
"s3:ListBucket",
"s3:GetLifecycleConfiguration"
],
resources: [bucket.bucketArn]
})
]
});
bucket.addEventNotification(EventType.OBJECT_CREATED, new LambdaDestination(lambda));
new CfnOutput(this, "bucketName", {
value: bucket.bucketName
});
}
}
const app = new App();
new S3LifeCycleRulesAuditorStack(app, 'S3LifeCycleRulesAuditorStack', app.node.tryGetContext('email_address'));