adam bien's blog

Infrastructure as Code: Provisioning a AWS S3 Website With Pulumi and Java 📎

To provision a public S3 bucket for a static website with pulumi.com and Java 17:

import com.pulumi.Pulumi;
import com.pulumi.asset.FileAsset;
import com.pulumi.aws.s3.BucketObjectv2;
import com.pulumi.aws.s3.BucketObjectv2Args;
import com.pulumi.aws.s3.BucketV2;
import com.pulumi.aws.s3.BucketV2Args;
import com.pulumi.aws.s3.BucketWebsiteConfigurationV2;
import com.pulumi.aws.s3.BucketWebsiteConfigurationV2Args;
import com.pulumi.aws.s3.inputs.BucketWebsiteConfigurationV2IndexDocumentArgs;

public static void main(String[] args) {
    Pulumi.run(ctx -> {

...a generic bucket is created first:

    
    var bucket = new BucketV2("Bucket", BucketV2Args.builder()
            .bucket("pulumi.duke")
            .forceDestroy(true)
            .build());

...then configured as website. The name of the index.html is configured as well:

    var configuration = new BucketWebsiteConfigurationV2("Configuration",
            BucketWebsiteConfigurationV2Args.builder()
                    .bucket(bucket.getId())
                    .indexDocument(BucketWebsiteConfigurationV2IndexDocumentArgs.builder()
                            .suffix("index.html").build())
                    .build());

Now a static file is uploaded. This step is fully optional:

    var object = new BucketObjectv2("Object",BucketObjectv2Args.builder()
        .acl("public-read")
        .bucket(bucket.getId())
        .source(new FileAsset("./index.html"))
        .key("index.html")
        .contentType("text/html")
    .build());


...and the outputs provide as the website's domain name

    ctx.export("bucketName", bucket.bucket());
    ctx.export("cli", configuration.websiteEndpoint().applyValue(uri -> "curl " + uri));
    });
}

See the deployment from scratch. Live deployment (and mistakes :-)) included: