Skip to content

Sagemaker studio studio launch glue interactive session

0

Hi All,

I want to use sagemaker studio to launch a glue interactive session. Since my sagemaker role does not have IAM permission to create glue interactive session, I need to assume another glue role to achieve it. As below image, i can use the %%assume_role magic function in the glue notebook: Enter image description here

This way i can launch a glue interactive session successfully, however, the default duration of assume role is 1 hour, after 1 hour my sts token is expired. How can i change this duration when assuming a role in glue interactive session, i don't see any option to set assume role duration in %help.

1 Answer
3

To launch a Glue interactive session from SageMaker Studio and extend the duration of the assumed role beyond the default 1 hour, you'll need to take a different approach as the %%assume_role magic function doesn't provide a direct option to set the duration.

Instead, you can use the AWS SDK within your notebook to assume the role with a longer duration before starting the Glue session. Here's how you can do this:

  1. First, install the AWS SDK for Python (Boto3) in your SageMaker Studio environment if it's not already available.

  2. In your notebook, use Boto3 to assume the role with a longer duration. The maximum duration you can set is typically 12 hours (43200 seconds), but this can vary based on the role's configuration.

  3. After assuming the role, use the temporary credentials to configure the Glue session.

Here's an example of how you might implement this:

import boto3
import os

# Assume the role with a longer duration
sts_client = boto3.client('sts')
assumed_role_object = sts_client.assume_role(
    RoleArn="arn:aws:iam::ACCOUNT_ID:role/YOUR_GLUE_ROLE",
    RoleSessionName="GlueInteractiveSession",
    DurationSeconds=43200  # 12 hours
)

# Set environment variables with the temporary credentials
os.environ['AWS_ACCESS_KEY_ID'] = assumed_role_object['Credentials']['AccessKeyId']
os.environ['AWS_SECRET_ACCESS_KEY'] = assumed_role_object['Credentials']['SecretAccessKey']
os.environ['AWS_SESSION_TOKEN'] = assumed_role_object['Credentials']['SessionToken']

# Now you can start your Glue session using these credentials
# The session will use the assumed role credentials for the extended duration

# Use Glue magic commands to start and configure your session
%glue_version 3.0
%idle_timeout 2880
%worker_type G.1X
%number_of_workers 5

# Your Glue code here

By setting the environment variables with the assumed role credentials, the Glue session will use these credentials for the duration you specified (up to 12 hours in this example).

Remember to replace "ACCOUNT_ID" and "YOUR_GLUE_ROLE" with your actual AWS account ID and the ARN of the Glue role you want to assume.

Also, keep in mind that while this approach allows for a longer session duration, it's important to manage your sessions responsibly to control costs. You should still monitor your session usage and terminate sessions when they're no longer needed.

Lastly, ensure that the role you're assuming has the necessary permissions to create and manage Glue interactive sessions, and that your SageMaker execution role has permission to assume this Glue role.

Sources
Using interactive sessions with SageMaker Studio - AWS Glue
Get Started with AWS Glue Interactive Sessions - Amazon SageMaker
Manage AWS Glue interactive sessions | AWS re:Post

answered a year ago
EXPERT
reviewed a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.