Security Hub コントロールに準拠した S3 バケットの作成と CloudFormation テンプレートの S3 バケットへのアップロードを AWS CDK であらかじめ実装する

Security Hub コントロールに準拠した S3 バケットの作成と CloudFormation テンプレートの S3 バケットへのアップロードを AWS CDK であらかじめ実装する

Clock Icon2025.06.12

こんにちは、製造ビジネステクノロジー部の若槻です。

AWS CloudFormation では、マネジメントコンソールからテンプレートを指定してスタックを作成することができます。IAM ユーザーなど IaC での管理に適していない AWS リソースをコードを編集することなく管理したい場合に便利です。
https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/template-guide.html#where-they-get-stored

さて、テンプレート指定によるスタック作成にはいくつか方法があるのですが、そのうちテンプレートを直接アップロードする方法の場合は初回に cf-templates-〜 というバケットが自動作成され、以降はそのバケットがテンプレート置き場として利用されるようになります。それはそれで一見便利そうなのですが、その際に自動作成される S3 バケットは各種 Security Hub コントロールに準拠していないため、セキュリティ上の懸念があったり、不必要なセキュリティアラートが発生してしまうことがあります。

そこで今回は、Security Hub コントロールに準拠した S3 バケットの作成と CloudFormation テンプレートの S3 バケットへのアップロードを AWS CDK であらかじめ実装する方法をご紹介します。

やってみた

例として、下記のような IAM ユーザーを作成する CloudFormation テンプレートを使用してみます。頻繁な開発メンバー追加に対応するためにこのようなテンプレートを使われている方も多いのではないでしょうか。

cloudformation-templates/iam-user-creation.yaml
AWSTemplateFormatVersion: "2010-09-09"
Description: "CloudFormation template to create an IAM user"

Parameters:
  UserName:
    Type: String
    Description: Name for the new IAM user
    MinLength: 1
    MaxLength: 64
    AllowedPattern: '^[\w+=,.@-]+$'
    ConstraintDescription: "User name can include alphanumeric characters and these special characters: +=,.@-_"

Resources:
  IAMUser:
    Type: AWS::IAM::User
    Properties:
      UserName: !Ref UserName

AWS CDK のコード実装です。CloudFormation テンプレートを S3 バケットにデプロイし、テンプレートの URL を出力します。S3 バケットも CDK で作成するので、Security Hub コントロールに準拠した設定を行うことができています。

lib/main-stack.ts
import * as cdk from "aws-cdk-lib";
import * as s3 from "aws-cdk-lib/aws-s3";
import * as s3_deployment from "aws-cdk-lib/aws-s3-deployment";
import { Construct } from "constructs";

export class MainStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props: cdk.StackProps) {
    super(scope, id, props);

    /**
     * テンプレート保管用 S3 バケットの作成
     */
    const bucket = new s3.Bucket(this, "CfTemplatesBucket", {
      /**
       * Security Hub コントロール準拠のための設定
       */
      versioned: true, // Security Hub コントロール [S3.10] 対応
      enforceSSL: true, // Security Hub コントロール [S3.5] 対応
      // serverAccessLogsBucket: ...略 // Security Hub コントロール [S3.9] 対応

      /**
       * テンプレートファイル自体はコードで管理するため、スタック削除時にバケットも削除する
       */
      removalPolicy: cdk.RemovalPolicy.DESTROY,
      autoDeleteObjects: true,
    });

    /**
     * S3 バケットへのテンプレートファイルのデプロイ
     */
    new s3_deployment.BucketDeployment(this, "BucketDeploy", {
      sources: [s3_deployment.Source.asset("cloudformation-templates")],
      destinationBucket: bucket,
    });

    /**
     * テンプレートファイルの URL(https://〜)を出力
     */
    new cdk.CfnOutput(this, "CfTemplatesBucketUrl", {
      value: `${bucket.bucketWebsiteUrl}/iam-user-creation.yaml`,
      description: "IAM User Creation CloudFormation Template S3 URL",
    });
  }
}

デプロイすると、CDK スタックでバケットを作成することができました。

動作確認

先ほど作成した CDK スタックの出力から CloudFormation テンプレートの URL を取得します。

AWS マネジメントコンソールの CloudFormation からテンプレートを指定してスタックを作成します。(S3 URL と書いてあるが s3:// ではなく https:// の形式での必要がある点に注意)

テンプレートから Cloudformation スタックおよび IAM ユーザーを作成することができました。

注意点

今回テンプレートファイルをバケットにデプロイするために使用した BucketDeployment コンストラクトですが、Source.asset メソッドで指定するファイルは .zip 形式のファイルかディレクトリである必要があります。例えば、下記のよ縫いテンプレートファイルを直接指定してみます。

/**
 * S3 バケットへのテンプレートファイルのデプロイ
 */
new s3_deployment.BucketDeployment(this, "BucketDeploy", {
  sources: [
    s3_deployment.Source.asset(
      path.join(__dirname, "./sample-cloudformation-templates.yaml") // テンプレートファイルを直接指定した場合
    ),
  ],
  destinationBucket: bucket,
});

デプロイ時に次のようなエラーになります。

npm run deploy

> cdk_sample_app@0.1.0 deploy
> cdk deploy --require-approval never --method=direct

/Users/wakatsuki.ryuta/projects/cm-rwakatsuki/cdk_sample_app/lib/main-stack.ts:32
    new s3_deployment.BucketDeployment(this, "BucketDeploy", {
    ^
ValidationError: Asset path must be either a .zip file or a directory
    at path [Main/BucketDeploy] in aws-cdk-lib.aws_s3_deployment.BucketDeployment

おわりに

Security Hub コントロールに準拠した S3 バケットの作成と CloudFormation テンプレートの S3 バケットへのアップロードを AWS CDK であらかじめ実装する方法をご紹介しました。

どなたかの参考になれば幸いです。

以上

Share this article

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.