1. Answers
  2. Configuring an AWS S3 Bucket with Pulumi

How do I configure an AWS S3 bucket with Pulumi?

In this guide, we will walk through the steps to configure an AWS S3 bucket using Pulumi with TypeScript. We will define the S3 bucket, set its properties, and configure access policies. This setup will help you manage your S3 bucket as part of your infrastructure as code.

Key Points

  • We will create an S3 bucket.
  • We will set up bucket properties such as versioning and tags.
  • We will configure access policies for the bucket.
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

// Create an S3 bucket
const bucket = new aws.s3.Bucket("my-bucket", {
    acl: "private", // Access control list
    tags: {
        Environment: "Dev",
        Name: "MyBucket",
    },
    versioning: {
        enabled: true,
    },
});

// Export the name of the bucket
export const bucketName = bucket.id;

// Create a bucket policy allowing public read access to the bucket
const bucketPolicy = new aws.s3.BucketPolicy("bucketPolicy", {
    bucket: bucket.bucket,
    policy: bucket.bucket.apply(bucketName => JSON.stringify({
        Version: "2012-10-17",
        Statement: [{
            Action: ["s3:GetObject"],
            Effect: "Allow",
            Resource: [`arn:aws:s3:::${bucketName}/*`],
            Principal: "*",
        }],
    })),
});

// Export the bucket URL
export const bucketUrl = pulumi.interpolate`https://${bucket.bucketRegionalDomainName}`;

Concluding Summary

In this guide, we configured an AWS S3 bucket using Pulumi with TypeScript. We created an S3 bucket, enabled versioning, tagged the bucket, and set up a bucket policy to allow public read access. This setup helps in managing the S3 bucket as part of your infrastructure as code.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up