

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

# CodePipeline 用于部署 Terraform 和模板 CloudFormation
<a name="using-codepipeline"></a>

在 DPA 中，您可以使用的构建块为 AWS CodePipeline Terraform 和 IaC 创建加速器。 CloudFormation 本节介绍此用例的以下内容：
+ 标准化管道结构
+ 可重复使用的阶段和作业
+ 用于安全扫描的集成工具

DPA 存储库包含 [Terraform](https://github.com/aws-samples/aws-devops-pipeline-accelerator/tree/main/aws-codepipeline/terraform) 和。[CloudFormation](https://github.com/aws-samples/aws-devops-pipeline-accelerator/tree/main/aws-codepipeline/cloudformation)每个文件夹都包含以下两个子文件夹：
+ **pipeline-module** s — 此文件夹包含用于部署标准化管道结构的代码。
+ s@@ **h** ared — 此文件夹包含 DPA 阶段和作业的 ready-to-use buildspec 文件。

## 先决条件
<a name="prerequisites-codepipeline"></a>
+ 活跃的 AWS 账户
+ 使用 IaC 模板配置资源的权限
+ 创建 AWS CodeCommit 存储库和 CodePipeline 组件的权限

## 工具
<a name="tools-codepipeline"></a>
+ [cfn-lint](https://github.com/aws-cloudformation/cfn-lint) [是一款根据资源规范检查 CloudFormation YAML 或 JSON 模板的 linter。AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-resource-specification.html)它还会执行其他检查，例如检查资源属性的有效值以及是否遵守最佳实践。
+ [cfn\$1nag](https://github.com/stelligent/cfn_nag) 是一个开源工具，它通过搜索模式来识别 CloudFormation模板中的潜在安全问题。
+ [Checkov](https://www.checkov.io/1.Welcome/What%20is%20Checkov.html) 是一种静态代码分析工具，用于检查 IaC 是否存在安全性和合规性错误配置。
+ [TFLint](https://github.com/terraform-linters/tflint)是一个检查Terraform代码是否存在潜在错误以及是否遵守最佳实践的linter。
+ [tfsec](https://github.com/aquasecurity/tfsec) 是一种静态代码分析工具，用于检查 Terraform 代码是否存在潜在的配置错误。

## 说明
<a name="instructions-codepipeline"></a>

### 创建 CodeCommit 存储库
<a name="instructions-repo"></a>

1. 按如下方式创建两个单独的 CodeCommit 存储库：
   + `common-repo`— 此存储库包含共享库、buildspec 文件和依赖项。
   + `app-repo`— 此存储库包含用于部署基础架构的 Terraform 或 CloudFormation 模板。

   有关说明，请参阅[创建 AWS CodeCommit 存储库](https://docs.aws.amazon.com/codecommit/latest/userguide/how-to-create-repository.html)。

1. 在`common-repo`存储库中，创建一个名`shared`为的文件夹。将 Buildspec 文件从 GitHub DPA 存储库中的 [Terraform](https://github.com/aws-samples/aws-devops-pipeline-accelerator/tree/main/aws-codepipeline/terraform/shared) 或[CloudFormation](https://github.com/aws-samples/aws-devops-pipeline-accelerator/tree/main/aws-codepipeline/cloudformation/shared)**共享**文件夹复制到新文件夹。有关说明，请参阅[创建文件或向 AWS CodeCommit 存储库添加](https://docs.aws.amazon.com/codecommit/latest/userguide/how-to-create-file.html)文件。

1. 在`app-repo`存储库中，创建一个名为的文件夹`entrypoint`。将文件从 GitHub DPA 存储库中的 [Terraform](https://github.com/aws-samples/aws-devops-pipeline-accelerator/blob/main/examples/aws_codepipeline/terraform/entrypoint/terraform-infrastructure.json) 或 [CloudFormation](https://github.com/aws-samples/aws-devops-pipeline-accelerator/blob/main/examples/aws_codepipeline/cloudformation/entrypoint/config.json)**entrypoint** 文件夹复制到新文件夹。有关这些文件的更多信息，请参阅[了解入口点 JSON 文件](#understanding-json-file)。

1. 查看 [Terraform](https://github.com/aws-samples/aws-devops-pipeline-accelerator/tree/main/examples/aws_codepipeline/terraform) 或[CloudFormation](https://github.com/aws-samples/aws-devops-pipeline-accelerator/tree/main/examples/aws_codepipeline/cloudformation)**示例**目录，然后根据这些示例构建您的`app-repo`文件夹。这些目录包含部署亚马逊弹性计算云 (Amazon EC2) 实例或亚马逊简单存储服务 (Amazon S3) 存储桶的示例。

1. 继续阅读以下两个部分之一：
   + [创建管道并定义阶段 (Terraform)](#instructions-terraform-pipeline)
   + [创建管道并定义阶段 (CloudFormation)](#instructions-cfn-pipeline)

### 创建管道并定义阶段 (Terraform)
<a name="instructions-terraform-pipeline"></a>

1. 将[ DevOps 管道加速器 (DPA) 存储库](https://github.com/aws-samples/aws-devops-pipeline-accelerator)从克隆 GitHub到您的本地工作站。

1. 在克隆的存储库中，导航到该`aws-codepipeline/terraform/pipeline-modules`文件夹。

1. 在 **terraform.tfvars** 文件中，更新和验证 Terraform 状态和 (IAM) 特定于角色的变量。 AWS Identity and Access Management 

1. 创建 Docker 镜像。有关说明，请参阅[在 CodeBuild (GitHub) 中使用的 Docker 镜像创建](https://github.com/aws-samples/aws-devops-pipeline-accelerator/blob/main/shared-docker/docker-images/README.md)。

1. 更新 **terrafor** m.tfvars 文件中定义的`builder_image`变量。

1. 输入以下命令。这将通过 Terraform 初始化、预览和部署基础架构。

   ```
   terraform init
   terraform plan
   terraform apply
   ```

1. 登录到 AWS 账户。在[CodePipeline 控制台](https://console.aws.amazon.com/codesuite/codepipeline/home)中，确认新管道已创建。

   **注意**：如果第一次运行处于`failed`状态，请重复上一步操作。

1. 创建新 CodePipeline 管道后，将自动为创建新 AWS CodeBuild 的 IAM 角色。这个自动创建的角色的名称以结尾`-codebuild-role`。使用部署基础架构所需的权限更新此角色。

### 创建管道并定义阶段 (CloudFormation)
<a name="instructions-cfn-pipeline"></a>

1. 将[ DevOps 管道加速器 (DPA) 存储库](https://github.com/aws-samples/aws-devops-pipeline-accelerator)从克隆 GitHub到您的本地工作站。

1. 在克隆的存储库中，导航到该`aws-codepipeline/cloudformation/pipeline-modules`文件夹。

1. 部署 **pipeline-** CloudFormation cft.yaml 模板。以下是您必须传递到堆栈的必需参数。
   + `ArtifactsBucket`— 包含待更新的管道工件的存储库的名称
   + `EcrDockerRepository`— 带有标签的 Amazon ECR 存储库的统一资源标识符 (URI) `image`
   + `CodeCommitAppRepo`— 包含模板的 CodeCommit存储库的名称
   + `CodeCommitBaseRepo`— 包含共享文件的 CodeCommit存储库的名称
   + `CodeCommitRepoBranch`— CodeCommit 存储库分支的名称
   + `SNSMailAddress`— 用于接收亚马逊简单通知服务 (Amazon SNS) Simple Notification Service 有关管道状态的通知的电子邮件地址

   有关说明，[请参阅 CloudFormation 文档中的使用堆栈](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/stacks.html)。

1. 登录 AWS 账户。在[CodePipeline 控制台](https://console.aws.amazon.com/codesuite/codepipeline/home)中，确认新管道已创建。 

1. 创建新 CodePipeline 管道后，将自动为创建新 AWS CodeBuild 的 IAM 角色。这个自动创建的角色的名称以结尾`-codebuild-role`。使用部署基础架构所需的权限更新此角色。

## 了解入口点 JSON 文件
<a name="understanding-json-file"></a>

### Terraform 入口点文件
<a name="entry-point-terraform"></a>

这是主配置文件。在此文件中，您可以自定义、启用或禁用舞台。如果您禁用某个阶段，它不会从管道中删除或移除该舞台。相反，在运行时会跳过该阶段。

```
{
    "build_stage_required" : "true",
    "test_stage_required" : "true",
    "predeploy_stage_required": "true",
    "deploy_stage_required": "true",
    "postdeploy_stage_required": "true",
    "destroy_stage_required": "true",
    "bucket":"tf-state-dpa",                # S3 bucket used for Terraform backend
    "key":"terraform_test.tfstate",         # S3 key to be used
    "region":"us-east-1",       
    "dynamodb_table":"tf-state-dpa"         # DynamoDB Table for Terraform backend
}
```

### CloudFormation 入口点文件
<a name="entry-point-cfn"></a>

这是主配置文件。在此文件中，您可以自定义阶段并启用或禁用它们。如果您禁用某个阶段，它不会从管道中删除或移除该舞台。相反，管道会在运行时跳过该阶段。

```
{
    "init_stage_required" : "true",
    "test_stage_required" : "true",
    "createinfra_stage_required": "true",
    "envType" : "cloudformation",
    "stage_required" : "true",
    "cft_s3_bucket" : "pipeline-bucket",               #S3 bucket from the destination account to keep CFT templates
    "stack_name" : "aws-cft-poc",                      #CloudFormation stack name
    "account" : "************",                        #Destination AWS account to deploy stack
    "roleName" : "codestack-poc-cross-account-role",   #Cross-account IAM role name
    "region" : "us-east-1",
    "destroy_stack" : "false"                          #To destroy the provisioned stack, set this value to "true"
}
```