0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[AWS CDK]aws_cloudfront.Distributionで設定されるOAIを削除する

Last updated at Posted at 2024-05-06

CDKでCloudFrontのオリジンにS3を指定するとデフォルトのアクセス制御にはOAIが設定され、S3にOAIのバケットポリシーが追加されます。
OAIは既にレガシーであり、実際のアクセス制御にはより新しいOACを使うため、デフォルトで設定されるOAIを削除する方法を調べました。

結論

  • CloudFront側
    addPropertyOverrideでOAIの設定を空にする。
const cfnDistribution = hogeDistribution.node.defaultChild as aws_cloudfront.CfnDistribution;
cfnDistribution.addPropertyOverride(
  "DistributionConfig.Origins.0.S3OriginConfig.OriginAccessIdentity",
  ""
);
  • S3側
    addPropertyDeletionOverrideでS3のバケットポリシーからOAIに対応する箇所を削除する。
const policyOverride = hogeBucket.node.findChild("Policy").node.defaultChild as CfnBucket;
policyOverride.addPropertyDeletionOverride("PolicyDocument.Statement.0");

CDK全体

import {
  Stack,
  StackProps,
  aws_s3,
  aws_iam,
  aws_cloudfront,
  aws_cloudfront_origins,
  aws_s3_deployment
} from 'aws-cdk-lib';
import { CfnBucket } from 'aws-cdk-lib/aws-s3';
import { Construct } from 'constructs';

export class HelloCdkStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);

    // S3 Bucket作成
    const hogeBucket = new aws_s3.Bucket(this, "hogeBucket", {
      bucketName: `hoge-${this.account}-${this.region}`,
      publicReadAccess: false,
      blockPublicAccess: aws_s3.BlockPublicAccess.BLOCK_ALL,
    });

    // S3ファイルアップロード(index.html)
    new aws_s3_deployment.BucketDeployment(this, "FileUploadS3", {
      sources: [aws_s3_deployment.Source.asset("upload_test")],
      destinationBucket: hogeBucket
    });

    // CloudFront ディストリビューション作成
    const hogeDistribution = new aws_cloudfront.Distribution(this, "hogeDistribution", {
      defaultRootObject: "index.html",
      defaultBehavior: {
        origin: new aws_cloudfront_origins.S3Origin(hogeBucket)
      },
    });

    // S3 OAC作成
    const cfnS3Oac = new aws_cloudfront.CfnOriginAccessControl(this, "S3_OAC", {
      originAccessControlConfig: {
        name: "OAC for S3",
        originAccessControlOriginType: "s3",
        signingBehavior: "always",
        signingProtocol: "sigv4"
      }
    });

    // CloudFront OAI削除(S3)
    const cfnDistribution = hogeDistribution.node.defaultChild as aws_cloudfront.CfnDistribution;
    cfnDistribution.addPropertyOverride(
      "DistributionConfig.Origins.0.S3OriginConfig.OriginAccessIdentity",
      ""
    );

    // CloudFront OAC追加(S3)
    cfnDistribution.addPropertyOverride(
      "DistributionConfig.Origins.0.OriginAccessControlId",
      cfnS3Oac.attrId
    );

    // バケットポリシーにOACを追加
    hogeBucket.addToResourcePolicy(new aws_iam.PolicyStatement({
      actions: ['s3:GetObject'],
      effect: aws_iam.Effect.ALLOW,
      resources: [hogeBucket.arnForObjects("*")],
      principals: [new aws_iam.ServicePrincipal("cloudfront.amazonaws.com")],
      conditions: {
        StringEquals: {
          "AWS:SourceArn": `arn:aws:cloudfront::${this.account}:distribution/${hogeDistribution.distributionId}`
        }
      }
    }));

    // バケットポリシーからOAIを削除
    const policyOverride = hogeBucket.node.findChild("Policy").node.defaultChild as CfnBucket;
    policyOverride.addPropertyDeletionOverride("PolicyDocument.Statement.0");
  }
}

バケットポリシーの確認

  • addPropertyDeletionOverride追加前
    image.png

  • addPropertyDeletionOverride追加後
    OAIのバケットポリシーが削除されました。
    image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?