1. Answers
  2. Creating an AWS S3 Bucket

How do I create an AWS S3 Bucket?

In this guide, we’ll walk through creating an AWS S3 bucket. Amazon S3 is an excellent choice for scalable object storage with high durability. We will define the necessary resources to set up an S3 bucket and configure its properties like versioning and bucket policy for access control.

AWS S3 Bucket Creation

Below is a configuration to create an AWS S3 bucket. We’ll cover:

  1. Setting up the S3 bucket.
  2. Enabling versioning.
  3. Adding a bucket policy.
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

const myBucket = new aws.s3.BucketV2("my_bucket", {
    bucket: "my-awesome-bucket",
    acl: "private",
    versionings: [{
        enabled: true,
    }],
    tags: {
        Name: "MyBucket",
        Environment: "Dev",
    },
});
const myBucketPublicAccess = new aws.s3.BucketPublicAccessBlock("my_bucket_public_access", {
    bucket: myBucket.id,
    blockPublicAcls: true,
    blockPublicPolicy: true,
    ignorePublicAcls: true,
    restrictPublicBuckets: true,
});
const myBucketPolicy = new aws.s3.BucketPolicy("my_bucket_policy", {
    bucket: myBucket.id,
    policy: pulumi.jsonStringify({
        Version: "2012-10-17",
        Statement: [{
            Action: "s3:GetObject",
            Effect: "Allow",
            Resource: pulumi.interpolate`${myBucket.arn}/*`,
            Principal: "*",
        }],
    }),
});
export const bucketId = myBucket.id;
export const bucketArn = myBucket.arn;

In this example:

  • We defined an AWS provider to set the region.
  • We created an S3 bucket, enabled versioning, and tagged it.
  • We set a public access block to ensure the bucket’s content isn’t publicly accessible.
  • We added a bucket policy example, which allows public read-only access to the bucket’s content (be aware of security implications).
  • Finally, we exported the bucket ID and ARN for future reference.

This example demonstrates setting up a secure, versioned S3 bucket with a policy. Modify the names, configurations, and policies to suit your specific requirements.

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