IAM Role Assignment for Secure Model Serving Endpoints
PythonTo set up an IAM role for secure model serving endpoints, we will create an IAM role that has the necessary permissions to access resources required for model serving, such as Amazon SageMaker endpoints. This role can then be assumed by the service or user who is deploying the model.
IAM roles are a secure way to delegate permissions that do not require sharing security credentials. You can create roles in AWS IAM and manage permissions to control which operations can be performed by the entity, or AWS service, that assumes the role.
In this case, we'll create a role that has permissions to interact with SageMaker, and apply an inline policy to the role that defines the permissions. We'll use the AWS SDK for Pulumi to create these resources.
Let's start by writing the Pulumi program in Python to create an IAM role with the necessary policies attached for model serving:
import pulumi import pulumi_aws as aws # Create an IAM role for SageMaker model serving sagemaker_role = aws.iam.Role("SageMakerServiceRole", assume_role_policy="""{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "sagemaker.amazonaws.com" }, "Action": "sts:AssumeRole" } ] }""" ) # Create a policy that gives access to the necessary SageMaker actions sagemaker_policy = aws.iam.Policy("SageMakerPolicy", policy="""{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "sagemaker:CreateEndpoint", "sagemaker:DeleteEndpoint", "sagemaker:InvokeEndpoint", "sagemaker:UpdateEndpoint" // include any other permissions necessary for your use case ], "Resource": "*" } ] }""" ) # Attach the policy to the role sagemaker_role_policy_attachment = aws.iam.RolePolicyAttachment("SageMakerRolePolicyAttachment", role=sagemaker_role.name, policy_arn=sagemaker_policy.arn ) # Export the role ARN so it can be used outside of Pulumi pulumi.export('sagemaker_role_arn', sagemaker_role.arn)
In this program:
- We've declared an IAM role specifically for the SageMaker service, with a trust relationship that allows SageMaker to assume the role (
assume_role_policy
). - We're creating a new IAM policy (
sagemaker_policy
) that grants permissions to create, update, delete, and invoke SageMaker endpoints. - Then we attach the policy to the newly created role using
aws.iam.RolePolicyAttachment
, which connects our SageMaker policy to the IAM role.
Note: The policy in this example has broad permissions, using
"Resource": "*"
. In production, you should scope this down to the specific resources your model serving endpoints will need to access for better security. The action permissions should also be reviewed and limited to what is necessary for your use case.Finally, the
pulumi.export
line will output the ARN of the IAM role created, which you can use in your applications or services that need to assume this role for interacting with SageMaker.Please ensure that you have AWS credentials configured on your system where Pulumi is installed, and the AWS account has sufficient permissions to create these resources.
To run this Pulumi program, save the code into a file named
__main__.py
, and then runpulumi up
. Pulumi will handle the creation and deployment of these AWS resources for you.- We've declared an IAM role specifically for the SageMaker service, with a trust relationship that allows SageMaker to assume the role (