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 Resposta
2
Resposta aceita

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
ESPECIALISTA
respondido há 2 meses
profile picture
ESPECIALISTA
avaliado há 4 dias
profile picture
ESPECIALISTA
avaliado há 2 meses

Você não está conectado. Fazer login para postar uma resposta.

Uma boa resposta responde claramente à pergunta, dá feedback construtivo e incentiva o crescimento profissional de quem perguntou.

Diretrizes para responder a perguntas