

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

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

# AWS Cloud9 使用 的範例 AWS CLI
<a name="cli_2_cloud9_code_examples"></a>

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

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

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

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

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

### `create-environment-ec2`
<a name="cloud9_CreateEnvironmentEc2_cli_2_topic"></a>

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

**AWS CLI**  
**建立 an AWS Cloud9 EC2 開發環境**  
以下`create-environment-ec2`範例會使用指定的設定建立 an AWS Cloud9 開發環境、啟動 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體，然後從執行個體連線至環境。  

```
aws cloud9 create-environment-ec2 \
    --name my-demo-env \
    --description "My demonstration development environment." \
    --instance-type t2.micro --image-id amazonlinux-2023-x86_64 \
    --subnet-id subnet-1fab8aEX \
    --automatic-stop-time-minutes 60 \
    --owner-arn arn:aws:iam::123456789012:user/MyDemoUser
```
輸出：  

```
{
    "environmentId": "8a34f51ce1e04a08882f1e811bd706EX"
}
```
如需詳細資訊，請參閱《*AWS Cloud9 使用者指南*》中的[建立 EC2 環境](https://docs.aws.amazon.com/cloud9/latest/user-guide/create-environment-main.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [CreateEnvironmentEc2](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloud9/create-environment-ec2.html)。

### `create-environment-membership`
<a name="cloud9_CreateEnvironmentMembership_cli_2_topic"></a>

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

**AWS CLI**  
**將環境成員新增至 an AWS Cloud9 開發環境**  
此範例會將指定的環境成員新增至指定的 AWS Cloud9 開發環境。  
命令：  

```
aws cloud9 create-environment-membership --environment-id 8a34f51ce1e04a08882f1e811bd706EX --user-arn arn:aws:iam::123456789012:user/AnotherDemoUser --permissions read-write
```
輸出：  

```
{
  "membership": {
    "environmentId": "8a34f51ce1e04a08882f1e811bd706EX",
    "userId": "AIDAJ3LOROMOUXTBSU6EX",
    "userArn": "arn:aws:iam::123456789012:user/AnotherDemoUser",
    "permissions": "read-write"
  }
}
```
+  如需 API 詳細資訊，請參閱《*AWS CLI 命令參考*》中的 [CreateEnvironmentMembership](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloud9/create-environment-membership.html)。

### `delete-environment-membership`
<a name="cloud9_DeleteEnvironmentMembership_cli_2_topic"></a>

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

**AWS CLI**  
**從 an AWS Cloud9 開發環境刪除環境成員**  
此範例會從指定的 AWS Cloud9 開發環境刪除指定的環境成員。  
命令：  

```
aws cloud9 delete-environment-membership --environment-id 8a34f51ce1e04a08882f1e811bd706EX --user-arn arn:aws:iam::123456789012:user/AnotherDemoUser
```
輸出：  

```
None.
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [DeleteEnvironmentMembership](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloud9/delete-environment-membership.html)。

### `delete-environment`
<a name="cloud9_DeleteEnvironment_cli_2_topic"></a>

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

**AWS CLI**  
**刪除 an AWS Cloud9 開發環境**  
此範例會刪除指定的 AWS Cloud9 開發環境。如果 Amazon EC2 執行個體連線至環境，該執行個體也會終止。  
命令：  

```
aws cloud9 delete-environment --environment-id 8a34f51ce1e04a08882f1e811bd706EX
```
輸出：  

```
None.
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [DeleteEnvironment](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloud9/delete-environment.html)。

### `describe-environment-memberships`
<a name="cloud9_DescribeEnvironmentMemberships_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `describe-environment-memberships`。

**AWS CLI**  
**取得有關 an AWS Cloud9 開發環境的環境成員資訊**  
此範例會取得指定 AWS Cloud9 開發環境的環境成員相關資訊。  
命令：  

```
aws cloud9 describe-environment-memberships --environment-id 8a34f51ce1e04a08882f1e811bd706EX
```
輸出：  

```
{
  "memberships": [
    {
      "environmentId": "8a34f51ce1e04a08882f1e811bd706EX",
      "userId": "AIDAJ3LOROMOUXTBSU6EX",
      "userArn": "arn:aws:iam::123456789012:user/AnotherDemoUser",
      "permissions": "read-write"
    },
    {
      "environmentId": "8a34f51ce1e04a08882f1e811bd706EX",
      "userId": "AIDAJNUEDQAQWFELJDLEX",
      "userArn": "arn:aws:iam::123456789012:user/MyDemoUser",
      "permissions": "owner"
    }
  ]
}
```
**取得有關 an AWS Cloud9 開發環境擁有者的資訊**  
此範例會取得指定 AWS Cloud9 開發環境擁有者的相關資訊。  
命令：  

```
aws cloud9 describe-environment-memberships --environment-id 8a34f51ce1e04a08882f1e811bd706EX --permissions owner
```
輸出：  

```
{
  "memberships": [
    {
      "environmentId": "8a34f51ce1e04a08882f1e811bd706EX",
      "userId": "AIDAJNUEDQAQWFELJDLEX",
      "userArn": "arn:aws:iam::123456789012:user/MyDemoUser",
      "permissions": "owner"
    }
  ]
}
```
**取得多個 AWS Cloud9 開發環境的環境成員相關資訊**  
此範例會取得多個 AWS Cloud9 開發環境之指定環境成員的相關資訊。  
命令：  

```
aws cloud9 describe-environment-memberships --user-arn arn:aws:iam::123456789012:user/MyDemoUser
```
輸出：  

```
{
  "memberships": [
    {
      "environmentId": "10a75714bd494714929e7f5ec4125aEX",
      "lastAccess": 1516213427.0,
      "userId": "AIDAJNUEDQAQWFELJDLEX",
      "userArn": "arn:aws:iam::123456789012:user/MyDemoUser",
      "permissions": "owner"
    },
    {
      "environmentId": "1980b80e5f584920801c09086667f0EX",
      "lastAccess": 1516144884.0,
      "userId": "AIDAJNUEDQAQWFELJDLEX",
      "userArn": "arn:aws:iam::123456789012:user/MyDemoUser",
      "permissions": "owner"
    }
  ]
}
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [DescribeEnvironmentMemberships](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloud9/describe-environment-memberships.html)。

### `describe-environment-status`
<a name="cloud9_DescribeEnvironmentStatus_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `describe-environment-status`。

**AWS CLI**  
**取得 an AWS Cloud9 開發環境的狀態資訊**  
此範例會取得指定 AWS Cloud9 開發環境的狀態資訊。  
命令：  

```
aws cloud9 describe-environment-status --environment-id 685f892f431b45c2b28cb69eadcdb0EX
```
輸出：  

```
{
  "status": "ready",
  "message": "Environment is ready to use"
}
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [DescribeEnvironmentStatus](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloud9/describe-environment-status.html)。

### `describe-environments`
<a name="cloud9_DescribeEnvironments_cli_2_topic"></a>

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

**AWS CLI**  
**取得 AWS Cloud9 開發環境的相關資訊**  
此範例會取得指定 AWS Cloud9 開發環境的相關資訊。  
命令：  

```
aws cloud9 describe-environments --environment-ids 685f892f431b45c2b28cb69eadcdb0EX 349c86d4579e4e7298d500ff57a6b2EX
```
輸出：  

```
{
  "environments": [
    {
      "id": "685f892f431b45c2b28cb69eadcdb0EX",
      "name": "my-demo-ec2-env",
      "description": "Created from CodeStar.",
      "type": "ec2",
      "arn": "arn:aws:cloud9:us-east-1:123456789012:environment:685f892f431b45c2b28cb69eadcdb0EX",
      "ownerArn": "arn:aws:iam::123456789012:user/MyDemoUser",
      "lifecycle": {
        "status": "CREATED"
      }
    },
    {
      "id": "349c86d4579e4e7298d500ff57a6b2EX",
      "name": my-demo-ssh-env",
      "description": "",
      "type": "ssh",
      "arn": "arn:aws:cloud9:us-east-1:123456789012:environment:349c86d4579e4e7298d500ff57a6b2EX",
      "ownerArn": "arn:aws:iam::123456789012:user/MyDemoUser",
      "lifecycle": {
        "status": "CREATED"
      }
    }
  ]
}
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [DescribeEnvironments](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloud9/describe-environments.html)。

### `list-environments`
<a name="cloud9_ListEnvironments_cli_2_topic"></a>

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

**AWS CLI**  
**取得 available AWS Cloud9 開發環境識別符的清單**  
此範例會取得 available AWS Cloud9 開發環境識別符的清單。  
命令：  

```
aws cloud9 list-environments
```
輸出：  

```
{
  "environmentIds": [
    "685f892f431b45c2b28cb69eadcdb0EX",
    "1980b80e5f584920801c09086667f0EX"
  ]
}
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [ListEnvironments](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloud9/list-environments.html)。

### `update-environment-membership`
<a name="cloud9_UpdateEnvironmentMembership_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `update-environment-membership`。

**AWS CLI**  
**變更 an AWS Cloud9 開發環境現有環境成員的設定**  
此範例會變更指定 AWS Cloud9 開發環境之指定現有環境成員的設定。  
命令：  

```
aws cloud9 update-environment-membership --environment-id 8a34f51ce1e04a08882f1e811bd706EX --user-arn arn:aws:iam::123456789012:user/AnotherDemoUser --permissions read-only
```
輸出：  

```
{
  "membership": {
    "environmentId": "8a34f51ce1e04a08882f1e811bd706EX",
    "userId": "AIDAJ3LOROMOUXTBSU6EX",
    "userArn": "arn:aws:iam::123456789012:user/AnotherDemoUser",
    "permissions": "read-only"
  }
}
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [UpdateEnvironmentMembership](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloud9/update-environment-membership.html)。

### `update-environment`
<a name="cloud9_UpdateEnvironment_cli_2_topic"></a>

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

**AWS CLI**  
**變更現有 AWS Cloud9 開發環境的設定**  
此範例會變更指定之 existing AWS Cloud9 開發環境的指定設定。  
命令：  

```
aws cloud9 update-environment --environment-id 8a34f51ce1e04a08882f1e811bd706EX --name my-changed-demo-env --description "My changed demonstration development environment."
```
輸出：  

```
None.
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [UpdateEnvironment](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloud9/update-environment.html)。