-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.yaml
158 lines (145 loc) · 4.85 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
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
gtpd-monitor
Sample SAM Template for gtpd-monitor
Parameters:
WebsiteBucketName: {Type: String}
WebsiteHostname: {Type: String}
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Timeout: 30
Resources:
WebsiteBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Ref WebsiteBucketName
WebsiteConfiguration:
IndexDocument: 'index.html'
ErrorDocument: '404.html'
WebsiteBucketPolicy:
Type: AWS::S3::BucketPolicy
DependsOn: WebsiteBucket
Properties:
Bucket: !Ref WebsiteBucketName
PolicyDocument:
Version: 2012-10-17
Id: AllowCloudfrontAccess
Statement:
- Sid: AllowPublicReadAccess
Effect: Allow
Principal: '*'
Action: 's3:GetObject'
Resource: !Sub 'arn:aws:s3:::${WebsiteBucketName}/*'
- Sid: AllowPrivateAccess
Effect: Allow
Principal:
CanonicalUser: !GetAtt CfOriginAccessIdentity.S3CanonicalUserId
Action: 's3:GetObject'
Resource: !Sub 'arn:aws:s3:::${WebsiteBucketName}/*'
CfInvalidatorFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
DependsOn: CfDistribution
Properties:
CodeUri: update_function
Handler: gtpd_monitor/lambda.lambda_handler
Runtime: python3.11
Architectures:
#- x86_64
- arm64
Environment:
Variables:
WEBSITE_BUCKET_NAME: !Ref WebsiteBucketName
DISTRIBUTION_ID: !Ref CfDistribution
Policies:
- Version: '2012-10-17'
Statement:
- Effect: Allow
Action: 'cloudfront:CreateInvalidation'
Resource: !Sub 'arn:aws:cloudfront::${AWS::AccountId}:distribution/${CfDistribution}'
- Effect: Allow
Action:
- 's3:PutObject'
Resource: !Sub 'arn:aws:s3:::${WebsiteBucketName}/*'
CfInvalidatorFunctionDailyRulePermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !GetAtt CfInvalidatorFunction.Arn
Principal: 'events.amazonaws.com'
Action: 'lambda:InvokeFunction'
SourceArn: !GetAtt DailyUpdateRule.Arn
DailyUpdateRule:
Type: AWS::Events::Rule
Properties:
Name: update-gtpd-monitor-daily
Description: 'Update GTPD monitor site daily'
ScheduleExpression: 'rate(24 hours)'
Targets:
- Arn: !GetAtt CfInvalidatorFunction.Arn
Id: gtpd-monitor-lambda
CfDistribution:
Type: AWS::CloudFront::Distribution
DependsOn: WebsiteBucket
Properties:
DistributionConfig:
Aliases:
- !Ref WebsiteHostname
Enabled: true
IPV6Enabled: true
DefaultRootObject: index.html
CustomErrorResponses:
- ErrorCode: 403
ResponseCode: 404
ResponsePagePath: /404.html
DefaultCacheBehavior:
AllowedMethods: [GET, HEAD]
ForwardedValues: {QueryString: false}
TargetOriginId: s3
ViewerProtocolPolicy: redirect-to-https
ViewerCertificate:
AcmCertificateArn: !Ref CfDistributionCert
SslSupportMethod: sni-only
Origins:
- Id: s3
DomainName: !Sub '${WebsiteBucketName}.s3.amazonaws.com'
S3OriginConfig:
OriginAccessIdentity: !Sub 'origin-access-identity/cloudfront/${CfOriginAccessIdentity}'
CfDistributionCert:
Type: AWS::CertificateManager::Certificate
Properties:
DomainName: !Ref WebsiteHostname
ValidationMethod: DNS
CfOriginAccessIdentity:
Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
Properties:
CloudFrontOriginAccessIdentityConfig:
Comment: 'gtpd monitor bucket identity'
WebsiteBucketEditorPolicy:
Type: AWS::IAM::ManagedPolicy
DependsOn: WebsiteBucket
Properties:
ManagedPolicyName: GTPDMonitorEditor
Description: !Sub 'Full access to ${WebsiteBucketName} bucket'
PolicyDocument:
Version: '2012-10-17'
Statement:
- Sid: ConsoleAccess
Effect: Allow
Action:
- 's3:GetAccountPublicAccessBlock'
- 's3:GetBucketAcl'
- 's3:GetBucketLocation'
- 's3:GetBucketPolicyStatus'
- 's3:GetBucketPublicAccessBlock'
- 's3:ListAllMyBuckets'
Resource: '*'
- Sid: ListObjectsInBucket
Effect: Allow
Action: 's3:ListBucket'
Resource: !Sub 'arn:aws:s3:::${WebsiteBucketName}'
- Sid: AllObjectActions
Effect: Allow
Action: 's3:*Object'
Resource: !Sub 'arn:aws:s3:::${WebsiteBucketName}/*'
# vim:set ts=2 sw=2 et: