Catalog
huggingface/hf-cloud-sagemaker-iam-preflight

huggingface

hf-cloud-sagemaker-iam-preflight

Ensure a usable SageMaker execution role exists before deploying or training. Use this skill whenever about to create a SageMaker endpoint, model, training job, or any resource that requires an execution role. Use it especially when the user has not provided a role ARN explicitly, when scripts are about to call `iam:CreateRole`, or when an AccessDenied error mentions an IAM action. Never blindly call `iam:CreateRole` — always check for existing roles first. This skill prevents the most common SageMaker deployment failure: trying to create IAM resources from an SSO principal that has no IAM write permissions.

NewUpdated Sep 11, 2026

SageMaker IAM Preflight

Every SageMaker resource needs an execution role — the IAM role SageMaker assumes to read model artifacts from S3, pull serving containers from ECR, and write logs. Most deployments fail here because the script tried to create a new role without checking if a usable one already existed, then blew up because the caller is an SSO principal.

This skill encodes the right order: discover, validate, only create if necessary.

Running the helpers (cross-platform)

The helpers are Python so they run identically on Windows, macOS, and Linux:

python3 scripts/check_role.py        # macOS / Linux
python  scripts/check_role.py        # Windows (PowerShell / cmd)

Run them from the shell where the AWS CLI already works — i.e. wherever aws sts get-caller-identity succeeds. The script shells out to that same aws binary and inherits the shell's profile, region, SSO session, proxy, and credential chain.

Windows / WSL / Git Bash caveat. Do not invoke these through a Bash shim (WSL, Git Bash, MSYS) on Windows. Those Bash environments frequently do not share the Windows AWS config, credentials, SSO sessions, environment variables, or proxy settings — so aws sts get-caller-identity fails inside Bash even when it works natively in PowerShell. (This is exactly why the old .sh helpers failed on Windows and were replaced with Python.) If you're in PowerShell, run python ...\check_role.py directly in PowerShell. If the helper still can't see your identity, run the same discovery natively (see "Native AWS CLI equivalent" below) in the shell where aws sts get-caller-identity returns your ARN.

Order of operations

Step 1 — Did the user provide a role?

Validate that one specifically:

python3 scripts/check_role.py "<role-name-or-arn>"

On success it prints the ARN to stdout (exit 0). On failure it logs why on stderr. Don't try to silently fix a broken role — surface the problem.

Step 2 — Discover existing roles

python3 scripts/check_role.py

Lists roles matching common SageMaker patterns (AmazonSageMaker-ExecutionRole-*, SageMakerExecutionRole*, etc.), ranks by last-used date (most recent first), validates trust policy in that order, returns the first usable ARN. Most accounts that have used SageMaker before already have one.

Why rank by last-used: in accounts with multiple roles (auto-generated 2021 role + manual project role + etc.), the alphabetically-first one is rarely the actively-maintained one. The most-recently-used role is more likely to have current policies — including cross-account ECR pull. The script prints the ranking so you can see which got picked.

IAM frequently reports no RoleLastUsed at all (tracking only covers recent activity). When every candidate ties at "never used", the script falls back to newest creation date — a newer role is more likely to have current policies than a 2021 leftover.

Step 3 — Create, only if discovery found nothing

If the user can create (has IAM permissions):

python3 scripts/create_role.py "<role-name>" "<model-bucket>"

Second arg scopes S3 access to a specific bucket. Omit if unknown; script warns and the user can update the policy later.

If the user cannot create (SSO principal — hf-cloud-aws-context-discovery will have flagged this):

Stop and surface this clearly. Don't retry alternative IAM operations hoping one works:

I can't find an existing SageMaker execution role, and you're authenticated via SSO so you can't create one directly. Please either:

  • Ask your AWS admin for a SageMaker execution role ARN, or
  • Have them grant your SSO permission set iam:CreateRole, iam:PutRolePolicy

Specific instructions get unblocked fast; vague "permission denied" messages don't.

What "validated" means

A role is usable when (1) it exists, (2) its trust policy allows sagemaker.amazonaws.com to sts:AssumeRole, and (3) its permissions grant only the actions and resources this deployment needs. See references/trust-policy.json for the canonical trust policy.

check_role.py verifies existence and trust because policy evaluation depends on the deployment's exact S3, ECR, logging, and optional output resources. Before deployment, inspect the selected role's policies and compare them with references/minimum-permissions.json; add only missing actions and scope them to the required resources. Do not attach AmazonSageMakerFullAccess or defer permission review until an AccessDenied failure.

Minimum permissions

references/minimum-permissions.json is the standalone inline policy for endpoint execution:

  • s3:GetObject + s3:ListBucket on the model artifact bucket
  • ECR pull permissions
  • CloudWatch logs and metrics

create_role.py installs this inline policy without attaching a managed FullAccess policy. Replace REPLACE_WITH_MODEL_BUCKET in the template with the actual bucket name — create_role.py does this automatically when given a bucket as its second argument. Add narrowly scoped permissions separately for optional features such as async output or data capture.

Native AWS CLI equivalent (fallback)

If the Python helper can't run or can't see your identity (rare — usually a broken PATH or running under a Bash shim that lacks AWS context), do the same preflight by hand in the shell where aws sts get-caller-identity works. The logic is just AWS CLI calls; the helper exists only to bundle and rank them.

PowerShell:

# 1. List candidate SageMaker roles
aws iam list-roles --query "Roles[?contains(RoleName,'SageMaker') || contains(RoleName,'sagemaker')]" --output json

# 2. For each candidate, confirm the trust policy allows sagemaker.amazonaws.com
aws iam get-role --role-name <role-name> --query "Role.AssumeRolePolicyDocument" --output json

# 3. Prefer the most-recently-used role with SageMaker-execution naming
#    (LastUsedDate is often None for every role — then prefer newest CreateDate)
aws iam get-role --role-name <role-name> --query "Role.[RoleLastUsed.LastUsedDate, CreateDate]" --output text

Pick the most-recently-used role whose trust policy contains sagemaker.amazonaws.com. Use the resulting ARN exactly as if check_role.py had returned it. Bash/macOS/Linux use the same commands.

Files5
5 files · 22.8 KB

Select a file to preview

Overall Score

87/100

Grade

A

Excellent

Grades are signals, not a certification. Always review a skill yourself before use.

Safety

88

Quality

87

Clarity

88

Completeness

84

Summary

This skill automates the critical preflight check for SageMaker deployments: discovering, validating, and optionally creating IAM execution roles. It provides cross-platform Python helpers (Windows/macOS/Linux) that query AWS IAM, rank candidate roles by recency, and validate trust policies before deployment. The skill encodes AWS best practices: check before creating, handle SSO principal limitations, and scope permissions narrowly.

Detected Capabilities

shell execution (subprocess.run)AWS CLI invocationJSON parsing and file I/OIAM role discovery and validationIAM role creationpolicy document templatingcross-platform path resolution

Trigger Keywords

Phrases that agents use to match this skill to user intent.

sagemaker execution rolepreflight iam checkaws role validationsagemaker deployment setupiam role discoveryrole trust policysagemaker arn lookup

Risk Signals

INFO

AWS CLI execution via subprocess (aws iam list-roles, aws iam get-role, aws iam create-role, aws iam put-role-policy)

scripts/check_role.py and scripts/create_role.py
INFO

iam:ListRoles, iam:GetRole, iam:CreateRole, iam:PutRolePolicy API calls

scripts/check_role.py:run_aws(), scripts/create_role.py:run_aws()
INFO

Credential context inherited from shell (AWS profile, SSO session, region)

scripts/check_role.py and scripts/create_role.py (documented design)
INFO

SSO principal detection and graceful failure

scripts/create_role.py:main() checks for 'AWSReservedSSO_' in caller ARN
INFO

Inline policy document handling (no file:// paths, direct string passing)

scripts/create_role.py:run_aws([..., '--policy-document', inline_policy])

Referenced Domains

External domains referenced in skill content, detected by static analysis.

www.apache.org

Use Cases

  • >SageMaker endpoint deployment requires role, user hasn't provided one
  • Identify an existing execution role in accounts with multiple SageMaker roles
  • Validate a user-supplied role ARN before passing it to deployment scripts
  • Create a SageMaker execution role with minimal permissions when caller has IAM write access
  • Diagnose 'role not found' or 'AccessDenied' errors in SageMaker deployment pipelines
  • Educate users on trust policy requirements and minimum permissions for SageMaker
  • Handle SSO principal limitations and provide clear next steps when role creation isn't possible

Quality Notes

  • Excellent clarity on platform-specific behavior: Windows/Bash shim gotchas documented in detail with specific guidance
  • Strong error messaging — SSO principal detection explicitly warns and provides actionable next steps instead of silent failure
  • Cross-platform robustness justified: Python-based (not Bash) to inherit AWS context reliably on Windows; inline policy documents avoid path translation issues
  • Role ranking logic well-motivated: most-recently-used roles in multi-role accounts are more likely to have current policies; fallback to creation date when no usage tracking
  • Minimal permissions template is explicit and scoped: S3 access limited to model bucket, ECR only pulls, CloudWatch to SageMaker namespaces
  • Trust policy validation uses substring matching for robustness across JSON shapes (single vs. list Principal.Service)
  • Supporting reference files (trust-policy.json, minimum-permissions.json) are present and inline in documentation with placeholder guidance
  • Comprehensive native AWS CLI fallback provided for cases where Python helper can't run
  • Role discovery includes fallback for alphabetically-first selection — rarely the right choice but documented
  • Limitation: script does not verify inline policy permissions (only trust policy) — user must manually review before deployment per skill docs
Model: claude-haiku-4-5-20251001Analyzed: Sep 11, 2026

Reviews

Add this skill to your library to leave a review.

No reviews yet

Be the first to share your experience.

Version History

  1. v1.1

    Content updated

    ✦ AIRemoves `iam:AttachRolePolicy` from IAM permission requirements; shifts role validation to check trust policy only and defer detailed permissions review to deployment time with reference to minimum-p…

    2026-09-11

    LATEST
  2. v1.0

    2026-07-11

    View This VersionInitial version

Use huggingface/hf-cloud-sagemaker-iam-preflight in your dev environment

Command Palette

Search for a command to run...