

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

# 在 Secrets Manager 秘密中建立和存放權杖
<a name="asm-create-secret"></a>

如果您選擇使用 來存放使用 Secrets Manager 的存取字符，您可以使用現有的秘密連線或建立新的秘密。若要建立新的秘密，請執行下列動作：

------
#### [ AWS 管理主控台 ]

**在 中建立 Secrets Manager 秘密 AWS 管理主控台**

1. 針對**來源提供者**，選擇 **Bitbucket**、**GitHub** 或 **GitHub Enterprise**。

1. 對於**登入**資料，請執行下列其中一項操作：
   + 選擇**預設來源登入**資料，以使用您帳戶的預設來源登入資料套用至所有專案。

     1. 如果您未連線到來源提供者，請選擇**管理預設來源憑證**。

     1. 針對**登入資料類型**，選擇 **CodeConnections** 以外的登入資料類型。

     1. 針對**服務**，選擇 **Secrets Manager**，針對 **Secrets** 選擇**新秘密**。

     1. 在**秘密名稱**中，輸入秘密的名稱。

     1. 在**秘密描述 - 選用**中，輸入秘密的描述。

     1. 根據您選擇的來源提供者，輸入您的字符或使用者名稱和應用程式密碼，然後選擇**儲存**。
   + 選擇**自訂來源登入**資料，以使用自訂來源登入資料覆寫您帳戶的預設設定。

     1. 針對**登入資料類型**，選擇 **CodeConnections** 以外的登入資料類型。

     1. 在**連線**中，選擇**建立秘密**。

     1. 在**秘密名稱**中，輸入秘密的名稱。

     1. 在**秘密描述 - 選用**中，輸入秘密的描述。

     1. 根據您選擇的來源提供者，輸入您的字符或使用者名稱和應用程式密碼，然後選擇**建立**。

------
#### [ AWS CLI ]

**在 中建立 Secrets Manager 秘密 AWS CLI**
+ 開啟終端機 (Linux、macOS 或 Unix) 或命令提示 (Windows)。使用 AWS CLI 執行 Secrets Manager **create-secret**命令。

  ```
  aws secretsmanager create-secret --region <aws-region> \
              --name '<secret-name>' \
              --description '<secret-description>' \
              --secret-string '{
                  "ServerType":"<server-type>",
                  "AuthType":"<auth-type>",
                  "Token":"<token>"
                  }' \
              --tags Key=codebuild:source,Value='' \
                  Key=codebuild:source:type,Value=<type> \
                  Key=codebuild:source:provider,Value=<provider>
  ```

  CodeBuild 接受的 Secrets Manager 秘密必須與 CodeBuild 專案位於相同的帳戶和 AWS 區域，且必須是下列 JSON 格式：

  ```
  {
              "ServerType": ServerType,
              "AuthType: AuthType,
              "Token": string,
              "Username": string // Optional and is only used for Bitbucket app password
          }
  ```    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/codebuild/latest/userguide/asm-create-secret.html)

  此外，CodeBuild 會在秘密上使用下列資源標籤，以確保在建立或編輯專案時可輕鬆選取秘密。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/codebuild/latest/userguide/asm-create-secret.html)

------