2023-05-15 15:25:28 -07:00

2.0 KiB

title, meta_desc, meta_image, service, description, aws_here, menu, aliases
title meta_desc meta_image service description aws_here menu aliases
Creating an AWS S3 Bucket with Pulumi Learn how to use Pulumi to define an AWS S3 resource which can then be deployed to AWS and managed as infrastructure as code. /images/docs/service/aws-s3.png S3 is object storage built to store and retrieve any amount of data from anywhere https://aws.amazon.com/s3
aws
name parent
S3 aws-guides
/docs/aws/s3/

Create an AWS S3 resource using @pulumi/aws

The @pulumi/aws library enables fine-grained control over the AWS S3 resource meaning it can be coded, deployed, and managed entirely in code.

const aws = require("@pulumi/aws");

const bucket = new aws.s3.Bucket("my-bucket");

const bucketMetric = new aws.s3.BucketMetric("my-bucket-metric", {
    bucket: bucket.bucket
});

const bucketNotification = new aws.s3.BucketNotification("my-bucket-notification", {
    bucket: bucket.bucket
});

const bucketObject = new aws.s3.BucketObject("my-bucket-object", {
    bucket: bucket.bucket,
    content: "hello world"
});

const bucketPolicy = new aws.s3.BucketPolicy("my-bucket-policy", {
    bucket: bucket.bucket,
    policy: bucket.bucket.apply(publicReadPolicyForBucket)
})

function publicReadPolicyForBucket(bucketName: string) {
    return JSON.stringify({
        Version: "2012-10-17",
        Statement: [{
            Effect: "Allow",
            Principal: "*",
            Action: [
                "s3:GetObject"
            ],
            Resource: [
                `arn:aws:s3:::${bucketName}/*` // policy refers to bucket name explicitly
            ]
        }]
    });
}

Create an AWS S3 bucket using @pulumi/cloud

The @pulumi/cloud library provides a high-level abstraction over the AWS S3 resource ensuring the same code can be used in multi-cloud environments.

const cloud = require("@pulumi/cloud-aws");
const aws = require("@pulumi/aws");

// A new bucket that will work in any cloud
const bucket = new cloud.Bucket("bucket");