This repository has been archived by the owner on Apr 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathbackup.ts
74 lines (67 loc) · 2.83 KB
/
backup.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
74
import { Stack, StackProps } from 'aws-cdk-lib';
import {
BackupPlan,
BackupPlanRule,
BackupResource,
BackupSelection,
BackupVault,
TagOperation,
} from 'aws-cdk-lib/aws-backup';
import { Schedule } from 'aws-cdk-lib/aws-events';
import { Role, ServicePrincipal, PolicyDocument, PolicyStatement, Effect, ManagedPolicy } from 'aws-cdk-lib/aws-iam';
import { Key } from 'aws-cdk-lib/aws-kms';
import { Construct } from 'constructs';
export interface BackupProps extends StackProps {
backupKMSKey: Key;
}
export default class Backup extends Stack {
backupVaultWithDailyBackups: BackupVault;
backupPlanWithDailyBackups: BackupPlan;
tagBasedBackupSelection: BackupSelection;
constructor(scope: Construct, id: string, props?: BackupProps) {
super(scope, id, props);
this.backupVaultWithDailyBackups = new BackupVault(scope, 'backupVaultWithDailyBackups', {
backupVaultName: 'BackupVaultWithDailyBackups',
encryptionKey: props?.backupKMSKey,
});
this.backupPlanWithDailyBackups = new BackupPlan(scope, 'backupPlanWithDailyBackups', {
backupPlanName: 'BackupPlanWithDailyBackups',
backupPlanRules: [
new BackupPlanRule({
ruleName: 'RuleForDailyBackups',
backupVault: this.backupVaultWithDailyBackups,
scheduleExpression: Schedule.cron({
minute: '0',
hour: '5',
}),
}),
],
});
this.tagBasedBackupSelection = new BackupSelection(scope, 'tagBasedBackupSelection', {
backupSelectionName: 'TagBasedBackupSelection',
role: new Role(scope, 'BackupRole', {
assumedBy: new ServicePrincipal('backup.amazonaws.com'),
inlinePolicies: {
AssumeRolePolicyDocument: new PolicyDocument({
statements: [
new PolicyStatement({
effect: Effect.ALLOW,
actions: ['sts:AssumeRole'],
resources: ['*'],
}),
],
}),
},
managedPolicies: [
ManagedPolicy.fromAwsManagedPolicyName('service-role/AWSBackupServiceRolePolicyForBackup'),
ManagedPolicy.fromAwsManagedPolicyName('service-role/AWSBackupServiceRolePolicyForRestores'),
],
}),
resources: [
BackupResource.fromTag('backup', 'daily', TagOperation.STRING_EQUALS),
BackupResource.fromTag('fhir', 'service', TagOperation.STRING_EQUALS),
],
backupPlan: this.backupPlanWithDailyBackups,
});
}
}