

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

# 使用 CodePipeline 部署 Terraform 和 CloudFormation 範本
<a name="using-codepipeline"></a>

在 DPA 中，您可以使用 的建置區塊 AWS CodePipeline 來建立 Terraform 和 CloudFormation IaC 的加速器。本節說明此使用案例的下列項目：
+ 標準化管道結構
+ 可重複使用的階段和任務
+ 用於安全性掃描的整合式工具

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) 的資料夾。這些資料夾都包含下列兩個子資料夾：
+ **管道模組** – 此資料夾包含用於部署標準化管道結構的程式碼。
+ **shared** – 此資料夾包含 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) 是一種 linter，可根據[AWS CloudFormation 資源規格](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-resource-specification.html)檢查 CloudFormation YAML 或 JSON 範本。它也會執行其他檢查，例如檢查資源屬性的有效值，以及是否遵守最佳實務。
+ [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) 是 linter，可檢查 Terraform 程式碼是否有潛在錯誤並遵循最佳實務。
+ [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) **進入點**資料夾複製到新資料夾。如需這些檔案的詳細資訊，請參閱 [了解進入點 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 Elastic Compute Cloud (Amazon EC2) 執行個體或 Amazon Simple Storage Service (Amazon S3) 儲存貯體的範例。

1. 繼續前往下列兩個區段之一：
   + [建立管道並定義階段 (Terraform)](#instructions-terraform-pipeline)
   + [建立管道並定義階段 (CloudFormation)](#instructions-cfn-pipeline)

### 建立管道並定義階段 (Terraform)
<a name="instructions-terraform-pipeline"></a>

1. 將[ DevOps Pipeline Accelerator (DPA) 儲存庫](https://github.com/aws-samples/aws-devops-pipeline-accelerator)從 GitHub 複製到本機工作站。

1. 在複製的儲存庫中，導覽至 `aws-codepipeline/terraform/pipeline-modules` 資料夾。

1. 在 **terraform.tfvars** 檔案中，更新並驗證 Terraform 狀態和 AWS Identity and Access Management (IAM) 角色特定變數。

1. 建立 Docker 映像。如需說明，請參閱在 [ CodeBuild (GitHub) 中使用 的 Docker 映像建立](https://github.com/aws-samples/aws-devops-pipeline-accelerator/blob/main/shared-docker/docker-images/README.md)。 GitHub

1. 更新 **terraform.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 管道時，會自動為 建立新的 IAM 角色 AWS CodeBuild 。此自動建立角色的名稱結尾為 `-codebuild-role`。使用部署基礎設施所需的許可更新此角色。

### 建立管道並定義階段 (CloudFormation)
<a name="instructions-cfn-pipeline"></a>

1. 將[ DevOps Pipeline Accelerator (DPA) 儲存庫](https://github.com/aws-samples/aws-devops-pipeline-accelerator)從 GitHub 複製到本機工作站。

1. 在複製的儲存庫中，導覽至 `aws-codepipeline/cloudformation/pipeline-modules` 資料夾。

1. 部署 **pipeline-cft.yaml** CloudFormation 範本。以下是您必須傳遞至堆疊的必要參數。
   + `ArtifactsBucket` – 包含要更新之管道成品的儲存庫名稱
   + `EcrDockerRepository` – 具有 `image`標籤之 Amazon ECR 儲存庫的統一資源識別符 (URI)
   + `CodeCommitAppRepo` – 包含範本的 CodeCommit 儲存庫名稱
   + `CodeCommitBaseRepo` – 包含共用檔案的 CodeCommit 儲存庫名稱
   + `CodeCommitRepoBranch` – CodeCommit 儲存庫分支的名稱
   + `SNSMailAddress` – 將收到管道狀態相關 Amazon Simple Notification Service (Amazon SNS) 通知的電子郵件地址

   如需說明，請參閱 CloudFormation 文件中的[使用堆疊](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/stacks.html)。

1. 登入 AWS 帳戶。在 [CodePipeline 主控台](https://console.aws.amazon.com/codesuite/codepipeline/home)中，確認已建立新的管道。 

1. 建立新的 CodePipeline 管道時，會自動為 建立新的 IAM 角色 AWS CodeBuild 。此自動建立角色的名稱結尾為 `-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"
}
```