View a markdown version of this page

使用沒有網際網路存取的子網路連線至 VPC - Amazon SageMaker AI

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用沒有網際網路存取的子網路連線至 VPC

在沒有網際網路存取的情況下將遠端 IDE 連線到私有子網路中的 Studio 空間之前,請確定您的管理員具有 設定 Studio 以透過 VPC 內沒有網際網路存取的子網路執行

您有下列選項可將遠端 IDE 連線至私有子網路中的 Studio 空間:

  • 設定 HTTP Proxy (支援 VS 程式碼和 Kiro)

  • 預先封裝的遠端伺服器和延伸模組 (僅限 VS 程式碼)

重要

不支援在沒有傳出網際網路存取的情況下,將游標連接到私有子網路中的 Studio 空間。

具有受控允許清單的 HTTP Proxy

當您的 Studio 空間位於防火牆或代理之後時,請要求您的管理員允許存取您的 IDE 伺服器和延伸模組相關的 CDNs和端點。如需詳細資訊,請參閱使用受控允許清單設定 HTTP Proxy

VS Code

透過為代理 URL 提供 remote.SSH.httpProxyremote.SSH.httpsProxy設定,為 VS Code 遠端開發設定 HTTP 代理。

注意

考慮啟用「遠端.SSH:使用 Curl and Wget 組態檔案」來使用遠端環境的 curlrcwgetrc 檔案的組態。這是為了讓放置在 SageMaker 空間中各自預設位置的 curlrcwgetrc 檔案可用於啟用特定案例。

Kiro

透過將 aws.sagemaker.ssh.kiro.httpsProxy設定設定為 HTTP 或 HTTPS 代理端點,為 Kiro 遠端開發設定 HTTP 代理。

如果您在 Kiro 中使用 MCP (模型內容通訊協定) 伺服器,您也需要將代理環境變數新增至 MCP 伺服器組態:

"env": { "http_proxy": "${http_proxy}", "https_proxy": "${https_proxy}" }

此選項適用於允許您設定 HTTP Proxy,並可讓您彈性安裝其他延伸模組的情況,因為某些延伸模組需要公有端點。

預先封裝的遠端伺服器和延伸模組 (僅限 VS 程式碼)

注意

此選項僅適用於 Visual Studio Code。Kiro 和 Cursor 不支援預先封裝的遠端伺服器設定。

當您的 Studio 空間無法存取外部端點,下載 VS Code 遠端伺服器和延伸模組時,您可以預先封裝它們。使用此方法,您的管理員可以匯出一個 tarball,其中包含特定版本 VS Code 的 .VS Code-server 目錄。然後,管理員會使用 SageMaker AI 生命週期組態 (LCC) 指令碼,將 tarball 複製並擷取到您的主目錄 (/home/sagemaker-user)。如需詳細資訊,請參閱設定預先封裝的遠端伺服器和延伸模組 (僅限 VS 程式碼)

針對 VS Code 遠端伺服器和延伸模組使用預先封裝的指示

  1. 在您的本機電腦上安裝 VS Code

  2. 當您連線到 SageMaker 空間時:

    • 使用預設設定檔來確保與預先封裝的延伸模組相容。否則,您需要在連線至 Studio 空間之後,使用下載的 VSIX 檔案安裝延伸模組。

    • 選擇在您啟動空間時要連接至空間的 VS Code 版本特定 LCC 指令碼。

預先封裝 VS Code 遠端伺服器和延伸模組的 Dockerfile 使用範例

如果無法建立已啟用遠端存取和網際網路的空間,則以下是啟動已預先安裝 SSH 伺服器的本機容器的範例 Dockerfile。

注意
  • 在此範例中,SSH 伺服器不需要驗證,且僅用於匯出 VS Code 遠端伺服器。

  • 容器應該在 x64 架構上建置和執行。

FROM amazonlinux:2023 # Install OpenSSH server and required tools RUN dnf install -y \ openssh-server \ shadow-utils \ passwd \ sudo \ tar \ gzip \ && dnf clean all # Create a user with no password RUN useradd -m -s /bin/bash sagemaker-user && \ passwd -d sagemaker-user # Add sagemaker-user to sudoers via wheel group RUN usermod -aG wheel sagemaker-user && \ echo 'sagemaker-user ALL=(ALL) NOPASSWD:ALL' > /etc/sudoers.d/sagemaker-user && \ chmod 440 /etc/sudoers.d/sagemaker-user # Configure SSH to allow empty passwords and password auth RUN sed -i 's/^#\?PermitEmptyPasswords .*/PermitEmptyPasswords yes/' /etc/ssh/sshd_config && \ sed -i 's/^#\?PasswordAuthentication .*/PasswordAuthentication yes/' /etc/ssh/sshd_config # Generate SSH host keys RUN ssh-keygen -A # Expose SSH port EXPOSE 22 WORKDIR /home/sagemaker-user USER sagemaker-user # Start SSH server CMD ["bash"]

使用下列命令來建置和執行容器。

# Build the image docker build . -t remote_server_export # Run the container docker run --rm -it -d \ -v /tmp/remote_access/.VS Code-server:/home/sagemaker-user/.VS Code-server \ -p 2222:22 \ --name remote_server_export \ remote_server_export # change the permisson for the mounted folder docker exec -i remote_server_export \ bash -c 'sudo chown sagemaker-user:sagemaker-user ~/.VS Code-server' # start the ssh server in the container docker exec -i remote_server_export bash -c 'sudo /usr/sbin/sshd -D &'

使用下列命令進行連線:

ssh sagemaker-user@localhost -p 2222

在可以連線此容器之前,請在 .ssh/config 檔案中設定下列項目。之後,您可以在連線時於遠端 SSH 側邊面板中將 remote_access_export 視為主機名稱。例如:

Host remote_access_export HostName localhost User=sagemaker-user Port 2222 ForwardAgent yes

封存 /tmp/remote_access/.VS Code-server 並遵循預先封裝 VS Code 遠端伺服器和延伸模組中的步驟,來連線並安裝延伸模組。解壓縮後,請確定 .VS Code-server 資料夾顯示為父資料夾。

cd /tmp/remote_access/ sudo tar -czvf VS Code-server-with-extensions-for-1.100.2.tar.gz .VS Code-server

範例 LCC 指令碼 (LCC-install-VS Code-server-v1.100.2)

以下是如何安裝特定版本 VS Code 遠端伺服器的範例。

#!/bin/bash set -x remote_server_file=VS Code-server-with-extensions-for-1.100.2.tar.gz if [ ! -d "${HOME}/.VS Code-server" ]; then cd /tmp aws s3 cp s3://S3_BUCKET/remote_access/${remote_server_file} . tar -xzvf ${remote_server_file} mv .VS Code-server "${HOME}" rm ${remote_server_file} else echo "${HOME}/.VS Code-server already exists, skipping download and install." fi