

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用 AWS 代码服务和 AWS KMS 多区域密钥管理向多个账户和区域 blue/green 部署的微服务
<a name="manage-blue-green-deployments-of-microservices-to-multiple-accounts-and-regions-by-using-aws-code-services-and-aws-kms-multi-region-keys"></a>

*Balaji Vedagiri、Vanitha Dontireddy、Ashish Kumar、Faisal Shahdad、Vivek Thangamuthu 和 Anand Krishna Varanasi，Amazon Web Services*

## Summary
<a name="manage-blue-green-deployments-of-microservices-to-multiple-accounts-and-regions-by-using-aws-code-services-and-aws-kms-multi-region-keys-summary"></a>

此模式描述了如何根据部署策略将全球微服务应用程序从中央 AWS 账户 blue/green 部署到多个工作负载账户和区域。该模式支持以下内容：
+ 软件是在一个中心账户中开发的，而工作负载和应用程序则分布在多个账户和 AWS 区域中。
+ 单个 AWS 密钥管理系统（AWS KMS）多区域密钥用于加密和解密，以涵盖灾难恢复。
+ KMS 密钥是特定于区域的，必须在三个不同的区域中维护或创建管道构件。KMS 多区域密钥有助于跨区域保留相同的密钥 ID。
+ Git 工作流分支模型由两个分支（开发分支和主分支）实现，使用拉取请求 (PRs) 合并代码。从此堆栈部署的 AWS Lambda 函数创建了一个从开发分支到主分支的 PR。PR 合并到主分支会启动 AWS CodePipeline 管道，该管道协调持续集成和持续交付 (CI/CD) 流程，并将堆栈部署到各个账户。

此模式提供了通过 AWS CloudFormation 堆栈设置的基础设施即代码 (IaC) 示例，以演示此用例。微服务的 blue/green 部署是使用 AWS CodeDeploy 实现的。

## 先决条件和限制
<a name="manage-blue-green-deployments-of-microservices-to-multiple-accounts-and-regions-by-using-aws-code-services-and-aws-kms-multi-region-keys-prereqs"></a>

**先决条件**
+ 四个活跃的 Amazon Web Services account：
  + 用于管理代码管道和维护 AWS CodeCommit 存储库的工具账户。
  + 用于部署微服务工作负载的三个工作负载（测试）账户。
+ 此模式使用以下区域。如果您想使用其他区域，则必须对 AWS CodeDeploy 和 AWS KMS 多区域堆栈进行适当的修改。
  + 工具 (AWS CodeCommit) 账户：`ap-south-1`
  + 工作负载（测试）账户 1：`ap-south-1`
  + 工作负载(测试)账户 2： `eu-central-1`
  + 工作负载(测试)账户 3： `us-east-1`
+ 每个工作负载账户中用于部署区域的三个 Amazon Simple Storage Service (Amazon S3) 桶。（在此模式中，它们稍后被称为 `S3BUCKETNAMETESTACCOUNT1`、`S3BUCKETNAMETESTACCOUNT2 ` 和 `S3BUCKETNAMETESTACCOUNT3 `。）

  例如，您可以在特定账户和区域中使用唯一的桶名称创建这些桶，如下所示（将 *xxxx* 替换为随机数）：

  ```
  ##In Test Account 1
  aws s3 mb s3://ecs-codepipeline-xxxx-ap-south-1 --region ap-south-1
  ##In Test Account 2
  aws s3 mb s3://ecs-codepipeline-xxxx-eu-central-1 --region eu-central-1
  ##In Test Account 3
  aws s3 mb s3://ecs-codepipeline-xxxx-us-east-1 --region us-east-1
  
  #Example
  ##In Test Account 1
  aws s3 mb s3://ecs-codepipeline-18903-ap-south-1 --region ap-south-1
  ##In Test Account 2
  aws s3 mb s3://ecs-codepipeline-18903-eu-central-1 --region eu-central-1
  ##In Test Account 3
  aws s3 mb s3://ecs-codepipeline-18903-us-east-1 --region us-east-1
  ```

**限制**

该模式使用 AWS CodeBuild 和其他配置文件来部署示例微服务。如果您有不同的工作负载类型（例如无服务器），则必须更新所有相关配置。

## 架构
<a name="manage-blue-green-deployments-of-microservices-to-multiple-accounts-and-regions-by-using-aws-code-services-and-aws-kms-multi-region-keys-architecture"></a>

**目标技术堆栈**
+ AWS CloudFormation
+ AWS CodeCommit
+ AWS CodeBuild
+ AWS CodeDeploy
+ AWS CodePipeline

**目标架构**

![\[用于将微服务部署到多个账户和区域的目标架构\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/images/pattern-img/a144c977-6823-4b08-a215-fae779b3ce7c/images/eedfabdb-f266-4190-b271-5caf7ac9b47b.png)


**自动化和扩缩**

使用 AWS CloudFormation 堆栈模板 (IaC) 自动完成设置。它可以轻松扩展到多个环境和账户。

## 工具
<a name="manage-blue-green-deployments-of-microservices-to-multiple-accounts-and-regions-by-using-aws-code-services-and-aws-kms-multi-region-keys-tools"></a>

**AWS 服务**
+ [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html) 可帮助您设置 AWS 资源，快速一致地配置这些资源，并在 AWS 账户和区域的整个生命周期中对其进行管理。
+ [AWS CodeBuild](https://docs.aws.amazon.com/codebuild/latest/userguide/welcome.html) 是一项完全托管的构建服务，可帮助您编译源代码、运行单元测试和生成可随时部署的项目。
+ [AWS CodeCommit](https://docs.aws.amazon.com/codecommit/latest/userguide/welcome.html) 是一项版本控制服务，可帮助您私下存储和管理 Git 存储库，而无需管理自己的源代码控制系统。
+ [AWS CodeDeploy](https://docs.aws.amazon.com/codedeploy/latest/userguide/welcome.html) 自动部署到亚马逊弹性计算云 (Amazon EC2) 或本地实例、AWS Lambda 函数或亚马逊弹性容器服务 (Amazon ECS) 服务。
+ [AWS CodePipeline](https://docs.aws.amazon.com/codepipeline/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 Key Management Service (AWS KMS)](https://docs.aws.amazon.com/kms/latest/developerguide/overview.html) 可帮助您创建和控制加密密钥，以帮助保护您的数据。
+ [Amazon Simple Storage Service(Amazon S3)](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html) 是一项基于云的对象存储服务，可帮助您存储、保护和检索任意数量的数据。

**其他工具**
+ [Git](https://git-scm.com/docs) 是一个开源的分布式版本控制系统，可与 AWS CodeCommit 存储库配合使用。
+ [Docker](https://www.docker.com/) 是一组平台即服务（PaaS）产品，它们使用操作系统级别的虚拟化技术在容器中交付软件。该模式使用 Docker 在本地构建和测试容器映像。
+ [cfn-lint](https://github.com/aws-cloudformation/cfn-lint) 和 [cfn-nag](https://github.com/stelligent/cfn_nag) 是开源工具，可帮助您检查 CloudFormation 堆栈中是否存在任何错误和安全问题。

**代码存储库**

此模式的代码可在[多个地区的 GitHub 全球 Blue/Green 部署和账户存储库中](https://github.com/aws-samples/ecs-blue-green-global-deployment-with-multiregion-cmk-codepipeline)找到。

## 操作说明
<a name="manage-blue-green-deployments-of-microservices-to-multiple-accounts-and-regions-by-using-aws-code-services-and-aws-kms-multi-region-keys-epics"></a>

### 设置环境变量
<a name="set-up-environment-variables"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 导出用于 CloudFormation 堆栈部署的环境变量。 | 定义环境变量，这些变量将在此模式的后面用作 CloudFormation 堆栈的输入。[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/manage-blue-green-deployments-of-microservices-to-multiple-accounts-and-regions-by-using-aws-code-services-and-aws-kms-multi-region-keys.html) | AWS DevOps | 

### Package 并部署 CloudFormation 基础架构的堆栈
<a name="package-and-deploy-the-cloudformation-stacks-for-the-infrastructure"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 克隆存储库。 | 将[示例存储库](https://github.com/aws-samples/ecs-blue-green-global-deployment-with-multiregion-cmk-codepipeline)克隆到您工作位置的新存储库中：<pre>##In work location<br />git clone https://github.com/aws-samples/ecs-blue-green-global-deployment-with-multiregion-cmk-codepipeline.git</pre> | AWS DevOps | 
| 打包 Cloudformation 资源。 | 在此步骤中，您将打包 CloudFormation 模板引用的本地项目，以创建亚马逊虚拟私有云 (Amazon VPC) 和Application Load Balancer等服务所需的基础设施资源。这些模板位于代码存储库的 `Infra` 文件夹中。<pre>##In TestAccount1##<br />aws cloudformation package \<br />    --template-file mainInfraStack.yaml \<br />    --s3-bucket $S3BUCKETNAMETESTACCOUNT1 \<br />    --s3-prefix infraStack \<br />    --region $TESTACCOUNT1REGION \<br />    --output-template-file infrastructure_${TESTACCOUNT1}.template</pre><pre>##In TestAccount2##<br />aws cloudformation package \<br />    --template-file mainInfraStack.yaml \<br />    --s3-bucket $S3BUCKETNAMETESTACCOUNT2 \<br />    --s3-prefix infraStack \<br />    --region $TESTACCOUNT2REGION \<br />    --output-template-file infrastructure_${TESTACCOUNT2}.template</pre><pre>##In TestAccount3##<br />aws cloudformation package \<br />    --template-file mainInfraStack.yaml \<br />    --s3-bucket $S3BUCKETNAMETESTACCOUNT3 \<br />    --s3-prefix infraStack \<br />    --region $TESTACCOUNT3REGION \<br />    --output-template-file infrastructure_${TESTACCOUNT3}.template</pre> | AWS DevOps | 
| 验证程序包模板。 | 验证程序包模板：<pre>aws cloudformation validate-template \<br />    --template-body file://infrastructure_${TESTACCOUNT1}.template<br /><br />aws cloudformation validate-template \<br />    --template-body file://infrastructure_${TESTACCOUNT2}.template<br /><br />aws cloudformation validate-template \<br />    --template-body file://infrastructure_${TESTACCOUNT3}.template</pre> | AWS DevOps | 
| 将包文件部署到工作负载账户中， | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/manage-blue-green-deployments-of-microservices-to-multiple-accounts-and-regions-by-using-aws-code-services-and-aws-kms-multi-region-keys.html) | AWS DevOps | 

### 推送示例图像并扩展 Amazon ECS
<a name="push-a-sample-image-and-scale-amazon-ecs"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 将示例图像推送到 Amazon ECR 存储库。 | 将一个示例 (NGINX) 图像推送到名为 `web` 的 Amazon Elastic Container Registry (Amazon ECR) 存储库中（如参数中所设置）。您可以根据需要自定义图像。要登录并设置用于将图像推送到 Amazon ECR 的凭证，请按照 [Amazon ECR 文档](https://docs.aws.amazon.com/AmazonECR/latest/userguide/docker-push-ecr-image.html)中的说明进行操作。命令包括：<pre>  docker pull nginx<br />  docker images<br />  docker tag <imageid> aws_account_id.dkr.ecr.region.amazonaws.com/<web>:latest<br />  docker push <aws_account_id>.dkr.ecr.<region>.amazonaws.com/<web>:tag </pre> | AWS DevOps | 
| 扩展 Amazon ECS 并验证访问权限。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/manage-blue-green-deployments-of-microservices-to-multiple-accounts-and-regions-by-using-aws-code-services-and-aws-kms-multi-region-keys.html) | AWS DevOps | 

### 设置代码服务和资源
<a name="set-up-code-services-and-resources"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 在工具账户中创建 CodeCommit 存储库。 | 使用模板在工具帐户中创建 CodeCommit 存储库，该`codecommit.yaml`模板位于 GitHub 存储库的`code`文件夹中。您只能在计划开发代码的单个区域中创建此存储库。<pre>aws cloudformation deploy --stack-name codecommitrepoStack --parameter-overrides  CodeCommitReponame=$CODECOMMITREPONAME \<br />ToolsAccount=$TOOLSACCOUNT --template-file codecommit.yaml  --region $TOOLSACCOUNTREGION \<br />--capabilities CAPABILITY_NAMED_IAM</pre> | AWS DevOps | 
| 创建 S3 存储桶，用于管理由生成的项目 CodePipeline。 | 创建一个 S3 存储桶，用于管理 CodePipeline 通过使用`pre-reqs-bucket.yaml`模板生成的项目，该模板位于 GitHub 存储库的`code`文件夹中。堆栈必须部署在所有三个工作负载（测试）和工具账户和区域中。<pre>aws cloudformation deploy --stack-name pre-reqs-artifacts-bucket --parameter-overrides BucketStartName=$BUCKETSTARTNAME \<br />TestAccount1=$TESTACCOUNT1 TestAccount2=$TESTACCOUNT2 \<br />TestAccount3=$TESTACCOUNT3 CodeCommitAccount=$CODECOMMITACCOUNT ToolsAccount=$TOOLSACCOUNT \<br />--template-file pre-reqs_bucket.yaml --region $TESTACCOUNT1REGION --capabilities CAPABILITY_NAMED_IAM<br /><br />aws cloudformation deploy --stack-name pre-reqs-artifacts-bucket --parameter-overrides BucketStartName=$BUCKETSTARTNAME \<br />TestAccount1=$TESTACCOUNT1 TestAccount2=$TESTACCOUNT2 \<br />TestAccount3=$TESTACCOUNT3 CodeCommitAccount=$CODECOMMITACCOUNT ToolsAccount=$TOOLSACCOUNT \<br />--template-file pre-reqs_bucket.yaml --region $TESTACCOUNT2REGION --capabilities CAPABILITY_NAMED_IAM<br /><br />aws cloudformation deploy --stack-name pre-reqs-artifacts-bucket --parameter-overrides BucketStartName=$BUCKETSTARTNAME \<br />TestAccount1=$TESTACCOUNT1 TestAccount2=$TESTACCOUNT2 \<br />TestAccount3=$TESTACCOUNT3 CodeCommitAccount=$CODECOMMITACCOUNT ToolsAccount=$TOOLSACCOUNT \<br />--template-file pre-reqs_bucket.yaml --region $TESTACCOUNT3REGION --capabilities CAPABILITY_NAMED_IAM<br /><br />aws cloudformation deploy --stack-name pre-reqs-artifacts-bucket --parameter-overrides BucketStartName=$BUCKETSTARTNAME \<br />TestAccount1=$TESTACCOUNT1 TestAccount2=$TESTACCOUNT2 \<br />TestAccount3=$TESTACCOUNT3 CodeCommitAccount=$CODECOMMITACCOUNT ToolsAccount=$TOOLSACCOUNT \<br />--template-file pre-reqs_bucket.yaml --region $TOOLSACCOUNTREGION --capabilities CAPABILITY_NAMED_IAM</pre> | AWS DevOps | 
| 设置多区域 KMS 密钥。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/manage-blue-green-deployments-of-microservices-to-multiple-accounts-and-regions-by-using-aws-code-services-and-aws-kms-multi-region-keys.html) | AWS DevOps | 
| 在工具账户中设置 CodeBuild 项目。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/manage-blue-green-deployments-of-microservices-to-multiple-accounts-and-regions-by-using-aws-code-services-and-aws-kms-multi-region-keys.html) | AWS DevOps | 
|  CodeDeploy 在工作负载帐户中进行设置。 | 使用 GitHub 存储库`code`文件夹中的`codedeploy.yaml`模板在所有三个工作负载帐户 CodeDeploy 中进行设置。的输出`mainInfraStack`包括 Amazon ECS 集群的亚马逊资源名称 (ARNs) 和 Application Load Balancer 侦听器。基础架构堆栈中的值已经导出，因此它们由 CodeDeploy 堆栈模板导入。<pre>##WorkloadAccount1##<br />aws cloudformation deploy --stack-name ecscodedeploystack \<br />--parameter-overrides  ToolsAccount=$TOOLSACCOUNT mainInfrastackname=mainInfrastack \<br />--template-file codedeploy.yaml  --region $TESTACCOUNT1REGION --capabilities CAPABILITY_NAMED_IAM<br /><br />##WorkloadAccount2##<br />aws cloudformation deploy --stack-name ecscodedeploystack \<br />--parameter-overrides ToolsAccount=$TOOLSACCOUNT mainInfrastackname=mainInfrastack \<br />--template-file codedeploy.yaml  --region $TESTACCOUNT2REGION --capabilities CAPABILITY_NAMED_IAM<br /><br />##WorkloadAccount3##<br />aws cloudformation deploy --stack-name ecscodedeploystack \<br />--parameter-overrides ToolsAccount=$TOOLSACCOUNT mainInfrastackname=mainInfrastack \<br />--template-file codedeploy.yaml  --region $TESTACCOUNT3REGION --capabilities CAPABILITY_NAMED_IAM</pre> | AWS DevOps | 

### CodePipeline 在工具账户中设置
<a name="set-up-codepipeline-in-the-tools-account"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 在工具账户中创建代码管道。 | 在工具账户中，运行以下命令：<pre>aws cloudformation deploy --stack-name ecscodepipelinestack --parameter-overrides  \<br />TestAccount1=$TESTACCOUNT1 TestAccount1Region=$TESTACCOUNT1REGION \<br />TestAccount2=$TESTACCOUNT2 TestAccount2Region=$TESTACCOUNT2REGION \<br />TestAccount3=$TESTACCOUNT3 TestAccount3Region=$TESTACCOUNT3REGION \<br />CMKARNTools=$CMKTROOLSARN CMKARN1=$CMKARN1 CMKARN2=$CMKARN2 CMKARN3=$CMKARN3 \<br />CodeCommitRepoName=$CODECOMMITREPONAME BucketStartName=$BUCKETSTARTNAME \<br />--template-file codepipeline.yaml --capabilities CAPABILITY_NAMED_IAM</pre> | AWS DevOps | 
| 在 AWS KMS 密钥策略 CodePipeline 和 S3 存储桶策略中提供访问权限和 CodeBuild 角色。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/manage-blue-green-deployments-of-microservices-to-multiple-accounts-and-regions-by-using-aws-code-services-and-aws-kms-multi-region-keys.html) | AWS DevOps | 

### 调用并测试管道
<a name="call-and-test-the-pipeline"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 将更改推送到 CodeCommit 存储库。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/manage-blue-green-deployments-of-microservices-to-multiple-accounts-and-regions-by-using-aws-code-services-and-aws-kms-multi-region-keys.html) |  | 

### 清理
<a name="clean-up"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 清理所有已部署的资源。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/manage-blue-green-deployments-of-microservices-to-multiple-accounts-and-regions-by-using-aws-code-services-and-aws-kms-multi-region-keys.html) |  | 

## 问题排查
<a name="manage-blue-green-deployments-of-microservices-to-multiple-accounts-and-regions-by-using-aws-code-services-and-aws-kms-multi-region-keys-troubleshooting"></a>


| 问题 | 解决方案 | 
| --- | --- | 
| 您提交到存储库的更改不会得到部署。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/manage-blue-green-deployments-of-microservices-to-multiple-accounts-and-regions-by-using-aws-code-services-and-aws-kms-multi-region-keys.html) | 

## 相关资源
<a name="manage-blue-green-deployments-of-microservices-to-multiple-accounts-and-regions-by-using-aws-code-services-and-aws-kms-multi-region-keys-resources"></a>
+ [推送 Docker 映像](https://docs.aws.amazon.com/AmazonECR/latest/userguide/docker-push-ecr-image.html)（Amazon ECR 文档)
+ [连接到 AWS CodeCommit 存储库](https://docs.aws.amazon.com/codecommit/latest/userguide/how-to-connect.html)（AWS CodeCommit 文档）
+ [AWS 疑难解答 CodeBuild](https://docs.aws.amazon.com/codebuild/latest/userguide/troubleshooting.html)（AWS CodeBuild 文档）