CodePipeline

【VPC外リージョンサービス】

CodePipeline は、フルマネージド型の CI/CD(継続的インテグレーション/継続的デリバリー)オーケストレーションサービス です。

ソース取得(CodeCommit/GitHub/S3)から、ビルド(CodeBuild)、テスト、承認、デプロイ(CodeDeploy/ECS/EKS/Lambda/CloudFormation など)までのプロセスをStage(ステージ) として定義し、自動で実行することができます。

各ステージは、複数の Action(アクション) で構成され、Transition(トランジション) によって次のステージへと遷移します。ステージ内のアクションは直列・並列実行の両方に対応しており、柔軟なパイプライン設計が可能です。また、リリース前の品質保証を目的として Manual Approval(手動承認) ステップを挿入したり、Canary Deployment(カナリアデプロイ) Blue/Green Deployment(ブルーグリーンデプロイ) といった高度なデプロイ戦略を組み込むこともできます。

パイプラインの実行トリガーは、コミットによる変更検知やスケジュールに基づいて行われます。この際、各ビルドやデプロイがどのソース変更に基づいて実行されたかを示す Source Revision を自動的に追跡できます。

Artifact(アーティファクト) は S3 に保存され、ステージ間で成果物として受け渡されます。機密情報や認証情報は AWS Secrets Manager や Systems Manager Parameter Store で安全に管理します。

さらに、Cross-Region Pipeline(クロスリージョンパイプライン) により複数リージョンにまたがるデプロイを行ったり、Cross-Account Deployment(クロスアカウントデプロイ) によって複数アカウント間で統一的なリリースを実施することも可能です。

重要用語

ユースケース

継続的インテグレーション/デリバリー(CI/CD)パイプラインコードの変更をトリガーに、ビルド・テスト・承認・本番デプロイまでの一連のフローを自動化する。
ステージングと本番の段階的リリースdev → stg → prod のような複数ステージを定義し、テストや承認ステップを挟みながら順番にリリースしていく。
マルチアカウント/マルチリージョンデプロイ別アカウントや別リージョンにまたがるデプロイステップをパイプラインに組み込み、大規模な環境のリリースを一元管理する。

ベストプラクティス

ステージ分離と承認アクション本番リリース前に手動承認ステージを挟み、安全なデプロイフローを構築する。
小さなパイプラインの組み合わせマイクロサービスごとにパイプラインを分けて変更の影響範囲を限定する。
CloudWatch Events連携パイプライン状態に応じて通知や自動アクションをトリガーする。

セキュリティ

関連サービス設定内容
KMS(データの暗号化と鍵の安全管理)-
Secrets Manager(機密情報の安全管理)シークレット(秘密情報)の作成が推奨
 Git認証情報・APIキー・環境設定パスワード
SSM Parameter Store(設定情報の一元管理)パラメータ(Parameter)の作成が推奨
 環境依存変数・リポジトリURL・APIキー
CloudTrail(操作履歴の記録・監査・追跡)【自動記録】
作成・更新・削除・設定変更は自動記録される。(コントロールプレーンAPI)
データ操作は追跡できない(データプレーンAPI)
Config(リソースの構成状態・設定変更を記録)【Configが有効な場合】
パイプライン設定変更履歴・アーティファクト暗号化/ステージ設定の準拠評価
GuardDuty(脅威を自動検出)【GuardDutyが有効な場合】
パイプライン改ざんのAPI異常検知
CodePipeline が実行ロールを引き受ける典型的な連携パターン
実行ロールを介して連携するサービス実行ロールにアタッチするポリシー
CloudWatch Logslogs:CreateLogGroup
logs:CreateLogStream
logs:PutLogEvents
CloudWatch Metricscloudwatch:PutMetricData
ECRecr:DescribeImages
ecr:GetAuthorizationToken
ecr:BatchCheckLayerAvailability
ecr:GetDownloadUrlForLayer
ecr:BatchGetImage
S3s3:ListBucket
s3:GetObject
s3:PutObject
s3:GetObjectVersion
s3:GetBucketVersioning
s3:PutObjectAcl
CodeCommitcodecommit:GetBranch
codecommit:GetCommit
codecommit:UploadArchive
codecommit:GetUploadArchiveStatus
codecommit:CancelUploadArchive
CodeBuildcodebuild:BatchGetBuilds
codebuild:StartBuild
CodeDeploycodedeploy:CreateDeployment
codedeploy:GetApplication
codedeploy:GetApplicationRevision
codedeploy:GetDeployment
codedeploy:GetDeploymentConfig
codedeploy:RegisterApplicationRevision
CloudFormationcloudformation:CreateStack
cloudformation:DeleteStack
cloudformation:DescribeStacks
cloudformation:UpdateStack
cloudformation:CreateChangeSet
cloudformation:DeleteChangeSet
cloudformation:DescribeChangeSet
cloudformation:ExecuteChangeSet
cloudformation:SetStackPolicy
cloudformation:ValidateTemplate
iam:PassRole
Lambdalambda:InvokeFunction
lambda:ListFunctions
ECSecs:DescribeServices
ecs:DescribeTaskDefinition
ecs:DescribeTasks
ecs:ListTasks
ecs:RegisterTaskDefinition
ecs:UpdateService
iam:PassRole
Step Functionsstates:DescribeExecution states:DescribeStateMachine
states:StartExecution
EventBridgeevents:PutEvents
SNSsns:Publish
Service Catalogservicecatalog:ListProvisioningArtifacts
servicecatalog:CreateProvisioningArtifact
servicecatalog:DescribeProvisioningArtifact
servicecatalog:DeleteProvisioningArtifact
servicecatalog:UpdateProduct
AppConfigappconfig:StartDeployment
appconfig:GetDeployment
appconfig:StopDeployment
Elastic Beanstalkelasticbeanstalk:CreateApplicationVersion
elasticbeanstalk:DescribeApplicationVersions
elasticbeanstalk:DescribeEnvironments
elasticbeanstalk:DescribeEvents
elasticbeanstalk:UpdateEnvironment
Secrets Managersecretsmanager:GetSecretValue
SSM Parameter Storessm:GetParameters
KMSkms:Encrypt
kms:Decrypt
GitHub (CodeStar Connections)codestar-connections:UseConnection
Bitbucket (CodeStar Connections)codestar-connections:UseConnection
※)権限設計の原則

信頼ポリシー:CodePipeline

  {
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "codepipeline.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

ログ・監視

ログ出力先ログの種類
CloudWatch Logsビルドログ
CloudWatch Logs優先の原則
標準メトリクス
メトリクス名説明
ActionExecutionFailureアクション失敗実行数
ActionExecutionSuccessアクション成功実行数
PipelineExecutionFailureパイプライン失敗実行数
PipelineExecutionSuccessパイプライン成功実行数

制限値(固定値/ハードリミット/ソフトリミット)

ハードリミット制限値
ステージ数/パイプライン50
アクション数/ステージ50
パイプラインあたりの合計アクション数500

ソフトリミット制限値
パイプライン数/リージョン300
Webhook数/リージョン300
カスタムアクション数/リージョン50

AWS CLIのサンプルコード

ステージプロバイダーprovider
SourceCodeCommitCodeCommit
SourceGitHub (v2)CodeStarSourceConnection
SourceS3S3
SourceECRECR
BuildCodeBuildCodeBuild
DeployCloudFormationCloudFormation
DeployECSECS
DeployECS (Blue/Green)CodeDeployToECS
DeployLambdaLambda
DeployS3(静的ウェブサイト)S3
DeployCodeDeploy(EC2/オンプレミス)CodeDeploy
Source ステージのプロバイダー
CodeCommit
{
  "actionTypeId": {
    "category": "Source",
    "owner": "AWS",
    "provider": "CodeCommit",
    "version": "1"
  },
  "configuration": {
    "RepositoryName": "my-repo",
    "BranchName": "main"
  }
}
GitHub (v2)
{
  "actionTypeId": {
    "category": "Source",
    "owner": "AWS",
    "provider": "CodeStarSourceConnection",
    "version": "1"
  },
  "configuration": {
    "ConnectionArn": "arn:aws:codestar-connections:region:account:connection/xxxxx",
    "FullRepositoryId": "owner/repo",
    "BranchName": "main"
  }
}
S3
{
  "actionTypeId": {
    "category": "Source",
    "owner": "AWS",
    "provider": "S3",
    "version": "1"
  },
  "configuration": {
    "S3Bucket": "my-bucket",
    "S3ObjectKey": "source.zip"
  }
}
ECR
{
  "actionTypeId": {
    "category": "Source",
    "owner": "AWS",
    "provider": "ECR",
    "version": "1"
  },
  "configuration": {
    "RepositoryName": "my-image",
    "ImageTag": "latest"
  }
}
Build ステージのプロバイダー
CodeBuild
{
  "actionTypeId": {
    "category": "Build",
    "owner": "AWS",
    "provider": "CodeBuild",
    "version": "1"
  },
  "configuration": {
    "ProjectName": "my-build-project"
  },
  "inputArtifacts": [{"name": "SourceOutput"}],
  "outputArtifacts": [{"name": "BuildOutput"}]
}
Deploy ステージのプロバイダー
CloudFormation
{
  "actionTypeId": {
    "category": "Deploy",
    "owner": "AWS",
    "provider": "CloudFormation",
    "version": "1"
  },
  "configuration": {
    "ActionMode": "CREATE_UPDATE",
    "StackName": "my-stack",
    "TemplatePath": "BuildOutput::template.yaml",
    "Capabilities": "CAPABILITY_IAM",
    "RoleArn": "arn:aws:iam::account:role/CFNRole"
  }
}
ECS
{
  "actionTypeId": {
    "category": "Deploy",
    "owner": "AWS",
    "provider": "ECS",
    "version": "1"
  },
  "configuration": {
    "ClusterName": "my-cluster",
    "ServiceName": "my-service",
    "FileName": "imagedefinitions.json"
  }
}
ECS (Blue/Green)
{
  "actionTypeId": {
    "category": "Deploy",
    "owner": "AWS",
    "provider": "CodeDeployToECS",
    "version": "1"
  },
  "configuration": {
    "ApplicationName": "my-app",
    "DeploymentGroupName": "my-deployment-group",
    "TaskDefinitionTemplateArtifact": "BuildOutput",
    "TaskDefinitionTemplatePath": "taskdef.json",
    "AppSpecTemplateArtifact": "BuildOutput",
    "AppSpecTemplatePath": "appspec.yaml"
  }
}
Lambda
{
  "actionTypeId": {
    "category": "Deploy",
    "owner": "AWS",
    "provider": "Lambda",
    "version": "1"
  },
  "configuration": {
    "FunctionName": "my-function",
    "UserParameters": "optional-parameters"
  }
}
S3
{
  "actionTypeId": {
    "category": "Deploy",
    "owner": "AWS",
    "provider": "S3",
    "version": "1"
  },
  "configuration": {
    "BucketName": "my-website-bucket",
    "Extract": "true"
  }
}
CodeDeploy
{
  "actionTypeId": {
    "category": "Deploy",
    "owner": "AWS",
    "provider": "CodeDeploy",
    "version": "1"
  },
  "configuration": {
    "ApplicationName": "my-app",
    "DeploymentGroupName": "my-deployment-group"
  }
}
パイプラインを作成する(パライプライン設定ファイル 指定)
pipeline-config.json
{
  "pipeline": {
    "name": "MyPipeline",
    "roleArn": "arn:aws:iam::123456789012:role/CodePipelineServiceRole",
    "artifactStore": {
      "type": "S3",
      "location": "my-pipeline-artifact-bucket"
    },
    "stages": [
      {
        "name": "Source",
        "actions": [
          {
            "name": "SourceAction",
            "actionTypeId": {
              "category": "Source",
              "owner": "AWS",
              "provider": "CodeCommit",
              "version": "1"
            },
            "configuration": {
              "RepositoryName": "my-repo",
              "BranchName": "main"
            },
            "outputArtifacts": [
              {
                "name": "SourceOutput"
              }
            ]
          }
        ]
      },
      {
        "name": "Build",
        "actions": [
          {
            "name": "BuildAction",
            "actionTypeId": {
              "category": "Build",
              "owner": "AWS",
              "provider": "CodeBuild",
              "version": "1"
            },
            "configuration": {
              "ProjectName": "my-build-project"
            },
            "inputArtifacts": [
              {
                "name": "SourceOutput"
              }
            ],
            "outputArtifacts": [
              {
                "name": "BuildOutput"
              }
            ]
          }
        ]
      },
      {
        "name": "Deploy",
        "actions": [
          {
            "name": "DeployAction",
            "actionTypeId": {
              "category": "Deploy",
              "owner": "AWS",
              "provider": "ECS",
              "version": "1"
            },
            "configuration": {
              "ClusterName": "my-ecs-cluster",
              "ServiceName": "my-service",
              "FileName": "imagedefinitions.json"
            },
            "inputArtifacts": [
              {
                "name": "BuildOutput"
              }
            ]
          }
        ]
      }
    ]
  }
}
aws codepipeline create-pipeline \
  --pipeline file://pipeline-config.json
パイプラインの一覧を表示する
aws codepipeline list-pipelines
パイプラインを表示する(パイプライン名 指定)
aws codepipeline get-pipeline \
  --name MyPipeline
パイプラインを削除する(パイプライン名 指定)
aws codepipeline delete-pipeline \
  --name MyPipeline

パイプラインを更新する(パライプライン設定ファイル 指定)
aws codepipeline update-pipeline \
  --pipeline file://updated-pipeline-config.json
パイプラインを実行する(パイプライン名 指定)
aws codepipeline start-pipeline-execution \
  --name MyPipeline
パイプライン実行状態を確認する(パイプライン名 指定)
aws codepipeline get-pipeline-state \
  --name MyPipeline

CloudFormationのサンプルコード

Terraformのサンプルコード

料金計算

課金項目説明
アクティブパイプライン月あたりのアクティブなパイプライン数
料金計算ツール

公式ページ

AWSドキュメント CodePipeline