-
Notifications
You must be signed in to change notification settings - Fork 1
/
template.yaml
327 lines (281 loc) · 11.3 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
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
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
Amazon SageMaker SAML backend. This stack creates a SAML backend API and Lambda functions to suppport a custom SAML application
Metadata:
AWS::ServerlessRepo::Application:
Name: amazon-sagemaker-saml-backend
Description: Custom SAML application backend for Amazon SageMaker Studio
Author: ilyiny
SpdxLicenseId: MIT
LicenseUrl: ./LICENSE
ReadmeUrl: ./README.md
Labels: ['amazon', 'sagemaker', 'studio', 'saml', 'sso']
HomePageUrl: https://gitlab.aws.dev/ilyiny/amazon-sagemaker-team-and-user-management-sso
SemanticVersion: 1.0.0
SourceCodeUrl: https://gitlab.aws.dev/ilyiny/amazon-sagemaker-team-and-user-management-sso
Globals:
Function:
Runtime: python3.8
MemorySize: 128
Timeout: 60
Environment:
Variables:
LOG_LEVEL: INFO
Tags:
Project: amazon-sagemaker-saml-backend
Outputs:
SageMakerDomainId:
Description: SageMaker Domain Id
Value: !If
- CreateSageMakerDomainCondition
- !GetAtt SageMakerDomain.Outputs.SageMakerDomainId
- !Ref SageMakerDomainId
Export:
Name: 'sagemaker-domain-id'
SAMLBackendEndpoint:
Description: "API Gateway endpoint URL acting as the Application ACS URL"
Value: !Sub "https://${SageMakerDomainSAMLAPI}.execute-api.${AWS::Region}.amazonaws.com/prod/saml"
SAMLAudience:
Description: "Application SAML audience"
Value: !Sub "https://${SageMakerDomainSAMLAPI}.execute-api.${AWS::Region}.amazonaws.com/"
SageMakerStudioExecutionRoleTeam1Arn:
Description: The ARN of the SageMaker Team1 execution role
Value: !GetAtt IAM.Outputs.SageMakerStudioExecutionRoleTeam1Arn
SageMakerStudioExecutionRoleTeam2Arn:
Description: The ARN of the SageMaker Team2 execution role
Value: !GetAtt IAM.Outputs.SageMakerStudioExecutionRoleTeam2Arn
Parameters:
EnvironmentName:
Type: String
AllowedPattern: '[a-z0-9\-]*'
Default: 'sagemaker-team-mgmt-sso'
Description: Your Amazon SageMaker environment name
SageMakerDomainId:
Type: String
AllowedPattern: '[a-z0-9\-]*'
Default: ''
Description: Existing Amazon SageMaker domain id. Leave empty to create a new domain.
CreatePrivateSubnets:
AllowedValues:
- 'YES'
- 'NO'
Default: 'YES'
Description: Set to NO when you want to re-use existing subnets in the existing VPC (existing VPC Id must be provided in ExistingVPCId).
Type: String
ExistingVPCId:
Type: String
Description: Choose a VPC for SageMaker Studio. Leave empty to create a new VPC.
Default: ''
SAMLBackendSubnetId:
Type: String
Description: Existing private subnet id for SAML backend. Leave empty if CreatePrivateSubnets = YES
Default: ''
SageMakerDomainSubnetId:
Type: String
Description: Choose subnet for SageMaker domain. Leave empty if if CreatePrivateSubnets = YES
Default: ''
VPCCIDR:
AllowedPattern: ^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/(1[6-9]|2[0-8]))$
ConstraintDescription: CIDR block parameter must be in the form x.x.x.x/16-28
Type: String
Default: 10.0.0.0/16
Description: CIDR block for a new VPC
SAMLBackendPrivateSubnetCIDR:
AllowedPattern: ^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/(1[6-9]|2[0-8]))$
ConstraintDescription: CIDR block parameter must be in the form x.x.x.x/16-28
Type: String
Default: 10.0.0.0/19
Description: CIDR block for a private subnet for SAML backend
SageMakerDomainPrivateSubnetCIDR:
AllowedPattern: ^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/(1[6-9]|2[0-8]))$
ConstraintDescription: CIDR block parameter must be in the form x.x.x.x/16-28
Type: String
Default: 10.0.32.0/19
Description: CIDR block for a private subnet for SageMaker domain
PublicSubnetCIDR:
AllowedPattern: ^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/(1[6-9]|2[0-8]))$
ConstraintDescription: CIDR block parameter must be in the form x.x.x.x/16-28
Type: String
Default: 10.0.128.0/20
Description: CIDR block for a public subnet for Internet and NAT Gateways
DomainAccessAllowedCIDR:
Type: String
Default: ''
Description: Allowed CIDR block for CreatePresignedDomainURL API call. Leave empty to allow access from public internet
Conditions:
ExistingVPCCondition: !Not [ !Equals [ !Ref ExistingVPCId, ''] ]
CreateSageMakerDomainCondition: !Equals [ !Ref SageMakerDomainId, '' ]
Resources:
######## CF Stacks ########
VPC:
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: cfn-templates/vpc.yaml
Parameters:
EnvironmentName : !Ref EnvironmentName
ExistingVPCId: !Ref ExistingVPCId
VPCCIDR: !If [ ExistingVPCCondition, !GetAtt GetNetworkConfiguration.VPCCIDR, !Ref VPCCIDR ]
CreatePrivateSubnets: !Ref CreatePrivateSubnets
ExistingSAMLBackendPrivateSubnetId: !Ref SAMLBackendSubnetId
ExistingSageMakerDomainPrivateSubnetId: !Ref SageMakerDomainSubnetId
SAMLBackendPrivateSubnetCIDR: !Ref SAMLBackendPrivateSubnetCIDR
SageMakerDomainPrivateSubnetCIDR: !Ref SageMakerDomainPrivateSubnetCIDR
PublicSubnetCIDR: !Ref PublicSubnetCIDR
Tags:
- Key: EnvironmentName
Value: !Ref EnvironmentName
IAM:
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: cfn-templates/iam.yaml
Parameters:
EnvironmentName : !Ref EnvironmentName
AllowedCIDR: !Ref DomainAccessAllowedCIDR
Tags:
- Key: EnvironmentName
Value: !Ref EnvironmentName
SageMakerDomain:
Type: AWS::CloudFormation::Stack
Condition: CreateSageMakerDomainCondition
Properties:
TemplateURL: cfn-templates/sagemaker-domain.yaml
Parameters:
EnvironmentName: !Ref EnvironmentName
VPCId: !GetAtt VPC.Outputs.VPCId
SageMakerDomainSubnetIds: !GetAtt VPC.Outputs.SageMakerDomainPrivateSubnetId
SageMakerDomainSecurityGroupIds: !GetAtt VPC.Outputs.SageMakerDomainSecurityGroupId
SageMakerDefaultExecutionRoleArn: !GetAtt IAM.Outputs.SageMakerStudioExecutionRoleDefaultArn
SetupLambdaExecutionRoleArn: !GetAtt IAM.Outputs.SetupLambdaExecutionRoleArn
Tags:
- Key: EnvironmentName
Value: !Ref EnvironmentName
GetNetworkConfiguration:
Type: Custom::GetNetworkConfiguration
Condition: ExistingVPCCondition
Properties:
ServiceToken: !GetAtt GetNetworkConfigurationFunction.Arn
VPCId: !Ref ExistingVPCId
GetUserProfileMetadata:
Type: Custom::GetUserProfileMetadata
Properties:
ServiceToken: !GetAtt GetUserProfileMetadataFunction.Arn
Metadata:
Team1:
DomainId: !If
- CreateSageMakerDomainCondition
- !GetAtt SageMakerDomain.Outputs.SageMakerDomainId
- !Ref SageMakerDomainId
SessionExpiration: 43200
Tags:
- Key: Team
Value: Team1
UserSettings:
ExecutionRole: !GetAtt IAM.Outputs.SageMakerStudioExecutionRoleTeam1Arn
Team2:
DomainId: !If
- CreateSageMakerDomainCondition
- !GetAtt SageMakerDomain.Outputs.SageMakerDomainId
- !Ref SageMakerDomainId
SessionExpiration: 43200
Tags:
- Key: Team
Value: Team2
UserSettings:
ExecutionRole: !GetAtt IAM.Outputs.SageMakerStudioExecutionRoleTeam2Arn
######## API Gateway ########
SageMakerDomainSAMLAPI:
Type: AWS::Serverless::Api
Properties:
StageName: prod
EndpointConfiguration:
Type: REGIONAL
Auth:
ResourcePolicy:
CustomStatements: {
Effect: 'Allow',
Action: 'execute-api:Invoke',
Resource: ['execute-api:/*/*/*'],
Principal: '*'
}
######## Lambda Functions ########
GetNetworkConfigurationFunction:
Type: AWS::Serverless::Function
Properties:
ReservedConcurrentExecutions: 1
InlineCode: |
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
import json
import boto3
import cfnresponse
from botocore.exceptions import ClientError
ec2 = boto3.resource("ec2")
def lambda_handler(event, context):
try:
response_status = cfnresponse.SUCCESS
r = {}
if 'RequestType' in event and event['RequestType'] == 'Create':
r["VPCCIDR"] = get_vpc_cidr(
event['ResourceProperties']['VPCId']
)
cfnresponse.send(event, context, response_status, r, '')
except ClientError as exception:
print(exception)
cfnresponse.send(event, context, cfnresponse.FAILED, {}, physicalResourceId=event.get('PhysicalResourceId'), reason=str(exception))
def get_vpc_cidr(vpc_id):
print(vpc_id)
return ec2.Vpc(vpc_id).cidr_block
Role: !GetAtt IAM.Outputs.SetupLambdaExecutionRoleArn
Handler: index.lambda_handler
GetUserProfileMetadataFunction:
Type: AWS::Serverless::Function
Properties:
ReservedConcurrentExecutions: 1
InlineCode: |
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
import json
import boto3
import cfnresponse
from botocore.exceptions import ClientError
def lambda_handler(event, context):
try:
response_status = cfnresponse.SUCCESS
r = {}
if 'RequestType' in event and event['RequestType'] == 'Create':
r["Metadata"] = json.dumps(event['ResourceProperties']['Metadata'])
cfnresponse.send(event, context, response_status, r, '')
except ClientError as exception:
print(exception)
cfnresponse.send(event, context, cfnresponse.FAILED, {}, physicalResourceId=event.get('PhysicalResourceId'), reason=str(exception))
Handler: index.lambda_handler
Policies:
- AWSLambdaBasicExecutionRole
SAMLBackEndFunction:
Type: AWS::Serverless::Function
Properties:
ReservedConcurrentExecutions: 1
CodeUri: functions/saml-backend/
Role: !GetAtt IAM.Outputs.SAMLBackendLambdaExecutionRoleArn
Handler: saml_backend_function.lambda_handler
Environment:
Variables:
PRESIGNED_URL_EXPIRATION: 5
USER_PROFILE_METADATA: !GetAtt GetUserProfileMetadata.Metadata
KEY_NAME_USER_ID: 'ssouserid'
KEY_NAME_TEAM_ID: 'teamid'
Events:
SAMLBackEnd:
Type: Api
Properties:
RestApiId: !Ref SageMakerDomainSAMLAPI
Path: /saml
Method: POST
VpcConfig:
SecurityGroupIds:
- !GetAtt VPC.Outputs.SAMLBackendSecurityGroupId
SubnetIds:
- !GetAtt VPC.Outputs.SAMLBackendPrivateSubnetId