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달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠