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' ;
2
3
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' ;
11
6
12
7
export enum RdsEventId {
13
8
/**
@@ -58,7 +53,7 @@ export interface RdsSnapshot {
58
53
rdsSnapshotType : RdsSnapshotType ;
59
54
}
60
55
61
- export interface RdsSnapshotExportPipelineStackProps extends cdk . StackProps {
56
+ export interface RdsSnapshotExportPipelineStackProps extends StackProps {
62
57
/**
63
58
* Name of the S3 bucket to which snapshot exports should be saved.
64
59
*
@@ -77,20 +72,20 @@ export interface RdsSnapshotExportPipelineStackProps extends cdk.StackProps {
77
72
readonly rdsEvents : Array < RdsSnapshot > ;
78
73
} ;
79
74
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 ) {
82
77
super ( scope , id , props ) ;
83
78
84
- const bucket = new Bucket ( this , "SnapshotExportBucket" , {
79
+ const bucket = new aws_s3 . Bucket ( this , "SnapshotExportBucket" , {
85
80
bucketName : props . s3BucketName ,
86
- blockPublicAccess : BlockPublicAccess . BLOCK_ALL ,
81
+ blockPublicAccess : aws_s3 . BlockPublicAccess . BLOCK_ALL ,
87
82
} ) ;
88
83
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" ) ,
91
86
description : "Role used by RDS to perform snapshot exports to S3" ,
92
87
inlinePolicies : {
93
- "SnapshotExportTaskPolicy" : PolicyDocument . fromJson ( {
88
+ "SnapshotExportTaskPolicy" : aws_iam . PolicyDocument . fromJson ( {
94
89
"Version" : "2012-10-17" ,
95
90
"Statement" : [
96
91
{
@@ -112,11 +107,11 @@ export class RdsSnapshotExportPipelineStack extends cdk.Stack {
112
107
}
113
108
} ) ;
114
109
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" ) ,
117
112
description : 'RdsSnapshotExportToS3 Lambda execution role for the "' + props . dbName + '" database.' ,
118
113
inlinePolicies : {
119
- "SnapshotExporterLambdaPolicy" : PolicyDocument . fromJson ( {
114
+ "SnapshotExporterLambdaPolicy" : aws_iam . PolicyDocument . fromJson ( {
120
115
"Version" : "2012-10-17" ,
121
116
"Statement" : [
122
117
{
@@ -138,15 +133,15 @@ export class RdsSnapshotExportPipelineStack extends cdk.Stack {
138
133
} )
139
134
} ,
140
135
managedPolicies : [
141
- ManagedPolicy . fromAwsManagedPolicyName ( "service-role/AWSLambdaBasicExecutionRole" ) ,
136
+ aws_iam . ManagedPolicy . fromAwsManagedPolicyName ( "service-role/AWSLambdaBasicExecutionRole" ) ,
142
137
] ,
143
138
} ) ;
144
139
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" ) ,
147
142
description : "Role used by RDS to perform snapshot exports to S3" ,
148
143
inlinePolicies : {
149
- "SnapshotExportsGlueCrawlerPolicy" : PolicyDocument . fromJson ( {
144
+ "SnapshotExportsGlueCrawlerPolicy" : aws_iam . PolicyDocument . fromJson ( {
150
145
"Version" : "2012-10-17" ,
151
146
"Statement" : [
152
147
{
@@ -161,19 +156,30 @@ export class RdsSnapshotExportPipelineStack extends cdk.Stack {
161
156
} ) ,
162
157
} ,
163
158
managedPolicies : [
164
- ManagedPolicy . fromAwsManagedPolicyName ( "service-role/AWSGlueServiceRole" ) ,
159
+ aws_iam . ManagedPolicy . fromAwsManagedPolicyName ( "service-role/AWSGlueServiceRole" ) ,
165
160
] ,
166
161
} ) ;
167
162
168
- const snapshotExportEncryptionKey = new Key ( this , "SnapshotExportEncryptionKey" , {
163
+ const snapshotExportEncryptionKey = new aws_kms . Key ( this , "SnapshotExportEncryptionKey" , {
169
164
alias : props . dbName + "-snapshot-exports" ,
170
- policy : PolicyDocument . fromJson ( {
165
+ policy : aws_iam . PolicyDocument . fromJson ( {
171
166
"Version" : "2012-10-17" ,
172
167
"Statement" : [
173
168
{
174
169
"Principal" : {
175
170
"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" : [
177
183
lambdaExecutionRole . roleArn ,
178
184
snapshotExportGlueCrawlerRole . roleArn
179
185
]
@@ -186,39 +192,41 @@ export class RdsSnapshotExportPipelineStack extends cdk.Stack {
186
192
"kms:DescribeKey"
187
193
] ,
188
194
"Resource" : "*" ,
189
- "Effect" : "Allow" ,
195
+ "Effect" : "Allow"
190
196
} ,
191
197
{
192
- "Principal" : lambdaExecutionRole . roleArn ,
198
+ "Principal" : {
199
+ "AWS" : lambdaExecutionRole . roleArn
200
+ } ,
193
201
"Action" : [
194
202
"kms:CreateGrant" ,
195
203
"kms:ListGrants" ,
196
204
"kms:RevokeGrant"
197
205
] ,
198
206
"Resource" : "*" ,
199
207
"Condition" : {
200
- "Bool" : { "kms:GrantIsForAWSResource" : true }
208
+ "Bool" : { "kms:GrantIsForAWSResource" : true }
201
209
} ,
202
- "Effect" : "Allow" ,
210
+ "Effect" : "Allow"
203
211
}
204
212
]
205
213
} )
206
214
} ) ;
207
215
208
- const snapshotEventTopic = new Topic ( this , "SnapshotEventTopic" , {
216
+ const snapshotEventTopic = new aws_sns . Topic ( this , "SnapshotEventTopic" , {
209
217
displayName : "rds-snapshot-creation"
210
218
} ) ;
211
219
212
220
// Creates the appropriate RDS Event Subscription for RDS or Aurora clusters, to catch snapshot creation events
213
221
props . rdsEvents . find ( rdsEvent =>
214
222
rdsEvent . rdsEventId == RdsEventId . DB_AUTOMATED_AURORA_SNAPSHOT_CREATED ) ?
215
- new CfnEventSubscription ( this , 'RdsSnapshotEventNotification' , {
223
+ new aws_rds . CfnEventSubscription ( this , 'RdsSnapshotEventNotification' , {
216
224
snsTopicArn : snapshotEventTopic . topicArn ,
217
225
enabled : true ,
218
226
eventCategories : [ 'backup' ] ,
219
227
sourceType : 'db-cluster-snapshot' ,
220
228
} ) :
221
- new CfnEventSubscription ( this , 'RdsSnapshotEventNotification' , {
229
+ new aws_rds . CfnEventSubscription ( this , 'RdsSnapshotEventNotification' , {
222
230
snsTopicArn : snapshotEventTopic . topicArn ,
223
231
enabled : true ,
224
232
eventCategories : [ 'creation' ] ,
@@ -230,19 +238,19 @@ export class RdsSnapshotExportPipelineStack extends cdk.Stack {
230
238
// the serivce will simply copy the existing snapshot, and trigger another notification
231
239
props . rdsEvents . find ( rdsEvent =>
232
240
rdsEvent . rdsEventId == RdsEventId . DB_BACKUP_SNAPSHOT_FINISHED_COPY ) ?
233
- new CfnEventSubscription ( this , 'RdsBackupCopyEventNotification' , {
241
+ new aws_rds . CfnEventSubscription ( this , 'RdsBackupCopyEventNotification' , {
234
242
snsTopicArn : snapshotEventTopic . topicArn ,
235
243
enabled : true ,
236
244
eventCategories : [ 'notification' ] ,
237
245
sourceType : 'db-snapshot' ,
238
246
}
239
247
) : true ;
240
248
241
- new Function ( this , "LambdaFunction" , {
249
+ new aws_lambda . Function ( this , "LambdaFunction" , {
242
250
functionName : props . dbName + "-rds-snapshot-exporter" ,
243
- runtime : Runtime . PYTHON_3_8 ,
251
+ runtime : aws_lambda . Runtime . PYTHON_3_8 ,
244
252
handler : "main.handler" ,
245
- code : Code . fromAsset ( path . join ( __dirname , "/../assets/exporter/" ) ) ,
253
+ code : aws_lambda . Code . fromAsset ( path . join ( __dirname , "/../assets/exporter/" ) ) ,
246
254
environment : {
247
255
RDS_EVENT_IDS : new Array ( props . rdsEvents . map ( e => { return e . rdsEventId } ) ) . join ( ) ,
248
256
RDS_SNAPSHOT_TYPES : new Array ( props . rdsEvents . map ( e => { return e . rdsSnapshotType } ) ) . join ( ) ,
@@ -254,13 +262,13 @@ export class RdsSnapshotExportPipelineStack extends cdk.Stack {
254
262
DB_SNAPSHOT_TYPES : new Array ( props . rdsEvents . map ( e => { return e . rdsEventId == RdsEventId . DB_AUTOMATED_AURORA_SNAPSHOT_CREATED ? "cluster-snapshot" : "snapshot" } ) ) . join ( )
255
263
} ,
256
264
role : lambdaExecutionRole ,
257
- timeout : cdk . Duration . seconds ( 30 ) ,
265
+ timeout : Duration . seconds ( 30 ) ,
258
266
events : [
259
- new SnsEventSource ( snapshotEventTopic )
267
+ new aws_lambda_event_sources . SnsEventSource ( snapshotEventTopic )
260
268
]
261
269
} ) ;
262
270
263
- new CfnCrawler ( this , "SnapshotExportCrawler" , {
271
+ new aws_glue . CfnCrawler ( this , "SnapshotExportCrawler" , {
264
272
name : props . dbName + "-rds-snapshot-crawler" ,
265
273
role : snapshotExportGlueCrawlerRole . roleArn ,
266
274
targets : {
0 commit comments