

文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 [AWS SDK 範例](https://github.com/awsdocs/aws-doc-sdk-examples)。

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

# 使用 的 CodeConnections 範例 AWS CLI
<a name="cli_2_codestar-connections_code_examples"></a>

下列程式碼範例示範如何使用 AWS Command Line Interface 搭配 CodeConnections 來執行動作和實作常見案例。

*Actions* 是大型程式的程式碼摘錄，必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數，但您可以在其相關情境中查看內容中的動作。

每個範例均包含完整原始碼的連結，您可在連結中找到如何設定和執行內容中程式碼的相關指示。

**Topics**
+ [動作](#actions)

## 動作
<a name="actions"></a>

### `create-connection`
<a name="codestar-connections_CreateConnection_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `create-connection`。

**AWS CLI**  
**建立連線**  
下列 `create-connection` 範例示範如何建立與第三方儲存庫的連線。此範例會建立第三方提供者為 Bitbucket 的連線。  
根據預設，透過 CLI AWS 或 AWS CloudFormation 建立的連線處於待定狀態。建立與 CLI 或 AWS CloudFormation 的連線後，請使用 主控台編輯連線，使其狀態為可用。  

```
aws codestar-connections create-connection \
    --provider-type Bitbucket \
    --connection-name MyConnection
```
輸出：  

```
{
    "ConnectionArn": "arn:aws:codestar-connections:us-east-1:123456789012:connection/aEXAMPLE-8aad-4d5d-8878-dfcab0bc441f"
}
```
如需詳細資訊，請參閱《*開發人員工具主控台使用者指南*》中的[建立連線](https://docs.aws.amazon.com/dtconsole/latest/userguide/connections-create.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [CreateConnection](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-connections/create-connection.html)。

### `create-host`
<a name="codestar-connections_CreateHost_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `create-host`。

**AWS CLI**  
**建立主機**  
下列`create-host` 範例顯示如何建立主機來代表安裝第三方提供者基礎設施的端點。此範例會建立主機，其中第三方安裝的提供者是 GitHub Enterprise Server。  
根據預設，透過 CLI AWS 建立的主機處於待定狀態。使用 CLI 建立主機後，需使用主控台或 CLI 設定主機，使其狀態成為 Available (可用)。  

```
aws codestar-connections create-host \
    --name MyHost \
    --provider-type GitHubEnterpriseServer \
    --provider-endpoint "https://my-instance.dev"
```
輸出：  

```
{
    "HostArn": "arn:aws:codestar-connections:us-east-1:123456789012:host/My-Host-28aef605"
}
```
如需詳細資訊，請參閱《*開發人員工具主控台使用者指南*》中的[建立主機 (CLI)](https://docs.aws.amazon.com/dtconsole/latest/userguide/connections-host-create.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [CreateHost](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-connections/create-host.html)。

### `delete-connection`
<a name="codestar-connections_DeleteConnection_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `delete-connection`。

**AWS CLI**  
**刪除連線**  
下列 `delete-connection` 範例示範如何刪除連線。  

```
aws codestar-connections delete-connection \
    --connection-arn arn:aws:codestar-connections:us-west-2:123456789012:connection/aEXAMPLE-8aad-4d5d-8878-dfcab0bc441f
```
此命令不會產生輸出。  
如需詳細資訊，請參閱《*開發人員工具主控台使用者指南*》中的[刪除連線 (CLI)](https://docs.aws.amazon.com/dtconsole/latest/userguide/connections-delete.html#connections-delete-cli)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [DeleteConnection](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-connections/delete-connection.html)。

### `delete-host`
<a name="codestar-connections_DeleteHost_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `delete-host`。

**AWS CLI**  
**刪除主機**  
下列 `delete-host` 範例顯示如何刪除主機。您必須先刪除與主機相關聯的所有連線，才能刪除該主機。  

```
aws codestar-connections delete-host \
    --host-arn "arn:aws:codestar-connections:us-east-1 :123456789012:host/My-Host-28aef605"
```
此命令不會產生輸出。  
如需詳細資訊，請參閱《*開發人員工具主控台使用者指南*》中的[刪除主機 (CLI)](https://docs.aws.amazon.com/dtconsole/latest/userguide/connections-host-delete.html#connections-host-delete-cli)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [DeleteHost](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-connections/delete-host.html)。

### `get-connection`
<a name="codestar-connections_GetConnection_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `get-connection`。

**AWS CLI**  
**取得連線的相關資訊**  
下列 `get-connection` 範例顯示連線的詳細資訊。  

```
aws codestar-connections get-connection \
    --connection-arn arn:aws:codestar-connections:us-east-1:123456789012:connection/aEXAMPLE-8aad-4d5d-8878-dfcab0bc441f
```
輸出：  

```
{
    "Connection": {
        "ConnectionName": "MyConnection",
        "ConnectionArn": "arn:aws:codestar-connections:us-east-1:123456789012:connection/aEXAMPLE-8aad-4d5d-8878-dfcab0bc441f",
        "ProviderType": "Bitbucket",
        "OwnerAccountId": "123456789012",
        "ConnectionStatus": "AVAILABLE"
    }
}
```
如需詳細資訊，請參閱《*開發人員工具主控台使用者指南*》中的[檢視連線詳細資訊](https://docs.aws.amazon.com/dtconsole/latest/userguide/connections-view-details.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [GetConnection](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-connections/get-connection.html)。

### `get-host`
<a name="codestar-connections_GetHost_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `get-host`。

**AWS CLI**  
**取得主機的相關資訊**  
下列 `get-host` 範例顯示主機的詳細資訊。  

```
aws codestar-connections get-host \
    --host-arn arn:aws:codestar-connections:us-east-1:123456789012:host/MyHost-28aef605
```
輸出：  

```
{
    "Name": "MyHost",
    "Status": "AVAILABLE",
    "ProviderType": "GitHubEnterpriseServer",
    "ProviderEndpoint": "https://test-instance-1.dev/"
}
```
如需詳細資訊，請參閱《*開發人員工具主控台使用者指南*》中的[檢視主機的相關資訊 (CLI)](https://docs.aws.amazon.com/dtconsole/latest/userguide/connections-host-view.html#connections-host-view-cli)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [GetHost](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-connections/get-host.html)。

### `list-connections`
<a name="codestar-connections_ListConnections_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `list-connections`。

**AWS CLI**  
**列出連線**  
下列 `list-connections` 範例擷取您帳戶中所有 Bitbucket 提供者類型的連線清單：  

```
aws codestar-connections list-connections \
--provider-type Bitbucket \
--max-results 5 \
--next-token: next-token
```
輸出：  

```
{
    "Connections": [
        {
            "ConnectionName": "my-connection",
            "ProviderType": "Bitbucket",
            "Status": "PENDING",
            "ARN": "arn:aws:codestar-connections:us-east-1:123456789012:connection/aEXAMPLE-8aad-4d5d-8878-dfcab0bc441f",
            "OwnerAccountId": "123456789012"
        },
        {
            "ConnectionName": "my-other-connection",
            "ProviderType": "Bitbucket",
            "Status": "AVAILABLE",
            "ARN": "arn:aws:codestar-connections:us-east-1:123456789012:connection/aEXAMPLE-8aad-4d5d-8878-dfcab0bc441f",
            "OwnerAccountId": "123456789012"
        },
    ],
    "NextToken": "next-token"
}
```
如需詳細資訊，請參閱《*開發人員工具主控台使用者指南*》中的[列出連線 (CLI)](https://docs.aws.amazon.com/dtconsole/latest/userguide/connections-list.html#connections-list-cli)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [ListConnections](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-connections/list-connections.html)。

### `list-hosts`
<a name="codestar-connections_ListHosts_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `list-hosts`。

**AWS CLI**  
**列出主機**  
下列 `list-hosts` 範例擷取您帳戶中的所有主機清單。  

```
aws codestar-connections list-hosts
```
輸出：  

```
{
    "Hosts": [
        {
            "Name": "My-Host",
            "HostArn": "arn:aws:codestar-connections:us-east-1:123456789012:host/My-Host-28aef605",
            "ProviderType": "GitHubEnterpriseServer",
            "ProviderEndpoint": "https://my-instance.test.dev",
            "Status": "AVAILABLE"
        }
    ]
}
```
如需詳細資訊，請參閱《*開發人員工具主控台使用者指南*》中的[列出主機 (CLI)](https://docs.aws.amazon.com/dtconsole/latest/userguide/connections-host-list.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [ListHosts](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-connections/list-hosts.html)。

### `list-tags-for-resource`
<a name="codestar-connections_ListTagsForResource_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `list-tags-for-resource`。

**AWS CLI**  
**列出標籤**  
下列 `list-tags-for-resource` 範例擷取連接至指定連線資源的所有標籤清單。  

```
aws codestar-connections list-tags-for-resource \
    --resource-arn arn:aws:codestar-connections:us-east-1:123456789012:connection/aEXAMPLE-8aad-4d5d-8878-dfcab0bc441f
```
輸出：  

```
{
    "Tags": [
        {
            "Key": "Project",
            "Value": "ProjectA"
        },
        {
            "Key": "ReadOnly",
            "Value": "true"
        }
    ]
}
```
如需詳細資訊，請參閱《*開發人員工具主控台使用者指南*》中的[檢視連線資源的標籤](https://docs.aws.amazon.com/dtconsole/latest/userguide/connections-tag.html#connections-tag-view)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [ListTagsForResource](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-connections/list-tags-for-resource.html)。

### `tag-resource`
<a name="codestar-connections_TagResource_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `tag-resource`。

**AWS CLI**  
**標記資源**  
下列 `tag-resource` 範例會建立一組提供的標籤與連線的關聯。使用此命令新增或編輯標籤。  

```
aws codestar-connections tag-resource \
    --resource-arn arn:aws:codestar-connections:us-east-1:123456789012:connection/aEXAMPLE-8aad-4d5d-8878-dfcab0bc441f \
    --tags Key=Project,Value=ProjectA Key=IscontainerBased,Value=true
```
此命令不會產生輸出。  
如需詳細資訊，請參閱《*開發人員工具主控台使用者指南*》中的[將標籤新增至連線資源](https://docs.aws.amazon.com/dtconsole/latest/userguide/connections-tag.html#connections-tag-add)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [TagResource](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-connections/tag-resource.html)。

### `untag-resource`
<a name="codestar-connections_UntagResource_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `untag-resource`。

**AWS CLI**  
**從連線資源移除 AWS 標籤**  
以下 `untag-resource` 會從指定的資源移除標籤。  

```
aws codestar-connections untag-resource \
    --resource-arn arn:aws:codestar-connections:us-east-1:123456789012:connection/aEXAMPLE-8aad-4d5d-8878-dfcab0bc441f \
    --tag-keys Project ReadOnly
```
輸出：  

```
{
    "Tags": []
}
```
如需詳細資訊，請參閱《*開發人員工具主控台使用者指南*》中的[將標籤從連線資源移除](https://docs.aws.amazon.com/dtconsole/latest/userguide/connections-tag.html#connections-tag-delete)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [UntagResource](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-connections/untag-resource.html)。