

# 使用 AWS CLI 的 AWS Cloud9 示例
<a name="cli_cloud9_code_examples"></a>

以下代码示例演示如何通过将 AWS Command Line Interface与 AWS Cloud9 结合使用，来执行操作和实现常见场景。

*操作是大型程序的代码摘录*，必须在上下文中运行。您可以通过操作了解如何调用单个服务函数，还可以通过函数相关场景的上下文查看操作。

每个示例都包含一个指向完整源代码的链接，您可以从中找到有关如何在上下文中设置和运行代码的说明。

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

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

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

以下代码示例演示了如何使用 `create-environment-ec2`。

**AWS CLI**  
**创建 AWS Cloud9 EC2 开发环境**  
以下 `create-environment-ec2` 示例使用指定设置创建 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_topic"></a>

以下代码示例演示了如何使用 `create-environment-membership`。

**AWS CLI**  
**将环境成员添加到 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_topic"></a>

以下代码示例演示了如何使用 `delete-environment-membership`。

**AWS CLI**  
**从 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_topic"></a>

以下代码示例演示了如何使用 `delete-environment`。

**AWS CLI**  
**删除 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_topic"></a>

以下代码示例演示了如何使用 `describe-environment-memberships`。

**AWS CLI**  
**获取有关 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"
    }
  ]
}
```
**获取有关 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_topic"></a>

以下代码示例演示了如何使用 `describe-environment-status`。

**AWS CLI**  
**获取 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_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_topic"></a>

以下代码示例演示了如何使用 `list-environments`。

**AWS CLI**  
**获取可用的 AWS Cloud9 开发环境标识符列表**  
此示例获取可用的 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_topic"></a>

以下代码示例演示了如何使用 `update-environment-membership`。

**AWS CLI**  
**更改 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_topic"></a>

以下代码示例演示了如何使用 `update-environment`。

**AWS CLI**  
**更改现有 AWS Cloud9 开发环境的设置**  
此示例更改指定现有 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)。