Skip to content

Commit 5c02533

Browse files
committed
Upgraded to AWS CDK v2
1 parent abf95b6 commit 5c02533

File tree

6 files changed

+371
-2262
lines changed

6 files changed

+371
-2262
lines changed

bin/cdk.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
22
import 'source-map-support/register';
3-
import * as cdk from '@aws-cdk/core';
3+
import * as cdk from 'aws-cdk-lib';
44
import { RdsSnapshotExportPipelineStack, RdsEventId, RdsSnapshotType } from '../lib/rds-snapshot-export-pipeline-stack';
55

66
const app = new cdk.App();
@@ -14,4 +14,4 @@ new RdsSnapshotExportPipelineStack(app, 'RdsSnapshotExportToS3Pipeline', {
1414
}
1515
],
1616
s3BucketName: '<desired-s3-bucket-name>',
17-
});
17+
});

cdk.context.json

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
{
2-
"@aws-cdk/core:enableStackNameDuplicates": "true",
32
"aws-cdk:enableDiffNoFail": "true"
43
}

cdk.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
{
1+
{
22
"app": "npx ts-node bin/cdk.ts"
33
}

lib/rds-snapshot-export-pipeline-stack.ts

+50-42
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
import * as cdk from "@aws-cdk/core";
1+
import { aws_lambda_event_sources, Stack, StackProps, Duration } from 'aws-cdk-lib';
2+
import { Construct } from 'constructs';
23
import * as path from "path";
3-
import {CfnCrawler} from "@aws-cdk/aws-glue";
4-
import {ManagedPolicy, PolicyDocument, Role, ServicePrincipal, AccountRootPrincipal} from "@aws-cdk/aws-iam";
5-
import {Code, Function, Runtime} from "@aws-cdk/aws-lambda";
6-
import {SnsEventSource} from "@aws-cdk/aws-lambda-event-sources";
7-
import {Key} from "@aws-cdk/aws-kms";
8-
import {CfnEventSubscription} from "@aws-cdk/aws-rds";
9-
import {BlockPublicAccess, Bucket} from "@aws-cdk/aws-s3";
10-
import {Topic} from "@aws-cdk/aws-sns";
4+
import { aws_s3, aws_glue, aws_iam, aws_lambda, aws_sns, aws_rds, aws_kms } from 'aws-cdk-lib';
5+
import { Policy } from 'aws-cdk-lib/aws-iam';
116

127
export enum RdsEventId {
138
/**
@@ -58,7 +53,7 @@ export interface RdsSnapshot {
5853
rdsSnapshotType: RdsSnapshotType;
5954
}
6055

61-
export interface RdsSnapshotExportPipelineStackProps extends cdk.StackProps {
56+
export interface RdsSnapshotExportPipelineStackProps extends StackProps {
6257
/**
6358
* Name of the S3 bucket to which snapshot exports should be saved.
6459
*
@@ -77,20 +72,20 @@ export interface RdsSnapshotExportPipelineStackProps extends cdk.StackProps {
7772
readonly rdsEvents: Array<RdsSnapshot>;
7873
};
7974

80-
export class RdsSnapshotExportPipelineStack extends cdk.Stack {
81-
constructor(scope: cdk.Construct, id: string, props: RdsSnapshotExportPipelineStackProps) {
75+
export class RdsSnapshotExportPipelineStack extends Stack {
76+
constructor(scope: Construct, id: string, props: RdsSnapshotExportPipelineStackProps) {
8277
super(scope, id, props);
8378

84-
const bucket = new Bucket(this, "SnapshotExportBucket", {
79+
const bucket = new aws_s3.Bucket(this, "SnapshotExportBucket", {
8580
bucketName: props.s3BucketName,
86-
blockPublicAccess: BlockPublicAccess.BLOCK_ALL,
81+
blockPublicAccess: aws_s3.BlockPublicAccess.BLOCK_ALL,
8782
});
8883

89-
const snapshotExportTaskRole = new Role(this, "SnapshotExportTaskRole", {
90-
assumedBy: new ServicePrincipal("export.rds.amazonaws.com"),
84+
const snapshotExportTaskRole = new aws_iam.Role(this, "SnapshotExportTaskRole", {
85+
assumedBy: new aws_iam.ServicePrincipal("export.rds.amazonaws.com"),
9186
description: "Role used by RDS to perform snapshot exports to S3",
9287
inlinePolicies: {
93-
"SnapshotExportTaskPolicy": PolicyDocument.fromJson({
88+
"SnapshotExportTaskPolicy": aws_iam.PolicyDocument.fromJson({
9489
"Version": "2012-10-17",
9590
"Statement": [
9691
{
@@ -112,11 +107,11 @@ export class RdsSnapshotExportPipelineStack extends cdk.Stack {
112107
}
113108
});
114109

115-
const lambdaExecutionRole = new Role(this, "RdsSnapshotExporterLambdaExecutionRole", {
116-
assumedBy: new ServicePrincipal("lambda.amazonaws.com"),
110+
const lambdaExecutionRole = new aws_iam.Role(this, "RdsSnapshotExporterLambdaExecutionRole", {
111+
assumedBy: new aws_iam.ServicePrincipal("lambda.amazonaws.com"),
117112
description: 'RdsSnapshotExportToS3 Lambda execution role for the "' + props.dbName + '" database.',
118113
inlinePolicies: {
119-
"SnapshotExporterLambdaPolicy": PolicyDocument.fromJson({
114+
"SnapshotExporterLambdaPolicy": aws_iam.PolicyDocument.fromJson({
120115
"Version": "2012-10-17",
121116
"Statement": [
122117
{
@@ -138,15 +133,15 @@ export class RdsSnapshotExportPipelineStack extends cdk.Stack {
138133
})
139134
},
140135
managedPolicies: [
141-
ManagedPolicy.fromAwsManagedPolicyName("service-role/AWSLambdaBasicExecutionRole"),
136+
aws_iam.ManagedPolicy.fromAwsManagedPolicyName("service-role/AWSLambdaBasicExecutionRole"),
142137
],
143138
});
144139

145-
const snapshotExportGlueCrawlerRole = new Role(this, "SnapshotExportsGlueCrawlerRole", {
146-
assumedBy: new ServicePrincipal("glue.amazonaws.com"),
140+
const snapshotExportGlueCrawlerRole = new aws_iam.Role(this, "SnapshotExportsGlueCrawlerRole", {
141+
assumedBy: new aws_iam.ServicePrincipal("glue.amazonaws.com"),
147142
description: "Role used by RDS to perform snapshot exports to S3",
148143
inlinePolicies: {
149-
"SnapshotExportsGlueCrawlerPolicy": PolicyDocument.fromJson({
144+
"SnapshotExportsGlueCrawlerPolicy": aws_iam.PolicyDocument.fromJson({
150145
"Version": "2012-10-17",
151146
"Statement": [
152147
{
@@ -161,19 +156,30 @@ export class RdsSnapshotExportPipelineStack extends cdk.Stack {
161156
}),
162157
},
163158
managedPolicies: [
164-
ManagedPolicy.fromAwsManagedPolicyName("service-role/AWSGlueServiceRole"),
159+
aws_iam.ManagedPolicy.fromAwsManagedPolicyName("service-role/AWSGlueServiceRole"),
165160
],
166161
});
167162

168-
const snapshotExportEncryptionKey = new Key(this, "SnapshotExportEncryptionKey", {
163+
const snapshotExportEncryptionKey = new aws_kms.Key(this, "SnapshotExportEncryptionKey", {
169164
alias: props.dbName + "-snapshot-exports",
170-
policy: PolicyDocument.fromJson({
165+
policy: aws_iam.PolicyDocument.fromJson({
171166
"Version": "2012-10-17",
172167
"Statement": [
173168
{
174169
"Principal": {
175170
"AWS": [
176-
(new AccountRootPrincipal()).arn,
171+
(new aws_iam.AccountRootPrincipal()).arn
172+
]
173+
},
174+
"Action": [
175+
"kms:*"
176+
],
177+
"Resource": "*",
178+
"Effect": "Allow"
179+
},
180+
{
181+
"Principal": {
182+
"AWS": [
177183
lambdaExecutionRole.roleArn,
178184
snapshotExportGlueCrawlerRole.roleArn
179185
]
@@ -186,39 +192,41 @@ export class RdsSnapshotExportPipelineStack extends cdk.Stack {
186192
"kms:DescribeKey"
187193
],
188194
"Resource": "*",
189-
"Effect": "Allow",
195+
"Effect": "Allow"
190196
},
191197
{
192-
"Principal": lambdaExecutionRole.roleArn,
198+
"Principal": {
199+
"AWS": lambdaExecutionRole.roleArn
200+
},
193201
"Action": [
194202
"kms:CreateGrant",
195203
"kms:ListGrants",
196204
"kms:RevokeGrant"
197205
],
198206
"Resource": "*",
199207
"Condition": {
200-
"Bool": {"kms:GrantIsForAWSResource": true}
208+
"Bool": { "kms:GrantIsForAWSResource": true }
201209
},
202-
"Effect": "Allow",
210+
"Effect": "Allow"
203211
}
204212
]
205213
})
206214
});
207215

208-
const snapshotEventTopic = new Topic(this, "SnapshotEventTopic", {
216+
const snapshotEventTopic = new aws_sns.Topic(this, "SnapshotEventTopic", {
209217
displayName: "rds-snapshot-creation"
210218
});
211219

212220
// Creates the appropriate RDS Event Subscription for RDS or Aurora clusters, to catch snapshot creation events
213221
props.rdsEvents.find(rdsEvent =>
214222
rdsEvent.rdsEventId == RdsEventId.DB_AUTOMATED_AURORA_SNAPSHOT_CREATED) ?
215-
new CfnEventSubscription(this, 'RdsSnapshotEventNotification', {
223+
new aws_rds.CfnEventSubscription(this, 'RdsSnapshotEventNotification', {
216224
snsTopicArn: snapshotEventTopic.topicArn,
217225
enabled: true,
218226
eventCategories: ['backup'],
219227
sourceType: 'db-cluster-snapshot',
220228
}) :
221-
new CfnEventSubscription(this, 'RdsSnapshotEventNotification', {
229+
new aws_rds.CfnEventSubscription(this, 'RdsSnapshotEventNotification', {
222230
snsTopicArn: snapshotEventTopic.topicArn,
223231
enabled: true,
224232
eventCategories: ['creation'],
@@ -230,19 +238,19 @@ export class RdsSnapshotExportPipelineStack extends cdk.Stack {
230238
// the serivce will simply copy the existing snapshot, and trigger another notification
231239
props.rdsEvents.find(rdsEvent =>
232240
rdsEvent.rdsEventId == RdsEventId.DB_BACKUP_SNAPSHOT_FINISHED_COPY) ?
233-
new CfnEventSubscription(this, 'RdsBackupCopyEventNotification', {
241+
new aws_rds.CfnEventSubscription(this, 'RdsBackupCopyEventNotification', {
234242
snsTopicArn: snapshotEventTopic.topicArn,
235243
enabled: true,
236244
eventCategories: ['notification'],
237245
sourceType: 'db-snapshot',
238246
}
239247
) : true;
240248

241-
new Function(this, "LambdaFunction", {
249+
new aws_lambda.Function(this, "LambdaFunction", {
242250
functionName: props.dbName + "-rds-snapshot-exporter",
243-
runtime: Runtime.PYTHON_3_8,
251+
runtime: aws_lambda.Runtime.PYTHON_3_8,
244252
handler: "main.handler",
245-
code: Code.fromAsset(path.join(__dirname, "/../assets/exporter/")),
253+
code: aws_lambda.Code.fromAsset(path.join(__dirname, "/../assets/exporter/")),
246254
environment: {
247255
RDS_EVENT_IDS: new Array(props.rdsEvents.map(e => { return e.rdsEventId })).join(),
248256
RDS_SNAPSHOT_TYPES: new Array(props.rdsEvents.map(e => { return e.rdsSnapshotType })).join(),
@@ -254,13 +262,13 @@ export class RdsSnapshotExportPipelineStack extends cdk.Stack {
254262
DB_SNAPSHOT_TYPES: new Array(props.rdsEvents.map(e => { return e.rdsEventId == RdsEventId.DB_AUTOMATED_AURORA_SNAPSHOT_CREATED ? "cluster-snapshot" : "snapshot" })).join()
255263
},
256264
role: lambdaExecutionRole,
257-
timeout: cdk.Duration.seconds(30),
265+
timeout: Duration.seconds(30),
258266
events: [
259-
new SnsEventSource(snapshotEventTopic)
267+
new aws_lambda_event_sources.SnsEventSource(snapshotEventTopic)
260268
]
261269
});
262270

263-
new CfnCrawler(this, "SnapshotExportCrawler", {
271+
new aws_glue.CfnCrawler(this, "SnapshotExportCrawler", {
264272
name: props.dbName + "-rds-snapshot-crawler",
265273
role: snapshotExportGlueCrawlerRole.roleArn,
266274
targets: {

0 commit comments

Comments
 (0)