-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathcodecommit_backup_cfn_template.yaml
174 lines (173 loc) · 5.14 KB
/
codecommit_backup_cfn_template.yaml
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# Copyright 2012-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Parameters:
CodeCommitBackupsScriptsS3Bucket:
Type: "String"
Description: "CodeCommit backup scripts"
CodeCommitBackupsS3Bucket:
Type: "String"
Description: "S3 Bucket for CodeCommit repository backups"
BackupSchedule:
Type: "String"
Description: "Backup schedule as a cron expression"
Default: "cron(0 2 * * ? *)"
BackupScriptsFile:
Type: "String"
Description: "Compressed S3 file containing backup scripts"
Default: "codecommit_backup_scripts.zip"
Resources:
CodeCommitBackupLambdaRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Principal:
Service:
- "lambda.amazonaws.com"
Action:
- "sts:AssumeRole"
Path: "/"
Policies:
-
PolicyName: "lambdaexecute"
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- "logs:*"
Resource: "arn:aws:logs:*:*:*"
-
PolicyName: "codebuild"
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- "codebuild:StartBuild"
Resource: !GetAtt CodeBuildProject.Arn
CodeCommitBackupLambda:
Type: AWS::Serverless::Function
Properties:
FunctionName: CodeCommitBackupLambda
Handler: codecommit_backup_trigger_lambda.handler
Runtime: python3.6
MemorySize: 128
Timeout: 30
Role: !GetAtt CodeCommitBackupLambdaRole.Arn
Tags:
Name: CodeCommitBackup
CodeCommitBackupScheduledRule:
Type: "AWS::Events::Rule"
Properties:
Description: "Scheduled rule for CodeCommit backups"
ScheduleExpression: !Ref BackupSchedule
State: "ENABLED"
Targets:
-
Arn:
Fn::GetAtt:
- "CodeCommitBackupLambda"
- "Arn"
Id: "CodeCommitBackupLambda"
PermissionForEventsToInvokeLambda:
Type: "AWS::Lambda::Permission"
Properties:
FunctionName:
Ref: "CodeCommitBackupLambda"
Action: "lambda:InvokeFunction"
Principal: "events.amazonaws.com"
CodeBuildProjectRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Principal:
Service:
- "codebuild.amazonaws.com"
Action:
- "sts:AssumeRole"
Path: "/"
Policies:
-
PolicyName: "codecommit-readonly"
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- "codecommit:BatchGet*"
- "codecommit:Get*"
- "codecommit:Describe*"
- "codecommit:List*"
- "codecommit:GitPull"
Resource: "*"
-
PolicyName: "logs"
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- "logs:CreateLogGroup"
- "logs:CreateLogStream"
- "logs:PutLogEvents"
Resource: "*"
-
PolicyName: "s3-artifacts"
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- "s3:GetObject"
- "s3:GetObjectVersion"
Resource:
- !Sub "arn:aws:s3:::${CodeCommitBackupsScriptsS3Bucket}/*"
-
PolicyName: "s3-backup"
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- "s3:putObject"
Resource:
- !Sub "arn:aws:s3:::${CodeCommitBackupsS3Bucket}/*"
CodeBuildProject:
Type: AWS::CodeBuild::Project
Properties:
Name: CodeCommitBackup
Description: CodeBuild will backup all CodeCommit repo in this region
ServiceRole: !GetAtt CodeBuildProjectRole.Arn
Artifacts:
Type: no_artifacts
Environment:
Type: LINUX_CONTAINER
ComputeType: BUILD_GENERAL1_MEDIUM
Image: aws/codebuild/python:3.5.2
EnvironmentVariables:
-
Name: CodeCommitBackupsS3Bucket
Value: !Ref CodeCommitBackupsS3Bucket
Source:
Location: !Sub "arn:aws:s3:::${CodeCommitBackupsScriptsS3Bucket}/${BackupScriptsFile}"
Type: S3
TimeoutInMinutes: 60
Tags:
- Key: Name
Value: CodeCommitBackup