I think the only way to do is through custom resource in two stages:
- Create your role with "normal" thrust policy
- Update the role using custom resource
Below is fully working exemplary code on how to do it:
Resources:
SagemakerRole:
Type: 'AWS::IAM::Role'
Properties:
RoleName: sagemaker-role
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- sagemaker.amazonaws.com
Action: 'sts:AssumeRole'
Path: /
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AmazonS3FullAccess
- arn:aws:iam::aws:policy/AmazonSageMakerFullAccess
LambdaBasicExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
Path: /
Policies:
- PolicyName: UpdateAssumePolicy
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- iam:UpdateAssumeRolePolicy
- iam:GetRole
Resource: !GetAtt SagemakerRole.Arn
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
MyCustomResource:
Type: Custom::RoleAssumesItself
Properties:
ServiceToken: !GetAtt MyCustomFunction.Arn
RoleName: !Ref SagemakerRole
MyCustomFunction:
Type: AWS::Lambda::Function
Properties:
Handler: index.lambda_handler
Timeout: 10
Role: !GetAtt 'LambdaBasicExecutionRole.Arn'
Runtime: python3.7
Code:
ZipFile: |
import json
import cfnresponse
import boto3
iam = boto3.resource('iam')
def lambda_handler(event, context):
print(json.dumps(event, default=str))
try:
responseData = {}
if event['RequestType'] in ["Create"]:
role_name = event['ResourceProperties']['RoleName']
role = iam.Role(role_name)
current_permissions = role.assume_role_policy_document
print(current_permissions)
current_permissions['Statement'].append(
{'Effect': 'Allow',
'Principal':
{'AWS': role.arn},
'Action': 'sts:AssumeRole'
})
#print(current_permissions)
response = role.AssumeRolePolicy().update(
PolicyDocument=json.dumps(current_permissions))
print(response)
cfnresponse.send(event, context,
cfnresponse.SUCCESS, responseData)
else:
print('Unexpected RequestType!')
cfnresponse.send(event, context,
cfnresponse.SUCCESS, responseData)
except Exception as err:
print(str(err))
responseData = {"Data": str(err)}
cfnresponse.send(event,context,
cfnresponse.FAILED,responseData)
return
!Ref SagemakerRole
, try!Sub 'arn:${AWS::Partition}:iam::${AWS::AccountId}:role/sagemaker-role'
– jarmod