You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Terraform创建SageMaker Notebook实例进入Failed状态无法进入InService排查

问题描述

我在本地terraform-folder目录下执行terraform apply部署SageMaker Notebook实例,得到如下完整报错:

aws_sagemaker_notebook_instance.notebook_instance: Creating...
aws_sagemaker_notebook_instance.notebook_instance: Still creating... [10s elapsed]
aws_sagemaker_notebook_instance.notebook_instance: Still creating... [20s elapsed]
...
aws_sagemaker_notebook_instance.notebook_instance: Still creating... [15m21s elapsed]
aws_sagemaker_notebook_instance.notebook_instance: Still creating... [15m31s elapsed]
╷
│ Error: error waiting for sagemaker notebook instance (aws-sm-notebook-instance) to create: unexpected state 'Failed', wanted target 'InService'. last error: %!s(<nil>)
│
│   with aws_sagemaker_notebook_instance.notebook_instance,
│   on notebook_instance.tf line 2, in resource "aws_sagemaker_notebook_instance" "notebook_instance":
│    2: resource "aws_sagemaker_notebook_instance" "notebook_instance" {
│

已尝试的解决方法

  • 调整on-start.sh脚本中的IDLE_TIME参数为IDLE_TIME=1800(单位为秒,对应30分钟,理论可覆盖约15分钟的部署时长),调整后仍抛出相同报错。
  • 执行terraform refresh让Terraform对照云服务商的实际资源刷新自身状态文件,刷新后立即执行terraform apply仍未解决问题。

目前无法确定IDLE_TIME=1800配置未生效的原因,该时长理论上完全可以覆盖15分钟的apply操作时长。


补充代码细节(便于排查)

1. 创建SageMaker notebook实例的代码

resource "aws_sagemaker_notebook_instance" "notebook_instance" {
  name                    = "aws-sm-notebook-instance"
  role_arn                = aws_iam_role.notebook_iam_role.arn
  instance_type           = "ml.t2.medium"
  lifecycle_config_name   = aws_sagemaker_notebook_instance_lifecycle_configuration.notebook_config.name
  default_code_repository = aws_sagemaker_code_repository.git_repo.code_repository_name
}

2. 定义SageMaker notebook生命周期配置的代码

resource "aws_sagemaker_notebook_instance_lifecycle_configuration" "notebook_config" {
  name      = "dev-platform-al-sm-lifecycle-config"
  on_create = filebase64("../scripts/on-create.sh")
  on_start  = filebase64("../scripts/on-start.sh")
}

3. 定义SageMaker notebook实例关联Git仓库的代码

resource "aws_sagemaker_code_repository" "git_repo" {
  code_repository_name = "aws-sm-notebook-instance-repo"

  git_config {
    repository_url = "https://github.com/AndreasLuckert/aws-sm-notebook-instance.git"
  }
}

on-start.sh脚本内容(包含IDLE_TIME参数)

该脚本会被scripts/autostop.py调用:

#!/bin/bash

set -e

## IDLE AUTOSTOP STEPS
## ----------------------------------------------------------------

## Setting the timeout (in seconds) for how long the SageMaker notebook can run idly before being auto-stopped
# -> e.g. 1800 s = 30 min since first deployment can take between 15 and 20 minutes which could then fail like so:
# "Error: error waiting for sagemaker notebook instance (aws-sm-notebook-instance) to create: unexpected state 'Failed', wanted target 'InService'. last error: %!s(<nil>)"
IDLE_TIME=1800

# Getting the autostop.py script from GitHub
echo "Fetching the autostop script..."
wget https://raw.githubusercontent.com/andreasluckert/aws-sm-notebook-instance/main/scripts/autostop.py

# Using crontab to autostop the notebook when idle time is breached
echo "Starting the SageMaker autostop script in cron."
(crontab -l 2>/dev/null; echo "*/5 * * * * /usr/bin/python $PWD/autostop.py --time $IDLE_TIME --ignore-connections") | crontab -



## CUSTOM CONDA KERNEL USAGE STEPS
## ----------------------------------------------------------------

# Setting the proper user credentials
sudo -u ec2-user -i <<'EOF'
unset SUDO_UID

# Setting the source for the custom conda kernel
WORKING_DIR=/home/ec2-user/SageMaker/custom-miniconda
source "$WORKING_DIR/miniconda/bin/activate"

# Loading all the custom kernels
for env in $WORKING_DIR/miniconda/envs/*; do
    BASENAME=$(basename "$env")
    source activate "$BASENAME"
    python -m ipykernel install --user --name "$BASENAME" --display-name "Custom ($BASENAME)"
done
EOF

内容的提问来源于stack exchange,提问作者Andreas L.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 23:57:03