forked from HoraceShmorace/Image-Flex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.yaml
196 lines (181 loc) · 5.99 KB
/
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Resources:
EdgeLambdaRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
- edgelambda.amazonaws.com
Action:
- sts:AssumeRole
Path: /service-role/
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
- arn:aws:iam::aws:policy/AWSXrayFullAccess
Policies:
-
PolicyName: !Sub ${AWS::StackName}-images-Lambda-S3
PolicyDocument:
Version: 2012-10-17
Statement:
Effect: Allow
Action:
- s3:*
Resource:
- !Sub arn:aws:s3:::${AWS::StackName}-images
- !Sub arn:aws:s3:::${AWS::StackName}-images/*
OriginAccessId:
Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
Properties:
CloudFrontOriginAccessIdentityConfig:
Comment: !Sub ${AWS::StackName}-OriginAccessId
ImageBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub ${AWS::StackName}-images
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
CorsConfiguration:
CorsRules:
- AllowedOrigins:
- "*"
AllowedMethods:
- "GET"
AllowedHeaders:
- "*"
ImageBucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref ImageBucket
PolicyDocument:
Id: !Sub ${AWS::StackName}-images-policy
Version: 2012-10-17
Statement:
-
Action:
- s3:GetObject
Effect: Allow
Resource: !Sub arn:aws:s3:::${AWS::StackName}-images/*
Principal:
AWS: !Sub arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity ${OriginAccessId}
CloudFrontLoggingBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub ${AWS::StackName}-cflogs
PublicAccessBlockConfiguration:
BlockPublicAcls: Yes
BlockPublicPolicy: Yes
IgnorePublicAcls: Yes
RestrictPublicBuckets: Yes
UriToS3KeyFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-UriToS3Key
AutoPublishAlias: live
CodeUri: src/UriToS3Key/
Handler: index.handler
Runtime: nodejs12.x
MemorySize: 128
Timeout: 1
Role: !GetAtt EdgeLambdaRole.Arn
GetOrCreateImageFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-GetOrCreateImage
AutoPublishAlias: live
CodeUri: src/GetOrCreateImage/
Handler: index.handler
Runtime: nodejs12.x
MemorySize: 1024
Timeout: 30
Role: !GetAtt EdgeLambdaRole.Arn
WebACL:
Type: AWS::WAFv2::WebACL
Properties:
DefaultAction:
Allow: {}
Name: !Sub ${AWS::StackName}-WebAcl
Rules:
- Name: AWS-AWSManagedRulesCommonRuleSet
Priority: 0
OverrideAction:
None: {}
VisibilityConfig:
SampledRequestsEnabled: true
CloudWatchMetricsEnabled: true
MetricName: !Sub ${AWS::StackName}-MetricForAMRCRS
Statement:
ManagedRuleGroupStatement:
VendorName: AWS
Name: AWSManagedRulesCommonRuleSet
ExcludedRules:
- Name: NoUserAgent_HEADER
Scope: CLOUDFRONT
VisibilityConfig:
SampledRequestsEnabled: true
CloudWatchMetricsEnabled: true
MetricName: !Sub ${AWS::StackName}-WebAcl
Distribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
# Uncomment the next two lines to use a custom CNAME (must be configured in Route 53, or your DNS provider).
# Aliases:
# - YOUR CNAME HERE
ViewerCertificate:
CloudFrontDefaultCertificate: true
# Replace the above line with the following to indicate your own certificate to use for HTTPS
# CloudFrontDefaultCertificate: false
# AcmCertificateArn: YOUR CERTIFICATE MANAGER ARN HERE
# SslSupportMethod: sni-only
Comment: !Sub Distribution for ${AWS::StackName}
DefaultCacheBehavior:
Compress: true
ForwardedValues:
QueryString: true
QueryStringCacheKeys:
- w
- h
Cookies:
Forward: none
Headers:
- Access-Control-Request-Method
- Access-Control-Request-Headers
- Origin
LambdaFunctionAssociations:
- EventType: viewer-request
LambdaFunctionARN: !Ref UriToS3KeyFunction.Version
- EventType: origin-response
LambdaFunctionARN: !Ref GetOrCreateImageFunction.Version
MinTTL: 100
TargetOriginId: !Sub ${AWS::StackName}-images
ViewerProtocolPolicy: allow-all
Enabled: true
Logging:
Bucket: !GetAtt CloudFrontLoggingBucket.DomainName
Origins:
- DomainName: !Sub ${AWS::StackName}-images.s3.amazonaws.com
Id: !Sub ${AWS::StackName}-images
S3OriginConfig:
OriginAccessIdentity: !Sub origin-access-identity/cloudfront/${OriginAccessId}
PriceClass: PriceClass_All
WebACLId: !GetAtt WebACL.Arn
Outputs:
DistributionDomain:
Value: !GetAtt Distribution.DomainName
Description: Cloudfront distribution domain.
UriToS3KeyFunction:
Value: !Ref UriToS3KeyFunction
Description: Lambda function for the Cloudfront viewer-request event.
GetOrCreateImageFunction:
Value: !Ref GetOrCreateImageFunction
Description: Lambda function for the Cloudfront origin-response event.