CloudFormationでデプロイしたCloudFrontディストリビューションにデフォルトルートオブジェクトを設定する方法

かなりニッチな記事ですが、CloudFormationでCloudFrontディストリビューションにデフォルトのルートオブジェクトを設定してみました。
2023.06.25

こんにちは、AWS事業本部@福岡オフィスのべこみん(@beco_minn)です。

みなさん、Amazon CloudFrontは使ってますか?

今回はAWS Security Hubのコントロール、 AWS Foundational Security Best Practices(FSBP) CloudFront.1「CloudFront ディストリビューションでは、デフォルトのルートオブジェクトが設定されている必要があります」 の是正をCloudFormationで行う方法をご紹介します。

上記コントロールとその是正方法については下記記事をご参照ください。

対象読者

  • CloudFormationでデプロイしたCloudFrontディストリビューションに対して、AWS FSBP CloudFront.1の是正を行いたい人

CloudFormationテンプレートの変更点

例えば、下記のようなyamlファイルでCloudFrontディストリビューションをデプロイしているとします。

AWSTemplateFormatVersion: '2010-09-09'

Description: Sample Stack to create a CloudFront distribution

Parameters:
  S3BucketName:
    Type: String
    Description: The name of the existing S3 bucket
  ExistingOriginAccessControl:
    Type: String
    Description: The ID of the existing Origin Access Control

Resources:
  MyCloudFrontDistribution:
    Type: AWS::CloudFront::Distribution
    Properties:
      DistributionConfig:
        Enabled: true
        Comment: "My CloudFront Distribution"
        Origins:
          - DomainName: !Sub "${S3BucketName}.s3.amazonaws.com"
            Id: S3
            OriginAccessControlId: !Sub "${ExistingOriginAccessControl}"
            S3OriginConfig: 
              OriginAccessIdentity: ''
        DefaultCacheBehavior:
          TargetOriginId: S3
          ViewerProtocolPolicy: https-only
          ForwardedValues:
            QueryString: false
            Cookies:
              Forward: none
        ViewerCertificate:
          CloudFrontDefaultCertificate: true

デフォルトルートオブジェクトを設定するためには、DistributionConfigのプロパティ DefaultRootObject を下記のように設定します。

この際、値には index.html のようにルートオブジェクトとしたいオブジェクトの名称(拡張子含む)のみを指定して下さい。オブジェクト名の前に / は不要です。

AWSTemplateFormatVersion: '2010-09-09'

Description: Sample Stack to create a CloudFront distribution

Parameters:
  S3BucketName:
    Type: String
    Description: The name of the existing S3 bucket
  ExistingOriginAccessControl:
    Type: String
    Description: The ID of the existing Origin Access Control

Resources:
  MyCloudFrontDistribution:
    Type: AWS::CloudFront::Distribution
    Properties:
      DistributionConfig:
        Enabled: true
        Comment: "My CloudFront Distribution"
        Origins:
          - DomainName: !Sub "${S3BucketName}.s3.amazonaws.com"
            Id: S3
            OriginAccessControlId: !Sub "${ExistingOriginAccessControl}"
            S3OriginConfig:
              OriginAccessIdentity: ''
        DefaultCacheBehavior:
          TargetOriginId: S3
          ViewerProtocolPolicy: https-only
          ForwardedValues:
            QueryString: false
            Cookies:
              Forward: none
        ViewerCertificate:
          CloudFrontDefaultCertificate: true
        DefaultRootObject: index.html

公式ドキュメントは下記をご参照ください

上記をupdate stackすれば、是正は完了です。

コンソールからデフォルトルートオブジェクトが設定されていることを確認しましょう。

参考

最後に

適切なデフォルトルートオブジェクトを設定し、意図していないコンテンツの公開を防ぎましょう。

かなりニッチな記事となりましたが、本記事がどなたかのお役に立てれば幸いです。

以上、べこみんでした。