Static website hosting feature in S3 bucket using CDK

0

I am using AWS CDK to provision an S3 bucket and host an static website. Code below was already deployed successfully by running cdk deploy command.

    const myBucket = new s3.Bucket(this, 'MyBucket', {
      bucketName: 'my-bucket-name',
      publicReadAccess: true,
      blockPublicAccess: {
        blockPublicAcls: false,
        blockPublicPolicy: false,
        ignorePublicAcls: false,
        restrictPublicBuckets: false,
      },
      removalPolicy: cdk.RemovalPolicy.DESTROY,
    });

    new s3Deploy.BucketDeployment(this, 'BucketDeploymentId', {
      sources: [s3Deploy.Source.asset("./src/website")],
      destinationBucket: myBucket,
    });

However, if I take a look to the bucket from Management Console "Static website hosting" seems to be disabled, as shown in the following screenshot

Enter image description here

Is there any missing property or configuration I need to add to make my website available?

1回答
2
承認された回答

Hello.

I think you need to add "websiteIndexDocument".
https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_s3.Bucket.html

The name of the index document (e.g. "index.html") for the website. Enables static website hosting for this bucket.

So I think the code should be as follows.

    const myBucket = new s3.Bucket(this, 'MyBucket', {
      bucketName: 'my-bucket-name',
      publicReadAccess: true,
      blockPublicAccess: {
        blockPublicAcls: false,
        blockPublicPolicy: false,
        ignorePublicAcls: false,
        restrictPublicBuckets: false,
      },
      removalPolicy: cdk.RemovalPolicy.DESTROY,
      websiteIndexDocument: 'index.html'
    });

    new s3Deploy.BucketDeployment(this, 'BucketDeploymentId', {
      sources: [s3Deploy.Source.asset("./src/website")],
      destinationBucket: myBucket,
    });
profile picture
エキスパート
回答済み 2ヶ月前
profile picture
エキスパート
レビュー済み 5日前
profile picture
エキスパート
レビュー済み 2ヶ月前

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

質問に答えるためのガイドライン

関連するコンテンツ