

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

# 使用 Application Load Balancer 在 Amazon ECS 中使用交互 TLS 簡化應用程式身分驗證
<a name="simplify-application-authentication-with-mutual-tls-in-amazon-ecs"></a>

*Olawale Olaleye 和 Shamanth Devagari，Amazon Web Services*

## 總結
<a name="simplify-application-authentication-with-mutual-tls-in-amazon-ecs-summary"></a>

此模式可協助您使用 Application [Application Load Balancer (ALB)](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/mutual-authentication.html)，透過 Amazon Elastic Container Service (Amazon ECS) 中的交互 TLS，簡化應用程式身分驗證並卸載安全負擔。使用 ALB，您可以從中驗證 X.509 用戶端憑證 AWS 私有憑證授權單位。這種強大的組合有助於實現服務之間的安全通訊，減少應用程式中複雜身分驗證機制的需求。此外， 模式會使用 Amazon Elastic Container Registry (Amazon ECR) 來存放容器映像。

此模式中的範例使用來自公有圖庫的 Docker 影像，最初建立範例工作負載。之後，新的 Docker 映像會建置為儲存在 Amazon ECR 中。對於來源，請考慮 Git 型系統，例如 GitHub、GitLab 或 Bitbucket，或使用 Amazon Simple Storage Service Amazon S3 (Amazon S3)。若要建置 Docker 映像，請考慮 AWS CodeBuild 針對後續映像使用 。

## 先決條件和限制
<a name="simplify-application-authentication-with-mutual-tls-in-amazon-ecs-prereqs"></a>

**先決條件**
+  AWS 帳戶 具有部署 AWS CloudFormation 堆疊存取權的作用中 。請確定您具有部署 CloudFormation 的 AWS Identity and Access Management (IAM) [使用者或角色許可](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/control-access-with-iam.html)。
+ AWS Command Line Interface (AWS CLI) [已安裝](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)。使用 或在 `~/.aws/credentials` 檔案中[設定](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html)環境變數，在本機電腦 AWS CLI 或環境中設定您的 AWS 登入資料。
+ [已安裝](https://www.openssl.org/) OpenSSL。
+ [已安裝](https://www.docker.com/get-started/) Docker。
+ 熟悉[工具](#simplify-application-authentication-with-mutual-tls-in-amazon-ecs-tools) AWS 服務 中所述的 。
+ Docker 和 NGINX 的知識。

**限制**
+ Application Load Balancer 的相互 TLS 僅支援 X.509v3 用戶端憑證。不支援 X.509v1 用戶端憑證。
+ 此模式程式碼儲存庫中提供的 CloudFormation 範本不包含佈建 CodeBuild 專案做為堆疊的一部分。
+ 有些 AWS 服務 完全無法使用 AWS 區域。如需區域可用性，請參閱[AWS 依區域的服務](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services/)。如需特定端點，請參閱[服務端點和配額](https://docs.aws.amazon.com/general/latest/gr/aws-service-information.html)，然後選擇服務的連結。

**產品版本**
+ Docker 27.3.1 版或更新版本
+ AWS CLI 2.14.5 版或更新版本

## Architecture
<a name="simplify-application-authentication-with-mutual-tls-in-amazon-ecs-architecture"></a>

下圖顯示此模式的架構元件。

![\[使用 Application Load Balancer 透過交互 TLS 進行驗證的工作流程。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/a343fa4e-097f-416b-9c83-01a28eb57dc3/images/e1371297-b987-4487-9b13-8120933c921f.png)


 該圖顯示以下工作流程：

1. 建立 Git 儲存庫，並將應用程式程式碼遞交至儲存庫。

1. 在 中建立私有憑證授權機構 (CA) AWS 私有 CA。

1. 建立 CodeBuild 專案。CodeBuildproject 由遞交變更觸發，並建立 Docker 映像並將建置映像發佈至 Amazon ECR。

1. 從 CA 複製憑證鏈和憑證內文，並將憑證套件上傳至 Amazon S3。

1. 使用您上傳到 Amazon S3 的 CA 套件建立信任存放區。將信任存放區與 Application Load Balancer (ALB) 上的交互 TLS 接聽程式建立關聯。

1. 使用私有 CA 為容器工作負載發行用戶端憑證。也使用 建立私有 TLS 憑證 AWS 私有 CA。

1. 將私有 TLS 憑證匯入 AWS Certificate Manager (ACM)，並搭配 ALB 使用。

1. 當 與 中的容器工作負載通訊時， 中的容器工作負載`ServiceTwo`會使用發行的用戶端憑證來驗證 ALB`ServiceOne`。

1. 中的容器工作負載會在與 中的容器工作負載通訊時，`ServiceOne`使用發行的用戶端憑證來驗證 ALB`ServiceTwo`。

**自動化和擴展**

透過使用 CloudFormation 或 SDK 的 API 操作來佈建 AWS 資源 AWS Cloud Development Kit (AWS CDK) ，即可完全自動化此模式。

您可以使用 AWS CodePipeline 實作持續整合和持續部署 (CI/CD) 管道，使用 CodeBuild 自動化容器映像建置程序，並將新版本部署至 Amazon ECS 叢集服務。

## 工具
<a name="simplify-application-authentication-with-mutual-tls-in-amazon-ecs-tools"></a>

**AWS 服務 **
+ [AWS Certificate Manager (ACM)](https://docs.aws.amazon.com/acm/latest/userguide/acm-overview.html) 可協助您建立、存放和續約公有和私有 SSL/TLS X.509 憑證和金鑰，以保護 AWS 網站和應用程式。
+ [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html) 可協助您設定 AWS 資源、快速一致地佈建資源，以及在整個 AWS 帳戶 和 生命週期中管理資源 AWS 區域。
+ [AWS CodeBuild](https://docs.aws.amazon.com/codebuild/latest/userguide/welcome.html) 是一種全受管建置服務，可協助您編譯原始程式碼、執行單元測試，並產生準備好部署的成品。
+ [Amazon Elastic Container Registry (Amazon ECR)](https://docs.aws.amazon.com/AmazonECR/latest/userguide/what-is-ecr.html) 是一種受管容器映像登錄服務，安全、可擴展且可靠。
+ [Amazon Elastic Container Service (Amazon ECS)](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/Welcome.html) 是一種高度可擴展的快速容器管理服務，可用於執行、停止和管理叢集上的容器。您可以在 管理的無伺服器基礎設施上執行任務和服務 AWS Fargate。或者，若要進一步控制您的基礎設施，您可以在您管理的 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體叢集上執行任務和服務。
+ [Amazon ECS Exec](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-exec.html) 可讓您直接與容器互動，而不需要先與主機容器作業系統互動、開啟傳入連接埠或管理 SSH 金鑰。您可以使用 ECS Exec 在 Amazon EC2 執行個體上執行的容器上執行命令，或取得 Shell 至容器 AWS Fargate。
+ [Elastic Load Balancing (ELB)](https://docs.aws.amazon.com/elasticloadbalancing/latest/userguide/what-is-load-balancing.html) 會將傳入的應用程式或網路流量分配到多個目標。例如，您可以在一或多個可用區域中將流量分配到 Amazon EC2 執行個體、容器和 IP 地址。ELB 會監控其已註冊目標的運作狀態，並僅將流量路由至運作狀態良好的目標。ELB 會在傳入流量隨時間變更時擴展負載平衡器。它可以自動擴展到大多數工作負載。
+ [AWS Fargate](https://docs.aws.amazon.com/AmazonECS/latest/userguide/what-is-fargate.html) 可協助您執行容器，而無需管理伺服器或 Amazon EC2 執行個體。Fargate 與 Amazon ECS 和 Amazon Elastic Kubernetes Service (Amazon EKS) 相容。您可以使用 Fargate 啟動類型或 Fargate 容量提供者來執行 Amazon ECS 任務和服務。若要這樣做，請將您的應用程式封裝在容器中、指定 CPU 和記憶體需求、定義聯網和 IAM 政策，以及啟動應用程式。每個 Fargate 任務都有自己的隔離界限，不會與其他任務共用基礎核心、CPU 資源、記憶體資源或彈性網路界面。
+ [AWS 私有憑證授權單位](https://docs.aws.amazon.com/privateca/latest/userguide/PcaWelcome.html) 可讓您建立私有憑證授權機構 (CA) 階層 (包括根 CA 和次級 CA)，而不需要操作內部部署 CA 的投資和維護成本。

**其他工具**** **
+ [Docker](https://www.docker.com/) 是一組平台即服務 (PaaS) 產品，可在作業系統層級使用虛擬化在容器中交付軟體。
+ [GitHub](https://docs.github.com/en/repositories/creating-and-managing-repositories/quickstart-for-repositories)、[GitLab](https://docs.gitlab.com/ee/user/get_started/get_started_projects.html) 和 [Bitbucket](https://support.atlassian.com/bitbucket-cloud/docs/tutorial-learn-bitbucket-with-git/) 是一些常用的 Git 型來源控制系統，用於追蹤來源碼變更。
+ [NGINX Open Source](https://nginx.org/en/docs/?_ga=2.187509224.1322712425.1699399865-405102969.1699399865) 是開放原始碼負載平衡器、內容快取和 Web 伺服器。此模式會將其用作 Web 伺服器。
+ [OpenSSL](https://www.openssl.org/) 是一種開放原始碼程式庫，可提供 TLS 和 CMS 的 OpenSSL 實作所使用的服務。

**程式碼儲存庫**

此模式的程式碼可在 GitHub [mTLS-with-Application-Load-Balancer-in-Amazon-ECS](https://github.com/aws-samples/mTLS-with-Application-Load-Balancer-in-Amazon-ECS) 儲存庫中使用。

## 最佳實務
<a name="simplify-application-authentication-with-mutual-tls-in-amazon-ecs-best-practices"></a>
+ 使用 Amazon ECS Exec 執行命令或取得在 Fargate 上執行的容器 shell。您也可以使用 ECS Exec 來協助收集診斷資訊以進行偵錯。
+ 使用安全群組和網路存取控制清單 ACLs) 來控制服務之間的傳入和傳出流量。Fargate 任務會從虛擬私有雲端 (VPC) 中設定的子網路接收 IP 地址。

## 史詩
<a name="simplify-application-authentication-with-mutual-tls-in-amazon-ecs-epics"></a>

### 建立儲存庫
<a name="create-the-repository"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 下載原始程式碼。 | 若要下載此模式的原始碼，請分叉或複製 GitHub [mTLS-with-Application-Load-Balancer-in-Amazon-ECS](https://github.com/aws-samples/mTLS-with-Application-Load-Balancer-in-Amazon-ECS) 儲存庫。 | DevOps 工程師 | 
| 建立 Git 儲存庫。 | 若要建立 Git 儲存庫以包含 Dockerfile 和 `buildspec.yaml` 檔案，請使用下列步驟：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/simplify-application-authentication-with-mutual-tls-in-amazon-ecs.html)`git clone https://github.com/aws-samples/mTLS-with-Application-Load-Balancer-in-Amazon-ECS.git` | DevOps 工程師 | 

### 建立 CA 並產生憑證
<a name="create-ca-and-generate-certificates"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 在 中建立私有 CA AWS 私有 CA。 | 若要建立私有憑證授權機構 (CA)，請在終端機中執行下列命令。將範例變數中的值取代為您自己的值。<pre>export AWS_DEFAULT_REGION="us-west-2"<br />export SERVICES_DOMAIN="www.example.com"<br /><br />export ROOT_CA_ARN=`aws acm-pca create-certificate-authority \<br />    --certificate-authority-type ROOT \<br />    --certificate-authority-configuration \<br />    "KeyAlgorithm=RSA_2048,<br />    SigningAlgorithm=SHA256WITHRSA,<br />    Subject={<br />        Country=US,<br />        State=WA,<br />        Locality=Seattle,<br />        Organization=Build on AWS,<br />        OrganizationalUnit=mTLS Amazon ECS and ALB Example,<br />        CommonName=${SERVICES_DOMAIN}}" \<br />        --query CertificateAuthorityArn --output text`</pre>如需詳細資訊，請參閱 AWS 文件中的在 [中建立私有 CA AWS 私有 CA](https://docs.aws.amazon.com/privateca/latest/userguide/create-CA.html)。 | DevOps 工程師，AWS DevOps | 
| 建立並安裝私有 CA 憑證。 | 若要為您的私有根 CA 建立並安裝憑證，請在終端機中執行下列命令：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/simplify-application-authentication-with-mutual-tls-in-amazon-ecs.html) | AWS DevOps，DevOps 工程師 | 
| 請求受管憑證。 | 若要在 中請求私有憑證 AWS Certificate Manager 以搭配私有 ALB 使用，請使用下列命令：<pre>export TLS_CERTIFICATE_ARN=`aws acm request-certificate \<br />    --domain-name "*.${DOMAIN_DOMAIN}" \<br />    --certificate-authority-arn ${ROOT_CA_ARN} \<br />    --query CertificateArn --output text`</pre> | DevOps 工程師，AWS DevOps | 
| 使用私有 CA 發行用戶端憑證。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/simplify-application-authentication-with-mutual-tls-in-amazon-ecs.html)`openssl req -out client_csr1.pem -new -newkey rsa:2048 -nodes -keyout client_private-key1.pem``openssl req -out client_csr2.pem -new -newkey rsa:2048 -nodes -keyout client_private-key2.pem`此命令會傳回兩個服務的 CSR 和私有金鑰。[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/simplify-application-authentication-with-mutual-tls-in-amazon-ecs.html)<pre>SERVICE_ONE_CERT_ARN=`aws acm-pca issue-certificate \<br />    --certificate-authority-arn ${ROOT_CA_ARN} \<br />    --csr fileb://client_csr1.pem \<br />    --signing-algorithm "SHA256WITHRSA" \<br />    --validity Value=5,Type="YEARS" --query CertificateArn --output text` <br /><br />echo "SERVICE_ONE_CERT_ARN: ${SERVICE_ONE_CERT_ARN}"<br /><br />aws acm-pca get-certificate \<br />    --certificate-authority-arn ${ROOT_CA_ARN} \<br />    --certificate-arn ${SERVICE_ONE_CERT_ARN} \<br />     | jq -r '.Certificate' > client_cert1.cert<br /><br />SERVICE_TWO_CERT_ARN=`aws acm-pca issue-certificate \<br />    --certificate-authority-arn ${ROOT_CA_ARN} \<br />    --csr fileb://client_csr2.pem \<br />    --signing-algorithm "SHA256WITHRSA" \<br />    --validity Value=5,Type="YEARS" --query CertificateArn --output text` <br /><br />echo "SERVICE_TWO_CERT_ARN: ${SERVICE_TWO_CERT_ARN}"<br /><br />aws acm-pca get-certificate \<br />    --certificate-authority-arn ${ROOT_CA_ARN} \<br />    --certificate-arn ${SERVICE_TWO_CERT_ARN} \<br />     | jq -r '.Certificate' > client_cert2.cert</pre>如需詳細資訊，請參閱 AWS 文件中的[發行私有終端實體憑證](https://docs.aws.amazon.com/privateca/latest/userguide/PcaIssueCert.html)。 | DevOps 工程師，AWS DevOps | 

### 佈建 AWS 服務
<a name="provision-aws-services"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
|  AWS 服務 使用 CloudFormation 範本佈建 。 | 若要佈建虛擬私有雲端 (VPC)、Amazon ECS 叢集、Amazon ECS 服務、Application Load Balancer 和 Amazon Elastic Container Registry (Amazon ECR)，請使用 CloudFormation 範本。 | DevOps 工程師 | 
| 取得變數。 | 確認您有執行兩個服務的 Amazon ECS 叢集。若要擷取資源詳細資訊並將其儲存為變數，請使用下列命令：<pre><br />export LoadBalancerDNS=$(aws cloudformation describe-stacks --stack-name ecs-mtls \<br />--output text \<br />--query 'Stacks[0].Outputs[?OutputKey==`LoadBalancerDNS`].OutputValue')<br /><br />export ECRRepositoryUri=$(aws cloudformation describe-stacks --stack-name ecs-mtls \<br />--output text \<br />--query 'Stacks[0].Outputs[?OutputKey==`ECRRepositoryUri`].OutputValue')<br /><br />export ECRRepositoryServiceOneUri=$(aws cloudformation describe-stacks --stack-name ecs-mtls \<br />--output text \<br />--query 'Stacks[0].Outputs[?OutputKey==`ECRRepositoryServiceOneUri`].OutputValue')<br /><br />export ECRRepositoryServiceTwoUri=$(aws cloudformation describe-stacks --stack-name ecs-mtls \<br />--output text \<br />--query 'Stacks[0].Outputs[?OutputKey==`ECRRepositoryServiceTwoUri`].OutputValue')<br /><br />export ClusterName=$(aws cloudformation describe-stacks --stack-name ecs-mtls \<br />--output text \<br />--query 'Stacks[0].Outputs[?OutputKey==`ClusterName`].OutputValue')<br /><br />export BucketName=$(aws cloudformation describe-stacks --stack-name ecs-mtls \<br />--output text \<br />--query 'Stacks[0].Outputs[?OutputKey==`BucketName`].OutputValue')<br /><br />export Service1ListenerArn=$(aws cloudformation describe-stacks --stack-name ecs-mtls \<br />--output text \<br />--query 'Stacks[0].Outputs[?OutputKey==`Service1ListenerArn`].OutputValue')<br /><br />export Service2ListenerArn=$(aws cloudformation describe-stacks --stack-name ecs-mtls \<br />--output text \<br />--query 'Stacks[0].Outputs[?OutputKey==`Service2ListenerArn`].OutputValue')</pre> | DevOps 工程師 | 
| 建立 CodeBuild 專案。 | 若要使用 CodeBuild 專案為您的 Amazon ECS 服務建立 Docker 映像，請執行下列動作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/simplify-application-authentication-with-mutual-tls-in-amazon-ecs.html)如需詳細資訊，請參閱 AWS 文件中的在 [中建立建置專案 AWS CodeBuild](https://docs.aws.amazon.com/codebuild/latest/userguide/create-project.html)。 | AWS DevOps，DevOps 工程師 | 
| 建置 Docker 映像。 | 您可以使用 CodeBuild 來執行映像建置程序。CodeBuild 需要與 Amazon ECR 互動和使用 Amazon S3 的許可。在此程序中，會建置 Docker 映像並推送至 Amazon ECR 登錄檔。如需範本和程式碼的詳細資訊，請參閱[其他資訊](#simplify-application-authentication-with-mutual-tls-in-amazon-ecs-additional)。（選用） 若要在本機建置以供測試之用，請使用下列命令：<pre># login to ECR<br />aws ecr get-login-password | docker login --username AWS --password-stdin $ECRRepositoryUri<br /><br /># build image for service one<br />cd /service1<br />aws s3 cp s3://$BucketName/serviceone/ service1/ --recursive<br />docker build -t $ECRRepositoryServiceOneUri .<br />docker push $ECRRepositoryServiceOneUri<br /><br /># build image for service two<br />cd ../service2<br />aws s3 cp s3://$BucketName/servicetwo/ service2/ --recursive<br />docker build -t $ECRRepositoryServiceTwoUri .<br />docker push $ECRRepositoryServiceTwoUri</pre> | DevOps 工程師 | 

### 啟用交互 TLS
<a name="enable-mutual-tls"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 將 CA 憑證上傳至 Amazon S3。 | 若要將 CA 憑證上傳至 Amazon S3 儲存貯體，請使用下列範例命令：`aws s3 cp ca-cert.pem s3://$BucketName/acm-trust-store/ ` | AWS DevOps，DevOps 工程師 | 
| 建立信任存放區。 | 若要建立信任存放區，請使用下列範例命令：<pre>TrustStoreArn=`aws elbv2 create-trust-store --name acm-pca-trust-certs \<br />    --ca-certificates-bundle-s3-bucket $BucketName \<br />    --ca-certificates-bundle-s3-key acm-trust-store/ca-cert.pem --query 'TrustStores[].TrustStoreArn' --output text`</pre> | AWS DevOps，DevOps 工程師 | 
| 上傳用戶端憑證。 | 若要將用戶端憑證上傳至 Amazon S3 for Docker 映像，請使用下列範例命令：<pre># for service one<br />aws s3 cp client_cert1.cert s3://$BucketName/serviceone/<br />aws s3 cp client_private-key1.pem s3://$BucketName/serviceone/<br /><br /># for service two<br />aws s3 cp client_cert2.cert s3://$BucketName/servicetwo/<br />aws s3 cp client_private-key2.pem s3://$BucketName/servicetwo/</pre> | AWS DevOps，DevOps 工程師 | 
| 修改接聽程式。 | 若要在 ALB 上啟用交互 TLS，請使用下列命令修改 HTTPS 接聽程式：<pre>aws elbv2 modify-listener \<br />    --listener-arn $Service1ListenerArn \<br />    --certificates CertificateArn=$TLS_CERTIFICATE_ARN_TWO \<br />    --ssl-policy ELBSecurityPolicy-2016-08 \<br />    --protocol HTTPS \<br />    --port 8080 \<br />    --mutual-authentication Mode=verify,TrustStoreArn=$TrustStoreArn,IgnoreClientCertificateExpiry=false<br /><br />aws elbv2 modify-listener \<br />    --listener-arn $Service2ListenerArn \<br />    --certificates CertificateArn=$TLS_CERTIFICATE_ARN_TWO \<br />    --ssl-policy ELBSecurityPolicy-2016-08 \<br />    --protocol HTTPS \<br />    --port 8090 \<br />    --mutual-authentication Mode=verify,TrustStoreArn=$TrustStoreArn,IgnoreClientCertificateExpiry=false<br /></pre>如需詳細資訊，請參閱 AWS 文件中的[在 Application Load Balancer 上設定交互 TLS](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/configuring-mtls-with-elb.html)。 | AWS DevOps，DevOps 工程師 | 

### 更新服務
<a name="update-the-services"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 更新 Amazon ECS 任務定義。 | 若要更新 Amazon ECS 任務定義，請修改新修訂中的 `image` 參數。若要取得個別服務的值，請使用您在先前步驟中建置的新 Docker 映像 Uri 更新任務定義： `echo $ECRRepositoryServiceOneUri` 或 `echo $ECRRepositoryServiceTwoUri`<pre><br />    "containerDefinitions": [<br />        {<br />            "name": "nginx",<br />            "image": "public.ecr.aws/nginx/nginx:latest",   # <----- change to new Uri<br />            "cpu": 0,</pre>如需詳細資訊，請參閱 AWS 文件中的使用 主控台[更新 Amazon ECS 任務定義](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/update-task-definition-console-v2.html)。 | AWS DevOps，DevOps 工程師 | 
| 更新 Amazon ECS 服務。 | 使用最新的任務定義更新服務。此任務定義是新建置 Docker 映像的藍圖，其中包含交互 TLS 身分驗證所需的用戶端憑證。 若要更新服務，請使用下列程序：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/simplify-application-authentication-with-mutual-tls-in-amazon-ecs.html)對其他服務重複這些步驟。 | AWS 管理員、AWS DevOps、DevOps 工程師 | 

### 存取應用程式
<a name="access-the-application"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 複製應用程式 URL。 | 使用 Amazon ECS 主控台檢視任務。當任務狀態更新為**執行**中時，選取任務。在**任務**區段中，複製任務 ID。 | AWS 管理員、AWS DevOps | 
| 測試您的應用程式。 | 若要測試您的應用程式，請使用 ECS Exec 存取任務。[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/simplify-application-authentication-with-mutual-tls-in-amazon-ecs.html) | AWS 管理員、AWS DevOps | 

## 相關資源
<a name="simplify-application-authentication-with-mutual-tls-in-amazon-ecs-resources"></a>

**Amazon ECS 文件**
+ [使用主控台建立 Amazon ECS 任務定義](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/create-task-definition.html)
+ [建立容器映像以用於 Amazon ECS](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/create-container-image.html)
+ [Amazon ECS 叢集](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/clusters.html)
+ [適用於 的 Amazon ECS AWS Fargate](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/create-container-image.html#create-container-image-next-steps)
+ [Amazon ECS 聯網最佳實務](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/networking-best-practices.html)
+ [Amazon ECS 服務定義參數](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/service_definition_parameters.html)

**其他 AWS 資源**
+ [如何使用 AWS 私有 CA 在 Application Load Balancer 上設定 mTLS？](https://repost.aws/knowledge-center/elb-alb-configure-private-ca-mtls) (AWS re:Post)

## 其他資訊
<a name="simplify-application-authentication-with-mutual-tls-in-amazon-ecs-additional"></a>

**編輯 Dockerfile**** **

下列程式碼顯示您在服務 1 的 Dockerfile 中編輯的命令：

```
FROM public.ecr.aws/nginx/nginx:latest
WORKDIR /usr/share/nginx/html
RUN echo "Returning response from Service 1: Ok" > /usr/share/nginx/html/index.html
ADD client_cert1.cert client_private-key1.pem /usr/local/share/ca-certificates/
RUN chmod -R 400 /usr/local/share/ca-certificates/
```

下列程式碼顯示您在服務 2 的 Dockerfile 中編輯的命令：

```
FROM public.ecr.aws/nginx/nginx:latest
WORKDIR /usr/share/nginx/html
RUN echo "Returning response from Service 2: Ok" > /usr/share/nginx/html/index.html
ADD client_cert2.cert client_private-key2.pem /usr/local/share/ca-certificates/
RUN chmod -R 400 /usr/local/share/ca-certificates/
```

如果您使用 CodeBuild 建置 Docker 映像， `buildspec` 檔案會使用 CodeBuild 組建編號來唯一地將映像版本識別為標籤值。您可以變更 `buildspec` 檔案以符合您的需求，如下列`buildspec `自訂程式碼所示：

```
version: 0.2

phases:
  pre_build:
    commands:
      - echo Logging in to Amazon ECR...
      - aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $ECR_REPOSITORY_URI
      - COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
      - IMAGE_TAG=${COMMIT_HASH:=latest}
  build:
    commands:
        # change the S3 path depending on the service
      - aws s3 cp s3://$YOUR_S3_BUCKET_NAME/serviceone/ $CodeBuild_SRC_DIR/ --recursive 
      - echo Build started on `date`
      - echo Building the Docker image...
      - docker build -t $ECR_REPOSITORY_URI:latest .
      - docker tag $ECR_REPOSITORY_URI:latest $ECR_REPOSITORY_URI:$IMAGE_TAG
  post_build:
    commands:
      - echo Build completed on `date`
      - echo Pushing the Docker images...
      - docker push $ECR_REPOSITORY_URI:latest
      - docker push $ECR_REPOSITORY_URI:$IMAGE_TAG
      - echo Writing image definitions file...
      # for ECS deployment reference
      - printf '[{"name":"%s","imageUri":"%s"}]' $CONTAINER_NAME $ECR_REPOSITORY_URI:$IMAGE_TAG > imagedefinitions.json   

artifacts:
  files:
    - imagedefinitions.json
```