

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

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

# AWS Proton 使用 的範例 AWS CLI
<a name="cli_2_proton_code_examples"></a>

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

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

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

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

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

### `cancel-service-instance-deployment`
<a name="proton_CancelServiceInstanceDeployment_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `cancel-service-instance-deployment`。

**AWS CLI**  
**取消服務執行個體部署**  
下列 `cancel-service-instance-deployment` 範例會取消服務執行個體部署。  

```
aws proton cancel-service-instance-deployment \
    --service-instance-name "instance-one" \
    --service-name "simple-svc"
```
輸出：  

```
{
    "serviceInstance": {
        "arn": "arn:aws:proton:region-id:123456789012:service/simple-svc/service-instance/instance-one",
        "createdAt": "2021-04-02T21:29:59.962000+00:00",
        "deploymentStatus": "CANCELLING",
        "environmentName": "simple-env",
        "lastDeploymentAttemptedAt": "2021-04-02T21:45:15.406000+00:00",
        "lastDeploymentSucceededAt": "2021-04-02T21:38:00.823000+00:00",
        "name": "instance-one",
        "serviceName": "simple-svc",
        "spec": "proton: ServiceSpec\npipeline:\n  my_sample_pipeline_optional_input: abc\n  my_sample_pipeline_required_input: '123'\ninstances:\n- name: my-instance\n  environment: MySimpleEnv\n  spec:\n    my_sample_service_instance_optional_input: def\n    my_sample_service_instance_required_input: '456'\n- name: my-other-instance\n  environment: MySimpleEnv\n  spec:\n    my_sample_service_instance_required_input: '789'\n",
        "templateMajorVersion": "1",
        "templateMinorVersion": "1",
        "templateName": "svc-simple"
    }
}
```
如需詳細資訊，請參閱《* AWS Proton 管理員指南*》中的[更新服務執行個體](https://docs.aws.amazon.com/proton/latest/adminguide/ag-svc-instance-update.html)或《* AWS Proton 使用者指南*》中的[更新服務執行個體](https://docs.aws.amazon.com/proton/latest/userguide/ug-svc-instance-update.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [CancelServiceInstanceDeployment](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/proton/cancel-service-instance-deployment.html)。

### `cancel-service-pipeline-deployment`
<a name="proton_CancelServicePipelineDeployment_cli_2_topic"></a>

以下程式碼範例顯示如何使用 `cancel-service-pipeline-deployment`。

**AWS CLI**  
**取消服務管道部署**  
下列 `cancel-service-pipeline-deployment` 範例會取消服務管道部署。  

```
aws proton cancel-service-pipeline-deployment \
    --service-name "simple-svc"
```
輸出：  

```
{
    "pipeline": {
        "arn": "arn:aws:proton:region-id:123456789012:service/simple-svc/pipeline",
        "createdAt": "2021-04-02T21:29:59.962000+00:00",
        "deploymentStatus": "CANCELLING",
        "lastDeploymentAttemptedAt": "2021-04-02T22:02:45.095000+00:00",
        "lastDeploymentSucceededAt": "2021-04-02T21:39:28.991000+00:00",
        "templateMajorVersion": "1",
        "templateMinorVersion": "1",
        "templateName": "svc-simple"
    }
}
```
如需詳細資訊，請參閱《* AWS Proton 管理員指南*》中的[更新服務管道](https://docs.aws.amazon.com/proton/latest/adminguide/ag-svc-pipeline-update.html)，或《* AWS Proton 使用者指南*》中的[更新服務管道](https://docs.aws.amazon.com/proton/latest/userguide/ag-svc-pipeline-update.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [CancelServicePipelineDeployment](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/proton/cancel-service-pipeline-deployment.html)。

### `create-service`
<a name="proton_CreateService_cli_2_topic"></a>

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

**AWS CLI**  
**建立服務**  
下列 `create-service` 範例會使用服務管道建立服務。  

```
aws proton create-service \
    --name "MySimpleService" \
    --template-name "fargate-service" \
    --template-major-version "1" \
    --branch-name "mainline" \
    --repository-connection-arn "arn:aws:codestar-connections:region-id:account-id:connection/a1b2c3d4-5678-90ab-cdef-EXAMPLE11111" \
    --repository-id "myorg/myapp" \
    --spec file://spec.yaml
```
`spec.yaml` 的內容：  

```
proton: ServiceSpec

pipeline:
    my_sample_pipeline_required_input: "hello"
    my_sample_pipeline_optional_input: "bye"

instances:
    - name: "acme-network-dev"
        environment: "ENV_NAME"
        spec:
            my_sample_service_instance_required_input: "hi"
            my_sample_service_instance_optional_input: "ho"
```
輸出：  

```
{
    "service": {
        "arn": "arn:aws:proton:region-id:123456789012:service/MySimpleService",
        "createdAt": "2020-11-18T19:50:27.460000+00:00",
        "lastModifiedAt": "2020-11-18T19:50:27.460000+00:00",
        "name": "MySimpleService",
        "repositoryConnectionArn": "arn:aws:codestar-connections:region-id:123456789012connection/a1b2c3d4-5678-90ab-cdef-EXAMPLE11111",
        "repositoryId": "myorg/myapp",
        "status": "CREATE_IN_PROGRESS",
        "templateName": "fargate-service"
    }
}
```
如需詳細資訊，請參閱《* AWS Proton 管理員指南*》中的[建立服務](https://docs.aws.amazon.com/proton/latest/adminguide/ag-create-svc.html)，以及《* AWS Proton 使用者指南*》中的[建立服務](https://docs.aws.amazon.com/proton/latest/userguide/ug-svc-create.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [CreateService](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/proton/create-service.html)。

### `delete-service`
<a name="proton_DeleteService_cli_2_topic"></a>

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

**AWS CLI**  
**刪除服務**  
下列 `delete-service` 範例會刪除服務。  

```
aws proton delete-service \
    --name "simple-svc"
```
輸出：  

```
{
    "service": {
        "arn": "arn:aws:proton:region-id:123456789012:service/simple-svc",
        "branchName": "mainline",
        "createdAt": "2020-11-28T22:40:50.512000+00:00",
        "description": "Edit by updating description",
        "lastModifiedAt": "2020-11-29T00:30:39.248000+00:00",
        "name": "simple-svc",
        "repositoryConnectionArn": "arn:aws:codestar-connections:region-id:123456789012:connection/a1b2c3d4-5678-90ab-cdef-EXAMPLE11111",
        "repositoryId": "myorg/myapp",
        "status": "DELETE_IN_PROGRESS",
        "templateName": "fargate-service"
    }
}
```
如需詳細資訊，請參閱《* AWS Proton 管理員指南*》中的[刪除服務](https://docs.aws.amazon.com/proton/latest/adminguide/ag-svc-delete.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [DeleteService](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/proton/delete-service.html)。

### `get-service-instance`
<a name="proton_GetServiceInstance_cli_2_topic"></a>

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

**AWS CLI**  
**取得執行個體詳細資訊**  
下列 `get-service-instance` 範例會取得服務執行個體的詳細資訊。  

```
aws proton get-service-instance \
    --name "instance-one" \
    --service-name "simple-svc"
```
輸出：  

```
{
    "serviceInstance": {
        "arn": "arn:aws:proton:region-id:123456789012:service/simple-svc/service-instance/instance-one",
        "createdAt": "2020-11-28T22:40:50.512000+00:00",
        "deploymentStatus": "SUCCEEDED",
        "environmentName": "simple-env",
        "lastDeploymentAttemptedAt": "2020-11-28T22:40:50.512000+00:00",
        "lastDeploymentSucceededAt": "2020-11-28T22:40:50.512000+00:00",
        "name": "instance-one",
        "serviceName": "simple-svc",
        "spec": "proton: ServiceSpec\npipeline:\n  my_sample_pipeline_optional_input: hello world\n  my_sample_pipeline_required_input: pipeline up\ninstances:\n- name: instance-one\n  environment: my-simple-env\n  spec:\n    my_sample_service_instance_optional_input: Ola\n    my_sample_service_instance_required_input: Ciao\n",
        "templateMajorVersion": "1",
        "templateMinorVersion": "0",
        "templateName": "svc-simple"
    }
}
```
如需詳細資訊，請參閱《* AWS Proton 管理員指南*》中的[檢視服務資料](https://docs.aws.amazon.com/proton/latest/adminguide/ag-svc-view.html)或《* AWS Proton 使用者指南*》中的[檢視服務資料](https://docs.aws.amazon.com/proton/latest/userguide/ug-svc-view.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [GetServiceInstance](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/proton/get-service-instance.html)。

### `get-service`
<a name="proton_GetService_cli_2_topic"></a>

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

**AWS CLI**  
**取得服務詳細資訊**  
下列 `get-service` 範例會取得服務的詳細資訊。  

```
aws proton get-service \
    --name "simple-svc"
```
輸出：  

```
{
    "service": {
        "arn": "arn:aws:proton:region-id:123456789012:service/simple-svc",
        "branchName": "mainline",
        "createdAt": "2020-11-28T22:40:50.512000+00:00",
        "lastModifiedAt": "2020-11-28T22:44:51.207000+00:00",
        "name": "simple-svc",
        "pipeline": {
            "arn": "arn:aws:proton:region-id:123456789012:service/simple-svc/pipeline/a1b2c3d4-5678-90ab-cdef-EXAMPLE11111",
            "createdAt": "2020-11-28T22:40:50.512000+00:00",
            "deploymentStatus": "SUCCEEDED",
            "lastDeploymentAttemptedAt": "2020-11-28T22:40:50.512000+00:00",
            "lastDeploymentSucceededAt": "2020-11-28T22:40:50.512000+00:00",
            "spec": "proton: ServiceSpec\npipeline:\n  my_sample_pipeline_required_input: hello\n  my_sample_pipeline_optional_input: bye\ninstances:\n- name: instance-svc-simple\n  environment: my-simple-env\n  spec:\n    my_sample_service_instance_required_input: hi\n    my_sample_service_instance_optional_input: ho\n",
            "templateMajorVersion": "1",
            "templateMinorVersion": "1",
            "templateName": "svc-simple"
        },
        "repositoryConnectionArn": "arn:aws:codestar-connections:region-id:123456789012:connection/a1b2c3d4-5678-90ab-cdef-EXAMPLE22222",
        "repositoryId": "myorg/myapp",
        "spec": "proton: ServiceSpec\npipeline:\n  my_sample_pipeline_required_input: hello\n  my_sample_pipeline_optional_input: bye\ninstances:\n- name: instance-svc-simple\n  environment: my-simple-env\n  spec:\n    my_sample_service_instance_required_input: hi\n    my_sample_service_instance_optional_input: ho\n",
        "status": "ACTIVE",
        "templateName": "svc-simple"
    }
}
```
如需詳細資訊，請參閱《* AWS Proton 管理員指南*》中的[檢視服務資料](https://docs.aws.amazon.com/proton/latest/adminguide/ag-svc-view.html)或《* AWS Proton 使用者指南*》中的[檢視服務資料](https://docs.aws.amazon.com/proton/latest/userguide/ug-svc-view.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [GetService](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/proton/get-service.html)。

### `list-service-instances`
<a name="proton_ListServiceInstances_cli_2_topic"></a>

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

**AWS CLI**  
**範例 1：列出所有服務執行個體**  
下列 `list-service-instances` 範例列出服務執行個體。  

```
aws proton list-service-instances
```
輸出：  

```
{
    "serviceInstances": [
        {
            "arn": "arn:aws:proton:region-id:123456789012:service/simple-svc/service-instance/instance-one",
            "createdAt": "2020-11-28T22:40:50.512000+00:00",
            "deploymentStatus": "SUCCEEDED",
            "environmentArn": "arn:aws:proton:region-id:123456789012:environment/simple-env",
            "lastDeploymentAttemptedAt": "2020-11-28T22:40:50.512000+00:00",
            "lastDeploymentSucceededAt": "2020-11-28T22:40:50.512000+00:00",
            "name": "instance-one",
            "serviceName": "simple-svc",
            "templateMajorVersion": "1",
            "templateMinorVersion": "0",
            "templateName": "fargate-service"
        }
    ]
}
```
如需詳細資訊，請參閱《* AWS Proton 管理員指南*》中的[檢視服務執行個體資料](https://docs.aws.amazon.com/proton/latest/adminguide/ag-svc-instance-view.html)，或《* AWS Proton 使用者指南*》中的[檢視服務執行個體資料](https://docs.aws.amazon.com/proton/latest/userguide/ag-svc-instance-view.html)。  
**範例 2：列出指定的服務執行個體**  
下列 `get-service-instance` 範例會取得服務執行個體。  

```
aws proton get-service-instance \
    --name "instance-one" \
    --service-name "simple-svc"
```
輸出：  

```
{
    "serviceInstance": {
        "arn": "arn:aws:proton:region-id:123456789012:service/simple-svc/service-instance/instance-one",
        "createdAt": "2020-11-28T22:40:50.512000+00:00",
        "deploymentStatus": "SUCCEEDED",
        "environmentName": "simple-env",
        "lastDeploymentAttemptedAt": "2020-11-28T22:40:50.512000+00:00",
        "lastDeploymentSucceededAt": "2020-11-28T22:40:50.512000+00:00",
        "name": "instance-one",
        "serviceName": "simple-svc",
        "spec": "proton: ServiceSpec\npipeline:\n  my_sample_pipeline_optional_input: hello world\n  my_sample_pipeline_required_input: pipeline up\ninstances:\n- name: instance-one\n  environment: my-simple-env\n  spec:\n    my_sample_service_instance_optional_input: Ola\n    my_sample_service_instance_required_input: Ciao\n",
        "templateMajorVersion": "1",
        "templateMinorVersion": "0",
        "templateName": "svc-simple"
    }
}
```
如需詳細資訊，請參閱《* AWS Proton 管理員指南*》中的[檢視服務執行個體資料](https://docs.aws.amazon.com/proton/latest/adminguide/ag-svc-instance-view.html)，或《* AWS Proton 使用者指南*》中的[檢視服務執行個體資料](https://docs.aws.amazon.com/proton/latest/userguide/ag-svc-instance-view.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [ListServiceInstances](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/proton/list-service-instances.html)。

### `update-service-instance`
<a name="proton_UpdateServiceInstance_cli_2_topic"></a>

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

**AWS CLI**  
**將服務執行個體更新為新的次要版本**  
下列 `update-service-instance` 範例會將服務執行個體更新至其服務範本的新次要版本，新增名為 "my-other-instance" 的新執行個體與新的必要輸入。  

```
aws proton update-service-instance \
    --service-name "simple-svc" \
    --spec "file://service-spec.yaml " \
    --template-major-version "1" \
    --template-minor-version "1" \
    --deployment-type "MINOR_VERSION" \
    --name "instance-one"
```
`service-spec.yaml` 的內容：  

```
proton: ServiceSpec
pipeline:
    my_sample_pipeline_optional_input: "abc"
    my_sample_pipeline_required_input: "123"
instances:
    - name: "instance-one"
        environment: "simple-env"
        spec:
            my_sample_service_instance_optional_input: "def"
            my_sample_service_instance_required_input: "456"
    - name: "my-other-instance"
        environment: "simple-env"
        spec:
            my_sample_service_instance_required_input: "789"
```
輸出：  

```
{
    "serviceInstance": {
        "arn": "arn:aws:proton:region-id:123456789012:service/simple-svc/service-instance/instance-one",
        "createdAt": "2021-04-02T21:29:59.962000+00:00",
        "deploymentStatus": "IN_PROGRESS",
        "environmentName": "arn:aws:proton:region-id:123456789012:environment/simple-env",
        "lastDeploymentAttemptedAt": "2021-04-02T21:38:00.823000+00:00",
        "lastDeploymentSucceededAt": "2021-04-02T21:29:59.962000+00:00",
        "name": "instance-one",
        "serviceName": "simple-svc",
        "templateMajorVersion": "1",
        "templateMinorVersion": "0",
        "templateName": "svc-simple"
    }
}
```
如需詳細資訊，請參閱《* AWS Proton 管理員指南*》中的[更新服務執行個體](https://docs.aws.amazon.com/proton/latest/adminguide/ag-svc-instance-update.html)或《* AWS Proton 使用者指南*》中的[更新服務執行個體](https://docs.aws.amazon.com/proton/latest/userguide/ag-svc-instance-update.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [UpdateServiceInstance](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/proton/update-service-instance.html)。

### `update-service-pipeline`
<a name="proton_UpdateServicePipeline_cli_2_topic"></a>

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

**AWS CLI**  
**更新服務管道**  
下列 `update-service-pipeline` 範例會將服務管道更新為其服務範本的新次要版本。  

```
aws proton update-service-pipeline \
    --service-name "simple-svc" \
    --spec "file://service-spec.yaml" \
    --template-major-version "1" \
    --template-minor-version "1" \
    --deployment-type "MINOR_VERSION"
```
輸出：  

```
{
    "pipeline": {
        "arn": "arn:aws:proton:region-id:123456789012:service/simple-svc/pipeline/a1b2c3d4-5678-90ab-cdef-EXAMPLE11111",
        "createdAt": "2021-04-02T21:29:59.962000+00:00",
        "deploymentStatus": "IN_PROGRESS",
        "lastDeploymentAttemptedAt": "2021-04-02T21:39:28.991000+00:00",
        "lastDeploymentSucceededAt": "2021-04-02T21:29:59.962000+00:00",
        "spec": "proton: ServiceSpec\n\npipeline:\n  my_sample_pipeline_optional_input: \"abc\"\n  my_sample_pipeline_required_input: \"123\"\n\ninstances:\n  - name: \"my-instance\"\n    environment: \"MySimpleEnv\"\n    spec:\n      my_sample_service_instance_optional_input: \"def\"\n      my_sample_service_instance_required_input: \"456\"\n  - name: \"my-other-instance\"\n    environment: \"MySimpleEnv\"\n    spec:\n      my_sample_service_instance_required_input: \"789\"\n",
        "templateMajorVersion": "1",
        "templateMinorVersion": "0",
        "templateName": "svc-simple"
    }
}
```
如需詳細資訊，請參閱《* AWS Proton 管理員指南*》中的[更新服務管道](https://docs.aws.amazon.com/proton/latest/adminguide/ag-svc-pipeline-update.html)，或《* AWS Proton 使用者指南*》中的[更新服務管道](https://docs.aws.amazon.com/proton/latest/userguide/ag-svc-pipeline-update.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [UpdateServicePipeline](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/proton/update-service-pipeline.html)。

### `update-service`
<a name="proton_UpdateService_cli_2_topic"></a>

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

**AWS CLI**  
**更新服務**  
下列 `update-service` 範例會編輯服務描述。  

```
aws proton update-service \
    --name "MySimpleService" \
    --description "Edit by updating description"
```
輸出：  

```
{
    "service": {
        "arn": "arn:aws:proton:region-id:123456789012:service/MySimpleService",
        "branchName": "mainline",
        "createdAt": "2021-03-12T22:39:42.318000+00:00",
        "description": "Edit by updating description",
        "lastModifiedAt": "2021-03-12T22:44:21.975000+00:00",
        "name": "MySimpleService",
        "repositoryConnectionArn": "arn:aws:codestar-connections:region-id:123456789012:connection/a1b2c3d4-5678-90ab-cdef-EXAMPLE11111",
        "repositoryId": "myorg/myapp",
        "status": "ACTIVE",
        "templateName": "fargate-service"
    }
}
```
如需詳細資訊，請參閱《* AWS Proton 管理員指南*》中的[編輯服務](https://docs.aws.amazon.com/proton/latest/adminguide/ag-svc-update.html)或《* AWS Proton 使用者指南*》中的[編輯服務](https://docs.aws.amazon.com/proton/latest/userguide/ug-svc-update.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [UpdateService](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/proton/update-service.html)。