[アップデート] AWS CodePipeline の Command アクションで Secret Manager を用いた環境変数の登録/Windows OS/コンピュートタイプの増強がサポートされました

[アップデート] AWS CodePipeline の Command アクションで Secret Manager を用いた環境変数の登録/Windows OS/コンピュートタイプの増強がサポートされました

Clock Icon2025.05.09

こんにちは!クラウド事業本部コンサルティング部のたかくに(@takakuni_)です。

AWS CodePipeline の Command アクションで Secret Manager を用いた環境変数の登録/Windows OS/コンピュートタイプの増強がサポートされました。

https://aws.amazon.com/jp/about-aws/whats-new/2025/05/aws-codepipeline-secrets-manager-configurations-commands-action/

AWS CodePipeline の Command

AWS CodePipeline の Command アクションはコマンドラインが実行可能なアクションタイプです。

2024 年 10 月にリリースされた比較的新しいアクションタイプです。

Command アクションでは自身で CodeBuild を用意しなくとも(AWS 管理の CodeBuild で)、コマンドラインを実行可能なため、パイプラインを非常にシンプルな作りにできます。

image.png
[アップデート] AWS CodePipeline で CodeBuild セットアップなしでコマンド実行が出来る新しいビルドアクション「Commands」を使ってみた より画像引用

https://dev.classmethod.jp/articles/codepipeline-action-commands/

アップデート内容

今回のアップデートはいくつかあるため、1つずつ取り上げます。

Secrets Manager を介した環境変数の登録

アップデート前まで Command アクションで、Secrets Manager を介した環境変数をサポートしておらず、以下の対応が必要でした。

  • Command アクションではなく大人しく CodeBuild を使う
    • CodeBuild プロジェクトでは Secret Manager を介した環境変数を登録可能
  • Command アクション自体のシェルの中で環境変数の登録を行う

シンプルですが、今回のアップデートで Command でもシークレットを参照して環境変数へネイティブに登録できるようになりました。

type
Specifies the type of use for the environment variable value. The value can be either PLAINTEXT or SECRETS_MANAGER. If the value is SECRETS_MANAGER, provide the Secrets reference in the EnvironmentVariable value.
Type: String
Valid Values: PLAINTEXT | SECRETS_MANAGER
Required: No

https://docs.aws.amazon.com/codepipeline/latest/APIReference/API_EnvironmentVariable.html

CodeBuild プロジェクトとは異なり、引き続き Parameter Store を介した環境変数の登録はサポートされていませんので注意しておきましょう。

Windows OS

今まで Linux OS のみをサポートしていたのですが、Windows OS もサポートとなりました。パラメーター名から見るに、バージョンは Windows Server 2022 のようです。OS を指定しない場合は Linux OS の Small が利用されるようです。

EnvironmentType
Required: No
The OS image for the build environment that supports the Commands action. > The following are valid values for build environments:

  • LINUX_CONTAINER
  • WINDOWS_SERVER_2022_CONTAINER
    The selection for EnvironmentType will then allow the compute type for that OS in the ComputeType field. For more information about the CodeBuild compute types available for this action, see the Build environment compute modes and types reference in the CodeBuild User Guide.

Command アクションは CodeBuild の料金が別途発生します。

コマンドアクションを実行すると、AWS CodeBuild で別途料金が発生します。

https://docs.aws.amazon.com/codepipeline/latest/userguide/action-reference-Commands.html

そのため、料金表は以下を参照すると良いと思います。

https://aws.amazon.com/codebuild/pricing/?nc1=h_ls

コンピュートタイプの増強

今まで Command ではコンピュートタイプ(スペック)の選択ができなかったのですが、今回新たに以下の3種類から選べるようになりました。繰り返しになりますが、デフォルトは Linux OS の BUILD_GENERAL1_SMALL が利用されます。

  • BUILD_GENERAL1_SMALL
  • BUILD_GENERAL1_MEDIUM
  • BUILD_GENERAL1_LARGE

ただし、Windows OS には BUILD_GENERAL1_SMALL が適用できないなどの組み合わせを意識する必要があるようです。

Some compute types are not compatible with certain environment types. For example, WINDOWS_SERVER_2022_CONTAINER is not compatible with BUILD_GENERAL1_SMALL. Using incompatible combinations causes the action to fail and generates a runtime error.

https://docs.aws.amazon.com/codepipeline/latest/userguide/action-reference-Commands.html#action-reference-Commands-config

利用できない組み合わせですが CodeBuild の価格表から、現状はドキュメントにある Windows OS と BUILD_GENERAL1_SMALL のみなのかなと思います。(今後、GPU や ARM の場合に増えてくるのかなと思いました。)

2025-05-09 at 18.10.50-Managed Build Server - AWS CodeBuild Pricing - AWS.png

https://aws.amazon.com/codebuild/pricing/?nc1=h_ls

やってみる

それでは今回は Linux OS で Secrets Manager を利用して環境変数を登録してみます。

IAM ロールの作成

IAM を作成します。信頼ポリシーは以下で設定します。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "CodePipelineTrustPolicy",
            "Effect": "Allow",
            "Principal": {
                "Service": "codepipeline.amazonaws.com"
            },
            "Action": "sts:AssumeRole",
            "Condition": {
                "StringEquals": {
                    "aws:SourceAccount": "123456789012"
                }
            }
        }
    ]
}

IAM ポリシーは以下を設定しました。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "AllowS3BucketAccess",
            "Effect": "Allow",
            "Action": [
                "s3:GetBucketVersioning",
                "s3:GetBucketAcl",
                "s3:GetBucketLocation"
            ],
            "Resource": [
                "バケット名"
            ],
            "Condition": {
                "StringEquals": {
                    "aws:ResourceAccount": "622809842341"
                }
            }
        },
        {
            "Sid": "AllowS3ObjectAccess",
            "Effect": "Allow",
            "Action": [
                "s3:PutObject",
                "s3:PutObjectAcl",
                "s3:GetObject",
                "s3:GetObjectVersion"
            ],
            "Resource": [
                "バケットARN/*"
            ],
            "Condition": {
                "StringEquals": {
                    "aws:ResourceAccount": "622809842341"
                }
            }
        }
    ]
}
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "codeconnections:UseConnection",
                "codestar-connections:UseConnection"
            ],
            "Resource": [
                "arn:aws:codestar-connections:*:622809842341:connection/Connection ID",
                "arn:aws:codeconnections:*:622809842341:connection/Connection ID"
            ]
        }
    ]
}
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:PutLogEvents"
            ],
            "Resource": [
                "arn:aws:logs:ap-northeast-1:622809842341:log-group:/aws/codepipeline/*",
                "arn:aws:logs:ap-northeast-1:622809842341:log-group:/aws/codepipeline/codepipeline-update:log-stream:*"
            ]
        }
    ]
}

Secrets Manager 用に IAM ポリシーとして SecretsManagerReadWrite も追加しておきましょう。

シークレットの登録

Command アクションで参照するシークレットを作成します。適当なプレーンテキストを登録しました。

2025-05-09 at 18.20.01-新しいシークレットを保存する  Secrets Manager  ap-northeast-1.png

名前は command-secret としました。

2025-05-09 at 18.20.28-新しいシークレットを保存する  Secrets Manager  ap-northeast-1.png

Pipeline の作成

Pipeline を作成します。先ほど作成した IAM ロールを使用しながらパイプラインを作成します。

2025-05-09 at 20.45.14-新規のパイプラインを作成する  CodePipeline  ap-northeast-1@2x.png

ソースプロバイダーには GitHub を指定します。検証用のため、ブランクのリポジトリを指定しました。

2025-05-09 at 18.16.59-新規のパイプラインを作成する  CodePipeline  ap-northeast-1.png

printenv コマンドを利用する Command アクションを指定します。 環境変数に Secrets Manager を指定できるようになっていますね!

2025-05-09 at 20.48.29-新規のパイプラインを作成する  CodePipeline  ap-northeast-1@2x.png

コンピュートタイプも SMALL 以上が選択できるようになっています。

2025-05-09 at 20.48.35-新規のパイプラインを作成する  CodePipeline  ap-northeast-1@2x.png

Windows OS の場合、利用できないコンピュートタイプは、マネジメントコンソールで非表示になっていました。(優しいですね)

2025-05-09 at 20.48.41-新規のパイプラインを作成する  CodePipeline  ap-northeast-1@2x.png

パイプラインを作成すると、実行され始めました。

2025-05-09 at 20.53.25-codepipeline-update  CodePipeline  ap-northeast-1@2x.png

実行結果を確認するとログからはマスクされた状態で出力されていました。うっかり出力されておらず、素晴らしいですね。

2025-05-09 at 20.54.23-codepipeline-update  CodePipeline  ap-northeast-1@2x.png

まとめ

以上、「AWS CodePipeline の Command アクションで Secret Manager を用いた環境変数の登録/Windows OS/コンピュートタイプの増強がサポートされました。」でした。

個人的に CodePipeline V2 の目玉は Commands だと思うのですが、どんどん便利になっていますね。

このブログがどなたかの参考になれば幸いです。クラウド事業本部コンサルティング部のたかくに(@takakuni_)でした!

Share this article

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.