Creating an AWS S3 Bucket with Pulumi
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");