-
Notifications
You must be signed in to change notification settings - Fork 2
/
cf_stack.yml
229 lines (211 loc) · 6.75 KB
/
cf_stack.yml
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
---
AWSTemplateFormatVersion: "2010-09-09"
Description: "Static Site Generation Stack"
Parameters:
#Domain name used for your static site
DomainName:
Type: String
MinLength: 4
MaxLength: 253
Default: "blog.fxaugury.com"
AllowedPattern: "[a-z0-9]+[-.a-z0-9]*(\\.[a-z][a-z]+)+"
#CodeCommit repo
ExistingGitRepository:
Description: "Git repository name for CodeCommit repository."
Type: String
Default: ""
#CodePipeline source code bucket
ExistingCodePipelineBucket:
Description: "Name of CodePipeline artifact bucket. Leave empty and it will be created for you."
Type: String
Default: ""
#Lambda parameters
GeneratorLambdaFunctionRuntime:
Type: String
Description: "Runtime language of Static Site Generator AWS Lambda function"
Default: "python3.6"
AllowedValues:
- "python3.6"
- "nodejs"
- "java8"
GeneratorLambdaFunctionHandler:
Type: String
Description: "Function Handler for AWS Lambda function"
Default: "generate_static_site.handler"
#S3 buckets
ExistingSiteBucket:
Description: "Name of website bucket."
Type: String
Default: "blog.fxaugury.com"
GeneratorLambdaFunctionS3Bucket:
Type: String
Description: "S3 bucket containing ZIP of AWS Lambda function"
Default: ""
GeneratorLambdaFunctionS3Key:
Type: String
Description: "S3 key containing ZIP of AWS Lambda function"
Default: "generate_static_site.zip"
Conditions:
RequiresCodePipelineBucket: !Equals [!Ref ExistingCodePipelineBucket, ""]
Resources:
#S3 temporary storage for CodeCommit source code, remove after 24 hours daily
CodePipelineBucket:
Condition: RequiresCodePipelineBucket
Type: "AWS::S3::Bucket"
Properties:
BucketName: !Sub "src.${DomainName}"
LifecycleConfiguration:
Rules:
ExpirationInDays: 1
DeletionPolicy: Delete
#Allow Lambda access to; List, upload, get, delete, put object or acl on S3 and Set job results on CodePipeline
LambdaExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
Policies:
- PolicyName: !Sub "${DomainName}-execution-policy"
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action: "logs:*"
Resource: "arn:aws:logs:*:*:*"
- Effect: Allow
Action:
- codepipeline:PutJobSuccessResult
- codepipeline:PutJobFailureResult
Resource: "*"
- Effect: Allow
Action:
- s3:GetBucketLocation
- s3:ListBucket
- s3:ListBucketMultipartUploads
- s3:AbortMultipartUpload
- s3:DeleteObject
- s3:GetObject
- s3:GetObjectAcl
- s3:ListMultipartUploadParts
- s3:PutObject
- s3:PutObjectAcl
Resource:
- !Join ["", ["arn:aws:s3:::", !Ref ExistingSiteBucket, "/*"]]
- !Join ["", ["arn:aws:s3:::", !Ref ExistingCodePipelineBucket, "/*"]]
GeneratorLambdaFunction:
Type: "AWS::Lambda::Function"
Properties:
Description: !Sub "Static site generator for ${DomainName}"
Role: !GetAtt LambdaExecutionRole.Arn
MemorySize: 256
Timeout: 300
Runtime: !Ref GeneratorLambdaFunctionRuntime
Handler: !Ref GeneratorLambdaFunctionHandler
Code:
S3Bucket: !Ref GeneratorLambdaFunctionS3Bucket
S3Key: !Ref GeneratorLambdaFunctionS3Key
Environment:
Variables:
SiteBucket: !Ref ExistingSiteBucket
# Allow all actions on Lambda and CodePipeline
CodePipelineRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- "lambda.amazonaws.com"
- "codepipeline.amazonaws.com"
Action:
- "sts:AssumeRole"
Path: "/"
Policies:
- PolicyName: "codepipeline-service"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action: "*"
Resource: "*"
# Fetch code from Master branche, execute Lambda function to generate, compress and deploy static site.
CodePipeline:
Type: "AWS::CodePipeline::Pipeline"
Properties:
Name: !Sub "${DomainName}-codepipeline"
ArtifactStore:
Type: S3
Location: !Ref ExistingCodePipelineBucket
RestartExecutionOnUpdate: false
RoleArn: !Sub "arn:aws:iam::${AWS::AccountId}:role/${CodePipelineRole}"
Stages:
- Name: Source
Actions:
- Name: SourceAction
ActionTypeId:
Category: Source
Owner: AWS
Provider: CodeCommit
Version: 1
Configuration:
RepositoryName: !Ref ExistingGitRepository
BranchName: master
OutputArtifacts:
- Name: SiteSource
RunOrder: 1
- Name: InvokeGenerator
Actions:
- Name: InvokeAction
InputArtifacts:
- Name: SiteSource
ActionTypeId:
Category: Invoke
Owner: AWS
Provider: Lambda
Version: 1
Configuration:
FunctionName: !Ref GeneratorLambdaFunction
OutputArtifacts:
- Name: SiteContent
RunOrder: 1
Outputs:
CodePipelineArn:
Description: CodePipeline ARN
Value: !Ref CodePipeline
CodePipelineS3Bucket:
Description: CodePipeline S3 Bucket
Value: !Ref ExistingCodePipelineBucket
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: Website domain
Parameters:
- DomainName
- Label:
default: CodeCommit
Parameters:
- NotificationEmail
- ExistingGitRepository
- Label:
default: AWS Lambda Static site generation function
Parameters:
- GeneratorLambdaFunctionS3Bucket
- GeneratorLambdaFunctionS3Key
- GeneratorLambdaFunctionRuntime
- GeneratorLambdaFunctionHandler
- Label:
default: S3 site and code buckets
Parameters:
- ExistingSiteBucket
- ExistingCodePipelineBucket