

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

# 建立自訂專案範本
<a name="sagemaker-projects-templates-custom"></a>

**重要**  
自 2024 年 10 月 28 日起， AWS CodeCommit 範本已移除。對於新專案，請從使用第三方 Git 儲存庫的可用專案範本中選取。如需詳細資訊，請參閱[MLOps 專案範本](sagemaker-projects-templates.md)。

如果 SageMaker AI 提供的範本不符合您的需求 (例如，您想要在 CodePipeline 中使用多個階段或自訂核准步驟進行更複雜的協同運作)，請建立您自己的範本。

我們建議您先使用 SageMaker AI 提供的範本，瞭解如何組織程式碼和資源，並在其上進行建置。若要這麼做，請在您啟用 SageMaker AI 範本的管理員存取權之後，登入 [https://console.aws.amazon.com/servicecatalog/](https://console.aws.amazon.com/servicecatalog/)，選擇**產品組合**，然後選擇**已匯入**。如需 Service Catalog 的相關資訊，請參閱 *Service Catalog 使用指南*中的 [Service Catalog 概觀](https://docs.aws.amazon.com/servicecatalog/latest/adminguide/what-is_concepts.html)。

建立您自己的專案範本以自訂 MLOps 專案。SageMaker AI 專案範本是 Service Catalog 佈建的產品，可為您的 MLOps 專案佈建資源。

若要建立自訂專案範本，請完成下列步驟。

1. 建立組合。如需相關資訊，請參閱[步驟 3：建立 Service Catalog 組合](https://docs.aws.amazon.com/servicecatalog/latest/adminguide/getstarted-portfolio.html)。

1. 建立產品。產品是 CloudFormation 範本。您可以建立產品的多個版本。如需相關資訊，請參閱[步驟 4：建立 Service Catalog 產品](https://docs.aws.amazon.com/servicecatalog/latest/adminguide/getstarted-product.html)。

   對於要使用 SageMaker 專案的產品，請將下列參數新增至您的產品範本。

   ```
   SageMakerProjectName:
   Type: String
   Description: Name of the project
   
   SageMakerProjectId:
   Type: String
   Description: Service generated Id of the project.
   ```
**重要**  
我們建議您將 CodeCommit 儲存庫包裝到 SageMaker AI 程式碼儲存庫中，以便在 VPC 模式下可看見專案的儲存庫。範例範本和必要的新增內容會顯示在下列程式碼範例中。  
原始 (範例) 範本：  

   ```
   ModelBuildCodeCommitRepository:
       Type: AWS::CodeCommit::Repository
       Properties:
         # Max allowed length: 100 chars
         RepositoryName: !Sub sagemaker-${SageMakerProjectName}-${SageMakerProjectId}-modelbuild # max: 10+33+15+10=68
         RepositoryDescription: !Sub SageMaker Model building workflow infrastructure as code for the Project ${SageMakerProjectName}
         Code:
           S3:
             Bucket: SEEDCODE_BUCKETNAME
             Key: toolchain/model-building-workflow-v1.0.zip
           BranchName: main
   ```
要在 VPC 模式下新增的其他內容：  

   ```
   SageMakerRepository:
       Type: AWS::SageMaker::CodeRepository
       Properties:
           GitConfig:
               RepositoryUrl: !GetAtt ModelBuildCodeCommitRepository.CloneUrlHttp
               Branch: main
   ```

1. 新增啟動限制。當使用者啟動產品時，啟動限制可指定 Service Catalog 擔任的 IAM 角色。如需相關資訊，請參閱[步驟 6：新增啟動限制以指派 IAM 角色](https://docs.aws.amazon.com/servicecatalog/latest/adminguide/getstarted-launchconstraint.html)。

1. 在 [https://console.aws.amazon.com/servicecatalog/](https://console.aws.amazon.com/servicecatalog/) 上佈建產品以測試範本。如果您對範本感到滿意，請繼續執行下一個步驟，讓範本可在 Studio (或 Studio Classic) 中使用。

1. 將您在步驟 1 中建立之 Service Catalog 組合的存取權授與 Studio (或 Studio Classic) 執行角色。使用網域執行角色或具有 Studio (或 Studio Classic) 存取權的使用者角色。有關向產品組合新增角色的資訊，請參閱[步驟 7：授予最終用戶對產品組合的存取權限](https://docs.aws.amazon.com/servicecatalog/latest/adminguide/getstarted-deploy.html)。

1. 若要讓您的專案範本可在 Studio (或 Studio Classic) 的**組織範本**清單中使用，請使用您在步驟 2 中建立的 Service Catalog 產品的下列索引鍵和值來建立標籤。
   + **key**：`sagemaker:studio-visibility`
   + **值**：`true`

在您完成這些步驟之後，您組織中的 Studio (或 Studio Classic) 使用者可以使用您建立的範本來建立專案，方法是遵循[使用 Amazon SageMaker Studio 或 Studio Classic 建立 MLOps 專案](sagemaker-projects-create.md)中的步驟，並在選擇範本時選擇**組織範本**。

## 使用 Amazon S3 儲存貯體中的範本
<a name="sagemaker-projects-templates-s3"></a>

您也可以使用存放在 Amazon S3 中的範本建立 SageMaker 專案。

**注意**  
雖然您可以使用 中的範本 AWS Service Catalog，但我們建議您將範本存放在 S3 儲存貯體中，並使用這些範本建立專案。

### 管理員設定
<a name="sagemaker-projects-templates-s3-setup"></a>

在使用 S3 儲存貯體中的範本建立專案之前，請執行下列步驟。

1. [建立 S3 儲存貯](https://docs.aws.amazon.com/AmazonS3/latest/userguide/create-bucket-overview.html)體，並將您的範本上傳至儲存貯體。

1. [在 S3 儲存貯體上設定 CORS 政策，以設定存取許可](https://docs.aws.amazon.com/AmazonS3/latest/userguide/enabling-cors-examples.html)。

1. 將下列鍵/值標籤新增至範本，讓 SageMaker AI 可以看到它們。

   ```
   sagemaker:studio-visibility : true
   ```

1. [建立網域](https://docs.aws.amazon.com/sagemaker/latest/dg/onboard-quick-start.html)。

1. SageMaker AI 完成建立網域後，請將下列鍵值標籤新增至網域：

   ```
   sagemaker:projectS3TemplatesLocation : s3://<amzn-s3-demo-bucket>
   ```

然後使用 AWS 主控台、Python 或 [CreateProject](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateProject.html) 和 [UpdateProject](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_UpdateProject.html) API 操作，從 S3 儲存貯體內的範本建立或更新 SageMaker 專案。 S3 

------
#### [ Studio ]

**建立專案**

1. 開啟位在 [https://console.aws.amazon.com/sagemaker/](https://console.aws.amazon.com/sagemaker/) 的 Amazon SageMaker AI 主控台。

1. 請遵循[啟動 Amazon SageMaker Studio](https://docs.aws.amazon.com/sagemaker/latest/dg/studio-updated-launch.html) 中的指示來開啟 SageMaker Studio 主控台。

1. 在左側導覽窗格中，選擇**部署**、**專案**、**建立專案**。

1. 選擇**組織範本**，然後選擇 **S3 範本**以查看可供您使用的範本。如果您沒有看到預期的範本，請通知您的管理員。

1. 選擇您要使用的範本，然後選擇**下一步**。

1. 輸入專案的名稱、選用描述和其他必要欄位。完成時，選擇 **Create (建立)**。

**更新專案**

1. 開啟位在 [https://console.aws.amazon.com/sagemaker/](https://console.aws.amazon.com/sagemaker/) 的 Amazon SageMaker AI 主控台。

1. 請遵循[啟動 Amazon SageMaker Studio](https://docs.aws.amazon.com/sagemaker/latest/dg/studio-updated-launch.html) 中的指示來開啟 SageMaker Studio 主控台。

1. 選擇您要更新的專案。選擇**動作**，然後選擇**更新專案**。

1. 更新專案時，您可以更新範本參數或範本 URL。完成後，請選擇 **Next (下一步)**。

1. 檢閱摘要資料表中的專案更新，然後選擇**更新**。

------
#### [ Python Boto3 ]

建立 S3 儲存貯體並上傳範本後，您可以使用下列範例來建立 SageMaker 專案。

```
sagemaker_client = boto3.client('sagemaker', region_name='us-west-2')

response = sagemaker_client.create_project(
    ProjectName='my-custom-project',
    ProjectDescription='SageMaker project with custom CFN template stored in S3',
    TemplateProviders=[{
        'CfnTemplateProvider': {
            'TemplateName': 'CustomProjectTemplate',
            'TemplateURL': f'https://<bucket_name>.s3.us-west-2.amazonaws.com/custom-project-template.yml',
            'Parameters': [
                {'Key': 'ParameterKey', 'Value': 'ParameterValue'}
            ]
        }
    }]
)
print(f"Project ARN: {response['ProjectArn']}")
```

若要更新 SageMaker 專案，請參閱下列範例。

```
sagemaker_client = boto3.client('sagemaker', region_name='us-west-2')

response = sagemaker_client.update_project(
    ProjectName='my-custom-project',
    ProjectDescription='SageMaker project with custom CFN template stored in S3',
    TemplateProvidersToUpdate=[{
        'CfnTemplateProvider': {
            'TemplateName': 'CustomProjectTemplate',
            'TemplateURL': f'https://<bucket_name>.s3.us-west-2.amazonaws.com/custom-project-template.yml',
            'Parameters': [
                {'Key': 'ParameterKey', 'Value': 'ParameterValue'}
            ]
        }
    }]
)
print(f"Project ARN: {response['ProjectArn']}")
```

------