-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.yml
162 lines (152 loc) · 4.79 KB
/
template.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
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Parameters:
CognitoUserPoolArn:
Description: "Userpool which must be auth'd against"
Type: String
Globals:
Api:
# Apply these (generous) Cors settings to all endpoints:
Cors:
AllowMethods: "'*'"
AllowOrigin: "'*'"
AllowHeaders: "'Authorization'"
Auth:
DefaultAuthorizer: CognitoAuth
# Don't authenticate Cors pre-flight requests:
AddDefaultAuthorizerToCorsPreflight: false
Authorizers:
CognitoAuth:
UserPoolArn: !Ref CognitoUserPoolArn
# We need to make sure Cors happens even on error responses:
GatewayResponses:
DEFAULT_4XX:
ResponseTemplates:
"application/json": '{ "message": "access denied!" }'
ResponseParameters:
Headers:
Access-Control-Allow-Origin: "'*'"
Access-Control-Allow-Methods: "'*'"
Access-Control-Allow-Headers: "'*'"
Resources:
EdgeAuthFunction:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs10.x
CodeUri: edge/function.zip
Role: !Sub "${EdgeExecutionRole.Arn}"
Timeout: 3
AutoPublishAlias: latest
BackendFunction:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs8.10
Events:
hello:
Type: Api
Properties:
Path: /helloWorld
Method: get
InlineCode: |
exports.handler = async (event) => {
console.log(JSON.stringify(event, null, 2));
let response = {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "*",
"Access-Control-Allow-Methods": "GET"
},
body: "Hello " + event.requestContext.authorizer.claims.name + "!"
}
return response
}
EdgeExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service:
- "edgelambda.amazonaws.com"
- "lambda.amazonaws.com"
Action:
- "sts:AssumeRole"
Policies:
- PolicyName: "logs"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action: "logs:CreateLogGroup"
Resource: "*"
- Effect: "Allow"
Action: [ "logs:CreateLogStream", "logs:PutLogEvents" ]
Resource: "*"
WebsiteBucket:
Type: AWS::S3::Bucket
Properties:
AccessControl: Private
WebsiteBucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref WebsiteBucket
PolicyDocument:
Statement:
- Action:
- "s3:GetObject"
Effect: "Allow"
Resource: !Sub "arn:aws:s3:::${WebsiteBucket}/*"
Principal:
CanonicalUser: !Sub "${OriginAccessIdentity.S3CanonicalUserId}"
CloudFrontDistribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
DefaultCacheBehavior:
TargetOriginId: "S3-authWebsite"
ViewerProtocolPolicy: "https-only"
LambdaFunctionAssociations:
- EventType: viewer-request
LambdaFunctionARN: !Sub "${EdgeAuthFunction.Version}"
ForwardedValues:
Cookies:
Forward: "none"
QueryString: false
# These CustomErrorResponses are required to make React Router function:
CustomErrorResponses:
- ErrorCode: 403
ResponseCode: 200
ResponsePagePath: "/index.html"
- ErrorCode: 404
ResponseCode: 200
ResponsePagePath: "/index.html"
DefaultRootObject: "/index.html"
Enabled: true
Origins:
- DomainName: !Sub "${WebsiteBucket}.s3.amazonaws.com"
Id: "S3-authWebsite"
S3OriginConfig:
OriginAccessIdentity: !Sub "origin-access-identity/cloudfront/${OriginAccessIdentity}"
OriginAccessIdentity:
Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
Properties:
CloudFrontOriginAccessIdentityConfig:
Comment: "Allow CloudFront access"
Outputs:
ApiEndpoint:
Description: Backend API Endpoint
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/"
BucketName:
Description: S3 Bucket Name
Value: !Ref WebsiteBucket
DomainName:
Description: Cloudfront Domain Name
Value: !Sub "${CloudFrontDistribution.DomainName}"
CloudFrontDistributionId:
Description: Cloudfront Domain Name
Value: !Ref CloudFrontDistribution