

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

# 使用 GitHub 操作基于 AWS CloudFormation 模板配置 AWS Service Catalog 产品
<a name="provision-aws-service-catalog-products-using-github-actions"></a>

*Ashish Bhatt 和 Ruchika Modi，Amazon Web Services*

## Summary
<a name="provision-aws-service-catalog-products-using-github-actions-summary"></a>

这种模式为组织提供了一种简化的方法，使用[AWS Service Catalog](https://docs.aws.amazon.com/servicecatalog/latest/adminguide/introduction.html)产品和产品组合来 AWS 服务 跨团队进行标准化和合规的配置。 [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html)有助于整合 Service Catalog 产品和产品组合中的基本组件，用于配置基础网络基础架构 AWS 云。这种模式还通过使用 Actions 将基础设施即代码 (IaC) 集成到自动化开发工作流程中，从而促进 DevOps 实践[GitHub 。](https://github.com/features/actions)

AWS Service Catalog 使组织能够在上创建和管理经批准的 IT 服务 AWS，从而提供标准化、集中控制、自助服务配置和成本管理等好处。通过操作自动部署 Service Catalog 产品组合和产品，公司可以执行以下 GitHub 操作：
+ 确保部署一致且可重复。
+ 对 IaC 使用版本控制。
+ 整合云资源管理与现有开发工作流。

这种组合可简化云端运营、增强合规性、加快经批准服务的交付，同时减少人为错误并提升整体效率。

## 先决条件和限制
<a name="provision-aws-service-catalog-products-using-github-actions-prereqs"></a>

**先决条件**
+ 活跃的 AWS 账户 
+ 访问[GitHub 存储库](https://docs.github.com/en/get-started/quickstart/create-a-repo)
+ 对 AWS CloudFormation 和的基本理解 AWS Service Catalog
+ 用于托管模板的亚马逊简单存储服务 (Amazon S3) Service 存储桶 CloudFormation 
+ 一个名为的 AWS Identity and Access Management (IAM) 角色`github-actions`，用于 GitHub 和之间的连接 AWS

**限制**
+ 此模式的可重用代码仅通过 Actions 进行了 GitHub 测试。
+ 有些 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)，然后选择相应服务的链接。

**产品版本**

此模式的解决方案是使用以下 [GitHub Marketplace](https://github.com/marketplace) 操作及其相应的版本创建的：
+ `actions/checkout@v4`
+ `aws-actions/configure-aws-credentials@v2`
+ `aws-actions/aws-cloudformation-github-deploy@v1.2.0`

## 架构
<a name="provision-aws-service-catalog-products-using-github-actions-architecture"></a>

下图显示了该解决方案的架构。

![\[使用 GitHub 操作根据 CloudFormation 模板配置 Service Catalog 产品。\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/images/pattern-img/49f82fa7-0c74-4581-bf92-95505dca264c/images/a13c7b41-534e-4a9e-bdca-2974fa40a49a.png)


1. 管理员或平台工程师将标准化 CloudFormation 模板推送到 GitHub 存储库，在那里维护这些模板。该 GitHub 存储库还包含 AWS Service Catalog 使用 GitHub 操作自动配置的工作流程。

1. GitHub 操作会触发一个工作流，该工作流 AWS 云 使用 OpenID Connect (OIDC) 提供程序连接到以配置 Service Catalog。

1. Service Catalog 包含开发人员可以直接用来配置标准化 AWS 资源的产品组合和产品。这种模式捆绑了虚拟私有云 (VPCs)、子网、NAT 和 Internet 网关以及路由表等 AWS 资源。

1. 开发者创建服务目录产品后，Service Catalog 会将其转换为预先配置的标准化 AWS 资源。因此，开发人员可以节省时间，因为他们无需手动逐个预调配和配置资源。

## 工具
<a name="provision-aws-service-catalog-products-using-github-actions-tools"></a>

**AWS 服务**
+ [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html)帮助您设置 AWS 资源，快速一致地配置资源，并在和的整个 AWS 账户 生命周期中对其进行管理 AWS 区域。它是一种基础设施即代码 (IaC) 服务，可以很容易地用作产品类型之一。 AWS Service Catalog
+ [AWS Identity and Access Management (IAM)](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html) 通过控制谁经过身份验证并有权使用 AWS 资源，从而帮助您安全地管理对资源的访问权限。
+ [AWS Service Catalog](https://docs.aws.amazon.com/servicecatalog/latest/adminguide/getstarted.html)帮助您集中管理已获批准的 IT 服务目录 AWS。最终用户可在遵循组织设定约束的情况下快速部署他们所需已获得批准的 IT 服务。
+ [Amazon Simple Storage Service（Amazon S3）](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html)是一项基于云的对象存储服务，可帮助您存储、保护和检索任意数量的数据。

**其他**
+ GitHub A@@ [c](https://docs.github.com/en/actions) tions 是一个持续集成和持续交付 (CI/CD) 平台，与 GitHub 存储库紧密集成。您可以使用 GitHub Actions 来自动执行构建、测试和部署管道。

**代码存储库**

此模式的代码可在 GitHub [service-catalog-with-github-](https://github.com/aws-samples/service-catalog-with-github-actions) actions 存储库中找到。存储库包含以下相关文件：
+ `github/workflows`:
  + `e2e-test.yaml` – 此文件调用 `workflow.yaml`，这是[可重用的工作流](https://docs.github.com/en/actions/sharing-automations/reusing-workflows)。当分支上有提交和推送操作时，就会触发此工作流。
  + `workflow.yaml` – 此文件包含该解决方案的可重用工作流，并配置为以 `workflow_call` 作为其触发器。作为可重用的工作流，`workflow.yaml` 可从任何其他工作流调用。
+ `templates`:
  + `servicecatalog-portfolio.yaml`— 此 CloudFormation 模板包括用于配置服务目录产品组合和服务目录产品的资源。模板包含在预调配 Service Catalog 组合和产品时使用的一组参数。一个参数接受用于上传 `vpc.yaml` 模板的 Amazon S3 文件 URL。尽管此模式包括 AWS 用于配置资源的`vpc.yaml`文件，但您也可以使用参数 S3 文件 URL 进行配置。
  + `vpc.yaml`— 此 CloudFormation 模板包含要添加到 Service Catalog 产品中的 AWS 资源。 AWS 资源包括子网 VPCs、互联网网关、NAT 网关和路由表。该`vpc.yaml`模板是如何将任何模板与 Service Catalog 产品和产品组合 CloudFormation 模板配合使用的示例。

## 最佳实践
<a name="provision-aws-service-catalog-products-using-github-actions-best-practices"></a>
+ 请参阅 AWS Service Catalog 文档 AWS Service Catalog中的[安全最佳实践](https://docs.aws.amazon.com/servicecatalog/latest/adminguide/security-best-practices.html)。
+ 有关[ GitHub 操作的信息，请参阅 GitHub 文档中的安全加固](https://docs.github.com/en/actions/security-for-github-actions/security-guides/security-hardening-for-github-actions)。

## 操作说明
<a name="provision-aws-service-catalog-products-using-github-actions-epics"></a>

### 设置本地工作站
<a name="set-up-local-workstation"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 在本地工作站上设置 Git。 | 要在本地工作站上安装和配置 Git，请遵照 Git 文档中的[开始使用 – 安装 Git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) 说明操作。 | 应用程序开发人员 | 
| 克隆 GitHub 项目存储库。 | 要克隆 GitHub 项目存储库，请执行以下操作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/provision-aws-service-catalog-products-using-github-actions.html) | DevOps 工程师 | 

### 设置 OIDC 提供程序
<a name="set-up-the-oidc-provider"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 配置 OIDC 提供程序。 | 创建一个 OpenID Connect (OIDC) 提供程序，允许 GitHub 操作工作流程访问其中的资源 AWS，而无需将 AWS 凭据存储为长期存在的机密。 GitHub 有关说明，请参阅文档[中的在亚马逊 Web Services 中配置 OpenID Connect](https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/configuring-openid-connect-in-amazon-web-services)。 GitHub OIDC 提供程序配置完成后，先前在[先决条件](#provision-aws-service-catalog-products-using-github-actions-prereqs)部分提到的 IAM 角色`github-actions`的信任策略将更新。 | AWS 管理员、AWS DevOps、常规 AWS | 

### 触发 GitHub 操作管道以部署 Service Catalog 产品组合和产品
<a name="trigger-github-actions-pipeline-to-deploy-sc-portfolio-and-products"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 更新 `e2e-test.yaml` | `e2e-test.yaml` 文件会触发 `workflow.yaml` 中的可重用工作流。更新并验证 `e2e-test.yaml` 中以下输入参数的值：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/provision-aws-service-catalog-products-using-github-actions.html) | DevOps 工程师 | 

### 验证部署
<a name="validate-deployment"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 验证 Service Catalog 资源。 | 要验证 Service Catalog 资源，请执行以下操作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/provision-aws-service-catalog-products-using-github-actions.html) | AWS DevOps | 

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


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 删除 CloudFormation 堆栈。 | 要删除 CloudFormation 堆栈，请执行以下操作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/provision-aws-service-catalog-products-using-github-actions.html)有关更多信息，请参阅 CloudFormation 文档[中的从 CloudFormation 控制台删除堆栈](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-console-delete-stack.html) | DevOps 工程师，AWS 管理员 | 

## 问题排查
<a name="provision-aws-service-catalog-products-using-github-actions-troubleshooting"></a>


| 问题 | 解决方案 | 
| --- | --- | 
| `e2e-test``Can't find 'action.yml', 'action.yaml' or 'Dockerfile' under '*/home/runner/work/service-catalog-with-github-actions/service-catalog-with-github-actions``Did you forget to run actions/checkout before running your local action?` | 要确保启用正确的存储库设置，请执行以下操作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/provision-aws-service-catalog-products-using-github-actions.html) | 

## 相关资源
<a name="provision-aws-service-catalog-products-using-github-actions-resources"></a>

**AWS 文档**
+ [Service Catalog 概述](https://docs.aws.amazon.com/servicecatalog/latest/adminguide/what-is_concepts.html)

**其他资源**
+ [关于触发工作流程的事件](https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows#about-events-that-trigger-workflows)（GitHub 文档）
+ [重复使用工作流程](https://docs.github.com/en/actions/sharing-automations/reusing-workflows)（GitHub 文档）

## 附加信息
<a name="provision-aws-service-catalog-products-using-github-actions-additional"></a>

要查看与 [Epics](#provision-aws-service-catalog-products-using-github-actions-epics) 相关的屏幕截图，请转到此模式存储**库中的 Images** 文件夹。 GitHub 以下屏幕截图可用：
+ [AWS Service Catalog 投资组合，管理部分](https://github.com/aws-samples/service-catalog-with-github-actions/blob/main/images/SC_portfolio.png)
+ [AWS Service Catalog 产品，“管理” 部分](https://github.com/aws-samples/service-catalog-with-github-actions/blob/main/images/SC_Product.png)
+ [AWS Service Catalog 产品， User/Provisioning 部分](https://github.com/aws-samples/service-catalog-with-github-actions/blob/main/images/SC_Product_User.png)