AWS CodePipeline、CodeBuild、CodeDeploy、CodeConnections、CodeArtifactを使用したCI/CDパイプライン(継続的な統合・デプロイの自動化)の構築・設定に対応しています。 CodePipeline V2(トリガー設定、変数管理、実行モード、複数AWSアカウント間の連携)、buildspec.yml(キャッシング、VPC、Docker)、CodeDeploy戦略(ブルーグリーンデプロイメント、カナリアデプロイメント、段階的デプロイメント)、CodeArtifact(プライベートパッケージレジストリ、認証トークン、複数アカウント間の連携)、ソース接続(GitHub、GitLab、Bitbucket)をカバーしています。 次のような場合に使用: CodePipeline、CodeBuild、CodeDeploy、CodeConnections、CodeArtifact、buildspec.yml、appspec.yml、またはCI/CDパイプラインのオーケストレーション(処理手順の自動制御)が関連する場合。 **対応していません:** - ECS Fargate のサービスやタスク定義(aws-containers を使用してください) - CDK Pipelines または cdk deploy(aws-cdk を使用してください) - sam deploy(aws-serverless を使用してください) - Amplify デプロイメント(aws-amplify を使用してください) - GitHub Actions / GitLab CI
Configures CI/CD pipelines using AWS CodePipeline, CodeBuild, CodeDeploy, CodeConnections, and CodeArtifact. Covers CodePipeline V2 (triggers, variables, execution modes, cross-account), buildspec.yml (caching, VPC, Docker), CodeDeploy strategies (blue/green, canary, linear), CodeArtifact (private package registries, auth tokens, cross-account), and source connections (GitHub, GitLab, Bitbucket). Applies when CodePipeline, CodeBuild, CodeDeploy, CodeConnections, CodeArtifact, buildspec.yml, appspec.yml, or CI/CD pipeline orchestration is referenced. Does NOT cover: ECS Fargate services or task definitions (use aws-containers), CDK Pipelines or cdk deploy (use aws-cdk), sam deploy (use aws-serverless), Amplify deployments (use aws-amplify), or GitHub Actions/GitLab CI.
最適な使用環境: AWS MCP サーバーと組み合わせると、CLI コマンドの実行と設定の検証を直接行えます。標準的な AWS CLI でも全てのガイダンスが適用されます。
CodeConnections の「保留中」の罠: CLI やCloudFormation 経由で作成した接続は無期限に「保留中」のままになります。必ず AWS コンソールで OAuth 認証を完了させてください。API のみでは完了できません。
複数 AWS アカウント間のデプロイは3つ全て必須: (1) ターゲットアカウントに権限を付与する KMS キーポリシー(キーのエイリアスではなく ID を使用)、(2) ターゲットアカウント向けの S3 バケットポリシー、(3) 信頼ポリシー付きのクロスアカウント IAM ロール。いずれか1つでも欠けると、わかりにくい「アクセス拒否」エラーが出ます。
CodeDeploy の ApplicationStop は以前のバージョンを使用: 前回のデプロイで停止スクリプトが壊れていると、その後のデプロイが全てブロックされます。停止スクリプトは冪等(べきとう:何度実行しても同じ結果)にし、サービスが存在しないときは終了コード 0 を返すようにしてください。--ignore-application-stop-failures で解除できます。
CodeBuild の VPC で NAT ゲートウェイなし: VPC サブネット内で NAT ゲートウェイなしでビルドを実行すると、DOWNLOAD_SOURCE で無言のうちにハングします。プライベートサブネットには必ず NAT ゲートウェイまたは VPC エンドポイントを設置してください。
CodeConnections の IAM: API 呼び出しと IAM ポリシーのアクション(権限)は codeconnections: で始まる形式を使用してください。リソース ARN は完全に一致する必要があります。新規リソースは codeconnections 接頭辞、既存リソースは codestar-connections 接頭辞の場合があります。新旧混在の場合は両方をリソースに指定してください。
UseConnection は権限が広すぎます: codeconnections:UseConnection は接続が到達できる全てのリポジトリへのアクセスを付与します。条件キー(codeconnections:FullRepositoryId、codeconnections:ProviderAction、codeconnections:BranchName)を指定し、CodeBuild を必要なリポジトリのみに制限してください。
CodeConnections → CodeBuild → CodeDeploy を、CodePipeline がまとめます。
| レイヤー | サービス | 役割 |
|---|---|---|
| ソース | CodeConnections | GitHub/GitLab/Bitbucket に認証し、コードを取得 |
| パッケージ | CodeArtifact | プライベートパッケージレジストリ、公開レジストリからの依存関係キャッシュ |
| ビルド/テスト | CodeBuild | コンパイル、テスト、成果物のパッケージ化 |
| デプロイ | CodeDeploy | EC2/ECS/Lambda へのデプロイ、トラフィック振り分け戦略を適用 |
| オーケストレーター | CodePipeline | ステージをつなぎ、遷移とゲートを管理 |
デフォルト: V2 パイプラインタイプ、キューイング実行モード。並列実行モードは、各実行が完全に独立している場合のみ使用してください。
| やりたいこと | 参照先 |
|---|---|
| パイプラインを作成(V2、トリガー、変数、実行モード) | codepipeline.md |
| GitHub/GitLab/Bitbucket をソースとして接続 | codeconnections.md |
| buildspec.yml を書く、ビルドを設定 | codebuild.md |
| ビルド用のプライベートパッケージレジストリを構築 | codeartifact.md |
| デプロイ戦略を設定(ブルーグリーン、カナリアリリース) | codedeploy.md |
| 複数アカウント間またはリージョン間のデプロイ | codepipeline.md |
| 失敗したパイプライン、ビルド、デプロイを修復 | troubleshooting.md |
| タスク | 作業内容 | 参照先 |
|---|---|---|
| GitHub から ECS へのパイプライン | 接続を作成 → CodeBuild Docker ステージ → CodeDeploy ECS ブルーグリーン | codepipeline, codedeploy |
| パイプラインがソース段階で止まっている | 接続ステータスを確認。保留中の場合、AWS コンソールで OAuth を完了 | troubleshooting |
| ビルドがタイムアウト | VPC/NAT を確認、timeoutInMinutes を増やす、Docker 特権モード(特別な実行権限)を有効化 |
codebuild |
| 別のアカウントにデプロイ | KMS + S3 バケットポリシー + クロスアカウントロールを設定、アクションに RoleArn を追加 |
codepipeline |
| 失敗したデプロイをロールバック | アラーム/失敗で自動ロールバック。手動: stop-deployment --auto-rollback-enabled |
codedeploy |
| Lambda カナリアリリース | CodeBuild でパッケージ化 → CodeDeploy でカナリアトラフィック振り分け | codedeploy |
| エラー/症状 | 原因 | 解決方法 |
|---|---|---|
CodeBuild の YAML_FILE_ERROR |
buildspec に runtime-versions が欠落または形式が不正(標準イメージ推奨) |
install フェーズに runtime-versions ブロックを追加 |
CodeDeploy で file already exists |
上書き設定なしで再デプロイ | file_exists_behavior: OVERWRITE を設定 |
| パイプライントリガーが動作しない | ファイルパスフィルターは差分の最初の 100 ファイルのみ確認 | パスフィルタスコープを縮小、またはマージサイズを小さく |
| PARALLEL モードで誤ったリビジョン | イベントとソースアクション間の競争状態 | QUEUED モード(順序保証)を使用 |
Docker: Cannot connect to daemon |
特権モード(特別な実行権限)が未設定 | privilegedMode: true を設定かつ buildspec で dockerd を起動 |
CODEBUILD_CLONE_REF 権限エラー |
CodeBuild ロールに UseConnection 権限がない | CodeBuild サービスロールに codeconnections:UseConnection を追加 |
| デプロイが完了しない | MinimumHealthyHosts(最小健全ホスト数)がインスタンス数に対して多すぎる | 健全性閾値 < インスタンス総数 を確保 |
| ECS デプロイが止まっている | 新しいタスクセットのヘルスチェック失敗 | ターゲットグループのヘルスチェックパス/ポートを確認 |
シークレットは必ず Secrets Manager または Parameter Store に保存し、CodeBuild の type: SECRETS_MANAGER で参照してください。buildspec に平文で埋め込まないでください。
クロスアカウント間の成果物暗号化には必ずカスタマーマネージド KMS キーを使用してください。デフォルト暗号化ではクロスアカウントに対応しません。
CodeBuild/CodeDeploy サービスロールは特定のリソース ARN にスコープ制限してください。s3:GetObject や kms:Decrypt で * ワイルドカード(全て)を使用しないでください。
ソース接続には CodeConnections を使用してください。個人用アクセストークンは使わないでください。OAuth トークンは自動ローテーションできません。
詳しくは CodePipeline セキュリティベストプラクティスを参照してください。
| トピック | 代わりに参照 |
|---|---|
CDK Pipelines(aws-cdk-lib/pipelines) |
aws-cdk |
sam deploy / SAM CLI |
aws-serverless |
| ECS サービスデプロイ設定(サーキットブレーカー、ローリングパラメーター) | aws-containers |
| GitHub Actions / GitLab CI | サードパーティツール、本ドキュメント外 |
Works best with the AWS MCP server for running CLI commands and validating configurations directly. All guidance also works with standard AWS CLI.
CodeConnections PENDING trap: Connections created via CLI/CloudFormation remain PENDING indefinitely — MUST complete OAuth in the AWS Console. No API-only path exists.
Cross-account triple requirement: Cross-account deploys need ALL THREE: (1) KMS key policy granting target account (use key ID, not alias), (2) S3 bucket policy for target account, (3) cross-account IAM role with trust policy. Missing any one = cryptic Access Denied.
CodeDeploy ApplicationStop uses PREVIOUS revision: Broken stop scripts in a prior deployment block ALL future deploys. Make stop scripts idempotent (exit 0 if service absent). Unblock with --ignore-application-stop-failures.
CodeBuild VPC without NAT: Builds in VPC subnets without NAT gateway hang at DOWNLOAD_SOURCE silently. Private subnets MUST have NAT gateway or VPC endpoints.
CodeConnections IAM: Use codeconnections: prefix for API calls and IAM policy Actions. Resource ARNs must match exactly — new resources use codeconnections prefix, existing resources may use codestar-connections prefix. Specify both in Resource if you have mixed-age resources.
UseConnection is over-permissive: codeconnections:UseConnection grants access to ALL repositories the connection can reach. MUST specify condition keys (codeconnections:FullRepositoryId, codeconnections:ProviderAction, codeconnections:BranchName) to limit CodeBuild to only the required repository.
CodeConnections → CodeBuild → CodeDeploy, orchestrated by CodePipeline.
| Layer | Service | Role |
|---|---|---|
| Source | CodeConnections | Authenticates to GitHub/GitLab/Bitbucket, delivers code |
| Packages | CodeArtifact | Private package registry, dependency caching from public registries |
| Build/Test | CodeBuild | Compiles, tests, packages artifacts |
| Deploy | CodeDeploy | Deploys to EC2/ECS/Lambda with traffic shifting strategies |
| Orchestrator | CodePipeline | Chains stages, manages transitions, approval gates |
Default: V2 pipeline type with QUEUED execution mode. Use PARALLEL only when executions are fully independent.
| You want to... | Go to |
|---|---|
| Create a pipeline (V2, triggers, variables, modes) | codepipeline.md |
| Connect GitHub/GitLab/Bitbucket source | codeconnections.md |
| Write buildspec.yml / configure builds | codebuild.md |
| Set up private package registry for builds | codeartifact.md |
| Configure deployment strategy (blue/green, canary) | codedeploy.md |
| Cross-account or cross-region deployment | codepipeline.md |
| Fix failing pipeline, build, or deployment | troubleshooting.md |
| Task | Action | Reference |
|---|---|---|
| Pipeline from GitHub to ECS | Create connection → CodeBuild Docker stage → CodeDeploy ECS blue/green | codepipeline, codedeploy |
| Pipeline stuck at source | Check connection status; if PENDING, complete OAuth in AWS Console | troubleshooting |
| Build timing out | Check VPC/NAT, increase timeoutInMinutes, verify Docker privileged mode |
codebuild |
| Deploy to another account | Configure KMS + S3 bucket policy + cross-account role, add RoleArn to action |
codepipeline |
| Roll back failed deployment | Auto-rollback on alarm/failure; manual: stop-deployment --auto-rollback-enabled |
codedeploy |
| Lambda canary deployment | CodeBuild packages → CodeDeploy Lambda with canary traffic shifting | codedeploy |
| Error/Symptom | Cause | Fix |
|---|---|---|
YAML_FILE_ERROR in CodeBuild |
Missing or malformed runtime-versions in buildspec (recommended for standard images) |
Add runtime-versions block in install phase |
file already exists on CodeDeploy |
Redeployment without overwrite config | Set file_exists_behavior: OVERWRITE |
| Pipeline trigger not firing | File path filter checks only first 100 files in diff | Reduce path filter scope or merge smaller |
| PARALLEL mode wrong revision | Race between event and source action | Use QUEUED mode for sequential consistency |
Docker: Cannot connect to daemon |
Missing privileged mode | Set privilegedMode: true AND start dockerd in buildspec |
CODEBUILD_CLONE_REF permission error |
CodeBuild role missing UseConnection | Add codeconnections:UseConnection to CodeBuild service role |
| Deployment never completes | MinimumHealthyHosts too high for instance count | Ensure healthy threshold < total instances |
| ECS deployment stuck | Health check failing on new task set | Verify target group health check path/port |
type: SECRETS_MANAGER — MUST NOT embed in buildspec as PLAINTEXT* for s3:GetObject or kms:Decrypt| Topic | Use instead |
|---|---|
CDK Pipelines (aws-cdk-lib/pipelines) |
aws-cdk |
sam deploy / SAM CLI |
aws-serverless |
| ECS service deployment config (circuit breaker, rolling params) | aws-containers |
| GitHub Actions / GitLab CI | Third-party tools, not covered |
原文・著作権は Anthropic および各プラグイン作者に帰属します。日本語訳は Claude API による自動翻訳です。