

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

# 使用代码存储库在中 AWS Service Catalog 配置 Terraform 产品
<a name="provision-a-terraform-product-in-aws-service-catalog-by-using-a-code-repository"></a>

*Rahul Sharad Gaikwad 博士和 Tamilselvan P，Amazon Web Services*

## Summary
<a name="provision-a-terraform-product-in-aws-service-catalog-by-using-a-code-repository-summary"></a>

AWS Service Catalog 支持自助服务配置，并管理您的 [HashiCorp Terraform](https://developer.hashicorp.com/terraform/tutorials/aws-get-started) 配置。如果您使用 Terraform，则可以使用 Service Catalog 作为单一工具，在其中大规模组织、管理和分发 Terraform 配置。 AWS 您可以访问 Service Catalog 的主要功能，包括对标准化和预先批准的基础架构即代码 (IaC) 模板进行编目、访问控制、使用最低权限访问的云资源配置、版本控制、与数 AWS 账户千人共享以及标记。最终用户（例如工程师、数据库管理员和数据科学家）会看到其有权访问的产品和版本列表，并且只需一个操作即可完成部署。

此模式可帮助您使用 Terraform 代码部署 AWS 资源。 GitHub 存储库中的 Terraform 代码可通过 Service Catalog 进行访问。借助此方法，您可以将产品整合到现有的 Terraform 工作流中。管理员可以使用 Terraform 创建 Service Catalog AWS Launch Wizard 产品组合并向其中添加产品。

这种解决方案的优点如下：
+ 由于 Service Catalog 中具有回滚功能，因此如果在部署过程中出现任何问题，可以将产品恢复到以前的版本。
+ 您可以轻松识别不同产品版本之间的差异。这有助于在部署期间解决问题。
+ 您可以在 Service Catalog 中配置存储库连接，例如到 GitHub 或 GitLab。您可以直接通过存储库更改产品。

有关总体优势的信息 AWS Service Catalog，请参阅[什么是 Service Catalog](https://docs.aws.amazon.com/servicecatalog/latest/adminguide/introduction.html)。

## 先决条件和限制
<a name="provision-a-terraform-product-in-aws-service-catalog-by-using-a-code-repository-prereqs"></a>

**先决条件**
+ 活跃 AWS 账户的.
+  GitHub BitBucket、或其他包含 ZIP 格式的 Terraform 配置文件的存储库。
+ AWS Serverless Application Model [已安装](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html)命令行界面 (AWS SAM CLI)。
+ AWS Command Line Interface (AWS CLI)，[已安装](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)并[配置](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html)。
+ [已安装](https://go.dev/doc/install) Go。
+ Python 版本 3.9，[已安装](https://www.python.org/downloads/release/python-3913/)。 AWS SAM CLI 需要这个版本的 Python。
+ 编写和运行 AWS Lambda 函数的权限以及访问和管理 Service Catalog 产品和产品组合的权限。

## 架构
<a name="provision-a-terraform-product-in-aws-service-catalog-by-using-a-code-repository-architecture"></a>

![\[通过代码存储库在 Service Catalog 中预调配 Terraform 产品的架构图\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/images/pattern-img/7d0d76e8-9485-4b3f-915f-481b6a7cdcd9/images/e83fa44a-4ca6-4438-a0d1-99f09a3541bb.png)


下图显示了如下工作流：

1. 当 Terraform 配置准备就绪后，开发人员会创建一个包含所有 Terraform 代码的 .zip 文件。开发人员将 .zip 文件上传至连接到 Service Catalog 的代码存储库中。

1. 管理员将 Terraform 产品与 Service Catalog 中的产品组合关联。管理员还会创建启动约束，以便最终用户预调配产品。

1. 在 Service Catalog 中，最终用户使用 Terraform 配置启动 AWS 资源。他们可以选择要部署的产品版本。

## 工具
<a name="provision-a-terraform-product-in-aws-service-catalog-by-using-a-code-repository-tools"></a>

**AWS 服务**
+ [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) 是一项计算服务，可帮助您运行代码，无需预调配或管理服务器。它只在需要时运行您的代码，并自动进行扩展，因此您只需为使用的计算时间付费。
+ [AWS Service Catalog](https://docs.aws.amazon.com/servicecatalog/latest/adminguide/introduction.html)帮助您集中管理已获批准的 IT 服务目录。 AWS最终用户可在遵循组织设定约束的情况下快速部署他们所需已获得批准的 IT 服务。

**其他服务**
+ [Go](https://go.dev/doc/install) 是谷歌支持的开源编程语言。
+ [Python](https://www.python.org/) 是通用的计算机编程语言。

**代码存储库**

如果您需要可通过 Service Catalog 部署的 Terraform 示例配置，则可以使用 [Amazon Mac GitHub ie 组织设置](https://github.com/aws-samples/aws-macie-customization-terraform-samples)使用 Terraform 存储库中的配置。并非必须使用此存储库中的代码示例。

## 最佳实践
<a name="provision-a-terraform-product-in-aws-service-catalog-by-using-a-code-repository-best-practices"></a>
+ 在通过 Service Catalog 启动产品时配置变量值，而不是在 Terraform 配置文件 (`terraform.tfvars`) 中提供变量的值。
+ 仅向特定用户或管理员授予对产品组合的访问权限。
+ 遵循最低权限原则，并授予执行任务所需的最低权限。有关更多信息，请参阅 AWS Identity and Access Management (IAM) 文档中的[授予最低权限](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies.html#grant-least-priv)和[安全最佳实践](https://docs.aws.amazon.com/IAM/latest/UserGuide/IAMBestPracticesAndUseCases.html)。

## 操作说明
<a name="provision-a-terraform-product-in-aws-service-catalog-by-using-a-code-repository-epics"></a>

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


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| （可选）安装 Docker。 | 如果要在开发环境中运行这些 AWS Lambda 函数，请安装 Docker。有关说明，请参阅 Docker 文档中的[安装 Docker 引擎](https://docs.docker.com/engine/install/)。 | DevOps 工程师 | 
| 为 Terraform 安装 AWS Service Catalog 引擎。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/provision-a-terraform-product-in-aws-service-catalog-by-using-a-code-repository.html) | DevOps 工程师，AWS 管理员 | 

### Connect GitHub 存储库
<a name="connect-the-github-repository"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 创建与 GitHub 存储库的连接。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/provision-a-terraform-product-in-aws-service-catalog-by-using-a-code-repository.html) | AWS 管理员 | 

### 在 Service Catalog 中创建 Terraform 产品
<a name="create-a-terraform-product-in-service-catalog"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 创建 Service Catalog 产品。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/provision-a-terraform-product-in-aws-service-catalog-by-using-a-code-repository.html) | AWS 管理员 | 
| 创建产品组合。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/provision-a-terraform-product-in-aws-service-catalog-by-using-a-code-repository.html) | AWS 管理员 | 
| 将 Terraform 产品添加到产品组合中。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/provision-a-terraform-product-in-aws-service-catalog-by-using-a-code-repository.html) | AWS 管理员 | 
| 创建访问策略。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/provision-a-terraform-product-in-aws-service-catalog-by-using-a-code-repository.html) | AWS 管理员 | 
| 创建自定义信任策略。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/provision-a-terraform-product-in-aws-service-catalog-by-using-a-code-repository.html) | AWS 管理员 | 
| 向 Service Catalog 产品添加启动约束。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/provision-a-terraform-product-in-aws-service-catalog-by-using-a-code-repository.html) | AWS 管理员 | 
| 授予对产品的访问权限。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/provision-a-terraform-product-in-aws-service-catalog-by-using-a-code-repository.html) | AWS 管理员 | 
| 启动产品。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/provision-a-terraform-product-in-aws-service-catalog-by-using-a-code-repository.html) | DevOps 工程师 | 

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


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 验证部署。 | Service Catalog 配置工作流程有两台 AWS Step Functions 状态机：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/provision-a-terraform-product-in-aws-service-catalog-by-using-a-code-repository.html)您可以检查 `ManageProvisionedProductStateMachine` 状态机的日志，以确认产品已预调配。[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/provision-a-terraform-product-in-aws-service-catalog-by-using-a-code-repository.html) | DevOps 工程师 | 

### 清理基础设施
<a name="clean-up-infrastructure"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 删除预调配产品。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/provision-a-terraform-product-in-aws-service-catalog-by-using-a-code-repository.html) | DevOps 工程师 | 
| 移除 Terraform 的 AWS Service Catalog 引擎。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/provision-a-terraform-product-in-aws-service-catalog-by-using-a-code-repository.html) | AWS 管理员 | 

## 相关资源
<a name="provision-a-terraform-product-in-aws-service-catalog-by-using-a-code-repository-resources"></a>

**AWS 文档**
+ [开始使用 Terraform 产品](https://docs.aws.amazon.com/servicecatalog/latest/adminguide/getstarted-Terraform.html)

**Terraform 文档**
+ [Terraform installation](https://learn.hashicorp.com/tutorials/terraform/install-cli)（Terraform 安装）
+ [Terraform 后端配置](https://developer.hashicorp.com/terraform/language/backend)
+ [Terraform AWS 提供者文档](https://registry.terraform.io/providers/hashicorp/aws/latest/docs)

## 附加信息
<a name="provision-a-terraform-product-in-aws-service-catalog-by-using-a-code-repository-additional"></a>

**访问策略**

```
{
    "Version": "2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": "s3:GetObject",
            "Resource": "*",
            "Condition": {
                "StringEquals": {
                    "s3:ExistingObjectTag/servicecatalog:provisioning": "true"
                }
            }
        },
        {
            "Action": [
                "s3:CreateBucket*",
                "s3:DeleteBucket*",
                "s3:Get*",
                "s3:List*",
                "s3:PutBucketTagging"
            ],
            "Resource": "arn:aws:s3:::*",
            "Effect": "Allow"
        },
        {
            "Action": [
                "resource-groups:CreateGroup",
                "resource-groups:ListGroupResources",
                "resource-groups:DeleteGroup",
                "resource-groups:Tag"
            ],
            "Resource": "*",
            "Effect": "Allow"
        },
        {
            "Action": [
                "tag:GetResources",
                "tag:GetTagKeys",
                "tag:GetTagValues",
                "tag:TagResources",
                "tag:UntagResources"
            ],
            "Resource": "*",
            "Effect": "Allow"
        }
    ]
}
```

**信任策略**

```
{
    "Version": "2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "GivePermissionsToServiceCatalog",
            "Effect": "Allow",
            "Principal": {
                "Service": "servicecatalog.amazonaws.com"
            },
            "Action": "sts:AssumeRole"
        },
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::account_id:root"
            },
            "Action": "sts:AssumeRole",
            "Condition": {
                "StringLike": {
                    "aws:PrincipalArn": [
                        "arn:aws:iam::accounti_id:role/TerraformEngine/TerraformExecutionRole*",
                        "arn:aws:iam::accounti_id:role/TerraformEngine/ServiceCatalogExternalParameterParserRole*",
                        "arn:aws:iam::accounti_id:role/TerraformEngine/ServiceCatalogTerraformOSParameterParserRole*"
                    ]
                }
            }
        }
    ]
}
```