-
Notifications
You must be signed in to change notification settings - Fork 1
/
quicksight-cognito-cloudformation.yml
348 lines (317 loc) · 11.3 KB
/
quicksight-cognito-cloudformation.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
Resources:
#### Lambda ####
# You can use AWS Serverless Application Model (SAM) tasks (AWS::Serverless::HttpApi, AWS::Serverless::Function) to easily create a serverless application.
# This hides some creation tasks (iam roles, logs, api gateway links, ...), this is not the goal here.
LambdaExecutionRole:
Type: AWS::IAM::Role
Properties:
RoleName: quicksight-cognito-lambda-execution-role
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Policies:
- PolicyDocument:
Version: 2012-10-17
Statement:
- Action:
- cognito-identity:GetId
- cognito-identity:GetOpenIdToken
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Effect: Allow
Resource: '*'
PolicyName: quicksight-cognito-execution-policy
QuicksightCognitoFunction:
Type: AWS::Lambda::Function
Properties:
FunctionName: quicksight-cognito-lambda
Handler: main.handler
Runtime: nodejs12.x
Role: !GetAtt LambdaExecutionRole.Arn
Code:
ZipFile: !Sub |
exports.handler = function(event, context) {
console.log('upload quicksight cognito code & update nodejs version to nodejs14.x');
};
Environment:
Variables:
COGNITO_IDENTITY_POOL_ID: !Ref CognitoIdentityPool
COGNITO_USER_POOL_URL: !Select [1, !Split ['://', !GetAtt CognitoUserPool.ProviderURL]] # Remove https:// from provider url
STS_ROLE_ARN_TO_ASSUME: !GetAtt ConnectedUserRoleToAssume.Arn
QUICKSIGHT_REGION: !Ref AWS::Region
QUICKSIGHT_DASHBOARD_ID: dashboardId
AWS_ACCOUNT_ID: !Ref AWS::AccountId
AWS_REGION: !Ref AWS::Region
LambdaApiGatewayPermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !GetAtt QuicksightCognitoFunction.Arn
Action: lambda:InvokeFunction
Principal: apigateway.amazonaws.com
LambdaLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub '/aws/lambda/${QuicksightCognitoFunction}'
RetentionInDays: 1
#### ####
#### API Gateway ####
APIGatewayHttpApi:
Type: AWS::ApiGatewayV2::Api
Properties:
Name: quicksight-cognito-api-gateway
Description: Quicksight Cognito API forward to lambda
ProtocolType: HTTP
APIGatewayLambdaIntegration:
Type: AWS::ApiGatewayV2::Integration
Properties:
ApiId: !Ref APIGatewayHttpApi
IntegrationType: AWS_PROXY
PayloadFormatVersion: 2.0
IntegrationUri: !Sub 'arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${QuicksightCognitoFunction.Arn}/invocations'
RequestParameters:
'overwrite:path': '$request.path' # Remove any stage name from path
APIGatewayAuthorizer:
Type: AWS::ApiGatewayV2::Authorizer
Properties:
ApiId: !Ref APIGatewayHttpApi
AuthorizerType: JWT
IdentitySource:
- "$request.header.Authorization"
JwtConfiguration:
Audience:
- !Ref CognitoUserPoolClient
Issuer: !Sub 'https://cognito-idp.${AWS::Region}.amazonaws.com/${CognitoUserPool}'
Name: JwtCognitoAuthorizer
APIGatewayGetLambdaProxyRoute:
Type: AWS::ApiGatewayV2::Route
Properties:
ApiId: !Ref APIGatewayHttpApi
RouteKey: 'GET /quicksight-cognito/{proxy+}'
AuthorizationType: JWT
AuthorizerId: !Ref APIGatewayAuthorizer
Target: !Sub 'integrations/${APIGatewayLambdaIntegration}'
APIGatewayOptionsLambdaProxyRoute:
Type: AWS::ApiGatewayV2::Route
Properties:
ApiId: !Ref APIGatewayHttpApi
RouteKey: 'OPTIONS /quicksight-cognito/{proxy+}'
Target: !Sub 'integrations/${APIGatewayLambdaIntegration}'
APIGatewayLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: api-gateway-log-grp
RetentionInDays: 1
APIGatewayDevStage:
Type: AWS::ApiGatewayV2::Stage
Properties:
StageName: Dev
Description: Dev Stage
AccessLogSettings:
DestinationArn: !GetAtt APIGatewayLogGroup.Arn
Format: '{ "requestId":"$context.requestId", "ip": "$context.identity.sourceIp", "requestTime":"$context.requestTime", "httpMethod":"$context.httpMethod","routeKey":"$context.routeKey", "status":"$context.status","protocol":"$context.protocol", "responseLength":"$context.responseLength", "integrationError":"$context.integrationErrorMessage" }'
AutoDeploy: true
ApiId: !Ref APIGatewayHttpApi
#### ####
#### Cognito ####
CognitoUserPool:
Type: AWS::Cognito::UserPool
Properties:
UserPoolName: quicksight-cognito-user-pool
CognitoUserPoolClient:
Type: AWS::Cognito::UserPoolClient
Properties:
UserPoolId: !Ref CognitoUserPool
ClientName: quicksight-cognito-web-app
AccessTokenValidity: 1 # eq 1 hour
IdTokenValidity: 1 # eq 1 hour
RefreshTokenValidity: 1 # eq 1 day
AllowedOAuthFlowsUserPoolClient: false
SupportedIdentityProviders:
- COGNITO
CognitoIdentityPool:
Type: AWS::Cognito::IdentityPool
Properties:
AllowUnauthenticatedIdentities: false
IdentityPoolName: quicksight-cognito-identity-pool
CognitoIdentityProviders:
- ProviderName: !GetAtt CognitoUserPool.ProviderName
ClientId: !Ref CognitoUserPoolClient
CognitoIdentityPoolRoleAttachment:
Type: AWS::Cognito::IdentityPoolRoleAttachment
Properties:
IdentityPoolId: !Ref CognitoIdentityPool
Roles:
"authenticated": !GetAtt CognitoAuthenticatedRole.Arn
"unauthenticated": !GetAtt CognitoUnAuthenticatedRole.Arn
CognitoAuthenticatedRole:
Type: AWS::IAM::Role
Properties:
RoleName: cognito-auth-role
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Federated:
- cognito-identity.amazonaws.com
Action:
- sts:AssumeRoleWithWebIdentity
Condition: { "ForAnyValue:StringLike": { "cognito-identity.amazonaws.com:amr": "authenticated" }, "StringEquals": { "cognito-identity.amazonaws.com:aud": !Ref CognitoIdentityPool } }
Policies:
- PolicyDocument:
Version: 2012-10-17
Statement:
- Action:
- mobileanalytics:PutEvents
- cognito-sync:*
- cognito-identity:*
Effect: Allow
Resource: '*'
PolicyName: cognito-auth-policy
CognitoUnAuthenticatedRole:
Type: AWS::IAM::Role
Properties:
RoleName: cognito-unauth-role
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Federated:
- cognito-identity.amazonaws.com
Action:
- sts:AssumeRoleWithWebIdentity
Condition: { "ForAnyValue:StringLike": { "cognito-identity.amazonaws.com:amr": "unauthenticated" }, "StringEquals": { "cognito-identity.amazonaws.com:aud": !Ref CognitoIdentityPool } }
#### ####
#### Quicksight ####
ConnectedUserRoleToAssume:
Type: AWS::IAM::Role
Properties:
RoleName: cognito-quicksight-assumed-role
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Federated:
- cognito-identity.amazonaws.com
Action:
- sts:AssumeRoleWithWebIdentity
Policies:
- PolicyDocument:
Version: 2012-10-17
Statement:
- Action:
- quicksight:RegisterUser
- quicksight:GetDashboardEmbedUrl
Effect: Allow
Resource: '*'
PolicyName: cognito-quicksight-assumed-policy
# Add quicksight create account + quicksight create analysis (AWS::QuickSight::Analysis) + quicksight create dashboard (AWS::QuickSight::Dashboard) when it's possible with CloudFormation
#### ####
#### S3 Website ####
WebAppS3Bucket:
Type: AWS::S3::Bucket
Properties:
AccessControl: Private
BucketName: quicksight-cognito-web-app-s3
WebAppS3BucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref WebAppS3Bucket
PolicyDocument:
Version: 2012-10-17
Statement:
- Action:
- s3:GetObject
Principal:
CanonicalUser: !GetAtt WebAppAccessIdentity.S3CanonicalUserId
Effect: Allow
Resource: !Sub '${WebAppS3Bucket.Arn}/*'
WebAppAccessIdentity:
Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
Properties:
CloudFrontOriginAccessIdentityConfig:
Comment: quicksight-cognito-web-app-cloudfront-access-identity
WebAppDistribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Origins:
- DomainName: !GetAtt WebAppS3Bucket.RegionalDomainName
Id: quicksightCognitoWebAppBucket
S3OriginConfig:
OriginAccessIdentity: !Sub 'origin-access-identity/cloudfront/${WebAppAccessIdentity.Id}'
Enabled: true
DefaultRootObject: index.html
HttpVersion: http2
CustomErrorResponses:
- ErrorCode: 403
ResponseCode: 200
ResponsePagePath: /index.html
DefaultCacheBehavior:
AllowedMethods:
- GET
- HEAD
Compress: true
TargetOriginId: quicksightCognitoWebAppBucket
ForwardedValues:
QueryString: false
Cookies:
Forward: none
ViewerProtocolPolicy: redirect-to-https
PriceClass: PriceClass_100
#### ####
#### CI/CD User to deploy Web App / Lambda files #####
IAMGroup:
Type: AWS::IAM::Group
Properties:
GroupName: quicksight-cognito-operators-grp
IAMUser:
Type: AWS::IAM::User
Properties:
UserName: ci-user
Groups:
- !Ref IAMGroup
IAMUserAccessKey:
Type: AWS::IAM::AccessKey
Properties:
Status: Active
UserName: !Ref IAMUser
FilesUploadPolicy:
Type: AWS::IAM::Policy
Properties:
PolicyName: quicksight-cognito-upload-code-policy
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- s3:ListBucket
Resource: !GetAtt WebAppS3Bucket.Arn
- Effect: Allow
Action:
- s3:PutObject
- s3:DeleteObject
Resource: !Sub '${WebAppS3Bucket.Arn}/*'
- Effect: Allow
Action:
- lambda:UpdateFunctionCode
Resource: !GetAtt QuicksightCognitoFunction.Arn
Groups:
- !Ref IAMGroup
#### ####
Outputs:
WebAppCloudFrontUrl:
Description: Web App Domain name to add to Quicksight Domains & Integration
Value: !Sub 'https://${WebAppDistribution.DomainName}'
CIUserAccessKey:
Description: CI User Access Information
Value: !Sub 'Access Key: ${IAMUserAccessKey}, Secret Key: ${IAMUserAccessKey.SecretAccessKey}' # Be careful with credentials in Cloudformation output :)