

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# 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) のフォルダが含まれています。これらの各フォルダには、次の 2 つのサブフォルダが含まれています。
+ **pipeline-modules** – このフォルダには、標準化されたパイプライン構造をデプロイするためのコードが含まれています。
+ **shared** – このフォルダには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 テンプレートを[AWS CloudFormation リソース仕様](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-resource-specification.html)と照合する linter です。また、リソースプロパティの有効な値の確認やベストプラクティスの遵守などの他のチェックも実行します。
+ [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. 次のように 2 つの個別の 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. 次の 2 つのセクションのいずれかに進みます。
   + [パイプラインを作成し、ステージを定義する (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 パイプラインが作成されると、 AWS CodeBuild の新しい IAM ロールが自動的に作成されます。この自動作成されたロールの名前は で終わります`-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 リポジトリの Uniform Resource Identifier (URI)
   + `CodeCommitAppRepo` – テンプレートを含む CodeCommit リポジトリの名前
   + `CodeCommitBaseRepo` – 共有ファイルを含む CodeCommit リポジトリの名前
   + `CodeCommitRepoBranch` – CodeCommit リポジトリブランチの名前
   + `SNSMailAddress` – パイプラインのステータスに関する Amazon Simple Notification Service (Amazon SNS) 通知を受け取る E メールアドレス

   手順については、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"
}
```