@studion/infra-code-blocks
Version:
Studion common infra components
68 lines (67 loc) • 2.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.S3Assets = void 0;
const pulumi = require("@pulumi/pulumi");
const aws = require("@pulumi/aws");
const common_tags_1 = require("../../shared/common-tags");
class S3Assets extends pulumi.ComponentResource {
name;
bucket;
websiteConfig;
constructor(name, args, opts = {}) {
super('studion:static-site:S3Assets', name, {}, opts);
this.name = name;
const { bucketPrefix = `${this.name}-`, indexDocument = 'index.html', errorDocument = 'index.html', tags, } = args;
const [bucket, websiteConfig] = this.createWebsiteBucket(bucketPrefix, indexDocument, errorDocument, tags);
this.bucket = bucket;
this.websiteConfig = websiteConfig;
this.registerOutputs();
}
setupWebsiteBucketAccess(bucket) {
const bucketPublicAccessBlock = new aws.s3.BucketPublicAccessBlock(`${this.name}-bucket-access-block`, {
bucket: bucket.id,
blockPublicAcls: false,
blockPublicPolicy: false,
ignorePublicAcls: false,
restrictPublicBuckets: false,
}, { parent: this });
const policy = bucket.bucket.apply(getWebsiteBucketPolicy);
new aws.s3.BucketPolicy(`${this.name}-bucket-policy`, {
bucket: bucket.id,
policy: policy.json,
}, { parent: this, dependsOn: [bucketPublicAccessBlock] });
}
createWebsiteBucket(bucketPrefix, indexDocument, errorDocument, tags) {
const bucket = new aws.s3.Bucket(`${this.name}-bucket`, {
bucketPrefix,
tags: { ...common_tags_1.commonTags, ...tags },
}, { parent: this });
const config = new aws.s3.BucketWebsiteConfiguration(`${this.name}-bucket-website-config`, {
bucket: bucket.id,
indexDocument: {
suffix: indexDocument,
},
errorDocument: {
key: errorDocument,
},
}, { parent: this });
this.setupWebsiteBucketAccess(bucket);
return [bucket, config];
}
}
exports.S3Assets = S3Assets;
const getWebsiteBucketPolicy = (bucketName) => aws.iam.getPolicyDocument({
statements: [
{
effect: 'Allow',
principals: [
{
type: '*',
identifiers: ['*'],
},
],
actions: ['s3:GetObject'],
resources: [`arn:aws:s3:::${bucketName}/*`],
},
],
});