-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcodecommit_backup_cfn_template.yaml
148 lines (147 loc) · 4.63 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
# Copyright 2012-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Amazon Software License (the "License").
# You may not use this file except in compliance with the License.
# A copy of the License is located at
#
# http://aws.amazon.com/asl/
#
# or in the "license" file accompanying this file. This file is distributed
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied. See the License for the specific language governing
# permissions and limitations under the License.
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: "/"
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AWSLambdaExecute
Policies:
-
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
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: "/"
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
- arn:aws:iam::aws:policy/AWSCodeCommitReadOnly
Policies:
-
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