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 Risposta
2
Risposta accettata

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
ESPERTO
con risposta 2 mesi fa
profile picture
ESPERTO
verificato 5 giorni fa
profile picture
ESPERTO
verificato 2 mesi fa

Accesso non effettuato. Accedi per postare una risposta.

Una buona risposta soddisfa chiaramente la domanda, fornisce un feedback costruttivo e incoraggia la crescita professionale del richiedente.

Linee guida per rispondere alle domande