📝
CodePipeline から ALB 配下の EC2 へのデプロイ時の挙動を確認してみた
Tutorial: Deploy to Amazon EC2 instances with CodePipeline - AWS CodePipeline
以下のデプロイ動作でした。
- 1 台目の EC2 インスタンスをターゲットグループからドレイン
- 1 台目の EC2 インスタンスにデプロイ
- 1 台目の EC2 インスタンスをターゲットグループに登録
- 2 台目の EC2 インスタンスをターゲットグループからドレイン
- 2 台目の EC2 インスタンスにデプロイ
- 2 台目の EC2 インスタンスをターゲットグループに登録
EC2 インスタンスの設定
Amazon Linux 2023 の EC2 インスタンスを 2 台起動して index.html を配置しました。
$ sudo dnf install -y nginx
$ sudo systemctl enable nginx
$ sudo systemctl start nginx
$ echo "<h1>Hello from Amazon Linux 2023</h1>" | sudo tee /usr/share/nginx/html/index.html
EC2 インスタンスにアクセスすると以下のページが表示されます。

ALB の作成
ALB およびターゲットグループを作成し、上記 EC2 インスタンスを登録しました。

CodeCommit リポジトリの作成
Tutorial: Deploy to Amazon EC2 instances with CodePipeline - AWS CodePipeline
上記ドキュメントに記載の script.sh をプッシュしておきました。

CodePipeline パイプラインの作成
Tutorial: Deploy to Amazon EC2 instances with CodePipeline - AWS CodePipeline
基本的には上記ドキュメント通りに作成ですが、ターゲットグループの指定を追加しました。

パイプライン作成直後のデプロイは権限不足で失敗します。
User: arn:aws:sts::012345678901:assumed-role/AWSCodePipelineServiceRole-ap-northeast-1-dev/1750331940072 is not authorized to perform: logs:PutLogEvents on resource: arn:aws:logs:ap-northeast-1:012345678901:log-group:/aws/codepipeline/dev:log-stream:5e0f2bfc-33ac-4855-b5c8-079a7766d9c6 because no identity-based policy allows the logs:PutLogEvents action (Service: AWSLogs; Status Code: 400; Error Code: AccessDeniedException; Request ID: e01b55c8-a08e-445a-b9d8-47e7696e2e75; Proxy: null)
上記デプロイ失敗は想定内なので、CodePipeline の実行ロールに AdministratorAccess 権限を付与しました。
再デプロイ
アクセス権限付与後、再度デプロイすると冒頭のフローでデプロイされることを確認できます。
以下、デプロイログです。
[2025/06/19 11:40:25.399] Describing tag, tag key = Name. tag value = dev.
[2025/06/19 11:40:25.520] Found 2 instances: i-00c6c88c3f5881cf5, i-0f72f0bf944a29836
[2025/06/19 11:40:25.657] Processing deploy event BLOCK_TRAFFIC on instances: i-00c6c88c3f5881cf5
[2025/06/19 11:40:57.395] Target group arn:aws:elasticloadbalancing:ap-northeast-1:172454386805:targetgroup/dev/b2e67ff85b4756b9 is unused for instance i-00c6c88c3f5881cf5.
[2025/06/19 11:40:57.454] Deploy event BLOCK_TRAFFIC succeeded on instances: i-00c6c88c3f5881cf5.
[2025/06/19 11:40:57.478] Processing deploy event DOWNLOAD on instances: i-00c6c88c3f5881cf5
[2025/06/19 11:40:57.596] Executing commands on instances i-00c6c88c3f5881cf5, SSM command id e5c7ca5c-f5fe-47ad-ad08-edcb6d3f85fe, commands:
mkdir -p /opt/codepipeline
if [ $(uname -m | grep arm) ]; then export DE_URL=[REDACTED]; else export DE_URL=[REDACTED]; fi
curl "$DE_URL" -o /opt/codepipeline/deploy || wget -O /opt/codepipeline/deploy "$DE_URL"
chmod +x /opt/codepipeline/deploy
/opt/codepipeline/deploy -event Download -executionId 92196d27-728a-4cb8-9e21-4e299d177f8b -bucket codepipeline-ap-northeast-1-bdaedb2f13d9-48eb-88a8-c6bd227c173a -key dev/SourceArti/rz13cr3 -accountId 172454386805
[2025/06/19 11:41:29.333] Deploy event DOWNLOAD succeeded on instances: i-00c6c88c3f5881cf5.
[2025/06/19 11:41:29.407] Processing deploy event BEFORE_DEPLOY on instances: i-00c6c88c3f5881cf5
[2025/06/19 11:41:29.430] Skipping deploy event BEFORE_DEPLOY on instances: i-00c6c88c3f5881cf5 as not specified in action configuration
[2025/06/19 11:41:29.451] Processing deploy event DEPLOY on instances: i-00c6c88c3f5881cf5
[2025/06/19 11:41:29.565] Executing commands on instances i-00c6c88c3f5881cf5, SSM command id f03bacc9-2e7c-44fa-8a22-b47ef870d1a0, commands:
/opt/codepipeline/deploy -event Deploy -executionId 92196d27-728a-4cb8-9e21-4e299d177f8b -destination /home/ec2-user/testhelloworld -deploySpec ""
[2025/06/19 11:42:00.392] Deploy event DEPLOY succeeded on instances: i-00c6c88c3f5881cf5.
[2025/06/19 11:42:00.444] Processing deploy event AFTER_DEPLOY on instances: i-00c6c88c3f5881cf5
[2025/06/19 11:42:00.557] Executing commands on instances i-00c6c88c3f5881cf5, SSM command id 1a9cd8e5-eea7-4ced-a882-96029deed454, commands:
/opt/codepipeline/deploy -event AfterDeploy -executionId 92196d27-728a-4cb8-9e21-4e299d177f8b -destination /home/ec2-user/testhelloworld -script test/script.sh -deploySpec ""
[2025/06/19 11:42:31.382] Deploy event AFTER_DEPLOY succeeded on instances: i-00c6c88c3f5881cf5.
[2025/06/19 11:42:31.446] Processing deploy event UNBLOCK_TRAFFIC on instances: i-00c6c88c3f5881cf5
[2025/06/19 11:43:02.630] Target group arn:aws:elasticloadbalancing:ap-northeast-1:172454386805:targetgroup/dev/b2e67ff85b4756b9 is healthy for instance i-00c6c88c3f5881cf5.
[2025/06/19 11:43:02.680] Deploy event UNBLOCK_TRAFFIC succeeded on instances: i-00c6c88c3f5881cf5.
[2025/06/19 11:43:02.715] Total instances 2, pending instances 1, in progress instances 0.
[2025/06/19 11:43:02.733] Processing deploy event BLOCK_TRAFFIC on instances: i-0f72f0bf944a29836
[2025/06/19 11:43:35.135] Target group arn:aws:elasticloadbalancing:ap-northeast-1:172454386805:targetgroup/dev/b2e67ff85b4756b9 is unused for instance i-0f72f0bf944a29836.
[2025/06/19 11:43:35.195] Deploy event BLOCK_TRAFFIC succeeded on instances: i-0f72f0bf944a29836.
[2025/06/19 11:43:35.215] Processing deploy event DOWNLOAD on instances: i-0f72f0bf944a29836
[2025/06/19 11:43:35.327] Executing commands on instances i-0f72f0bf944a29836, SSM command id b8693ac4-9cdf-4b28-b26a-d9f98cd36731, commands:
mkdir -p /opt/codepipeline
if [ $(uname -m | grep arm) ]; then export DE_URL=[REDACTED]; else export DE_URL=[REDACTED]; fi
curl "$DE_URL" -o /opt/codepipeline/deploy || wget -O /opt/codepipeline/deploy "$DE_URL"
chmod +x /opt/codepipeline/deploy
/opt/codepipeline/deploy -event Download -executionId 92196d27-728a-4cb8-9e21-4e299d177f8b -bucket codepipeline-ap-northeast-1-bdaedb2f13d9-48eb-88a8-c6bd227c173a -key dev/SourceArti/rz13cr3 -accountId 172454386805
[2025/06/19 11:43:35.354] Total instances 2, pending instances 0, in progress instances 1.
[2025/06/19 11:44:06.177] Deploy event DOWNLOAD succeeded on instances: i-0f72f0bf944a29836.
[2025/06/19 11:44:06.231] Processing deploy event BEFORE_DEPLOY on instances: i-0f72f0bf944a29836
[2025/06/19 11:44:06.251] Skipping deploy event BEFORE_DEPLOY on instances: i-0f72f0bf944a29836 as not specified in action configuration
[2025/06/19 11:44:06.271] Processing deploy event DEPLOY on instances: i-0f72f0bf944a29836
[2025/06/19 11:44:06.389] Executing commands on instances i-0f72f0bf944a29836, SSM command id a63d88af-4e80-4f32-bc90-d5ee60c110d3, commands:
/opt/codepipeline/deploy -event Deploy -executionId 92196d27-728a-4cb8-9e21-4e299d177f8b -destination /home/ec2-user/testhelloworld -deploySpec ""
[2025/06/19 11:44:06.418] Total instances 2, pending instances 0, in progress instances 1.
[2025/06/19 11:44:37.311] Deploy event DEPLOY succeeded on instances: i-0f72f0bf944a29836.
[2025/06/19 11:44:37.379] Processing deploy event AFTER_DEPLOY on instances: i-0f72f0bf944a29836
[2025/06/19 11:44:37.509] Executing commands on instances i-0f72f0bf944a29836, SSM command id fdd560ec-dbdc-49ad-bfae-672f5e1e89ed, commands:
/opt/codepipeline/deploy -event AfterDeploy -executionId 92196d27-728a-4cb8-9e21-4e299d177f8b -destination /home/ec2-user/testhelloworld -script test/script.sh -deploySpec ""
[2025/06/19 11:44:37.538] Total instances 2, pending instances 0, in progress instances 1.
[2025/06/19 11:45:10.083] Deploy event AFTER_DEPLOY succeeded on instances: i-0f72f0bf944a29836.
[2025/06/19 11:45:10.137] Processing deploy event UNBLOCK_TRAFFIC on instances: i-0f72f0bf944a29836
[2025/06/19 11:45:10.423] Total instances 2, pending instances 0, in progress instances 1.
[2025/06/19 11:45:41.247] Target group arn:aws:elasticloadbalancing:ap-northeast-1:172454386805:targetgroup/dev/b2e67ff85b4756b9 is healthy for instance i-0f72f0bf944a29836.
[2025/06/19 11:45:41.305] Deploy event UNBLOCK_TRAFFIC succeeded on instances: i-0f72f0bf944a29836.
[2025/06/19 11:45:41.409] Describing tag, tag key = Name. tag value = dev.
[2025/06/19 11:45:41.644] Found 0 more instances:
[2025/06/19 11:45:41.692] Deployment SUCCEEDED
ターゲットグループのコンソールからも一連の流れを確認できました。






まとめ
今回は CodePipeline から ALB 配下の EC2 へのデプロイ時の挙動を確認してみました。
どなたかの参考になれば幸いです。
Discussion