Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Website: use s3 static website hosting (so I can do redirects and other stuff #7255

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 19 additions & 51 deletions ts/pulumi/lib/website.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,50 +222,23 @@ export class Website extends pulumi.ComponentResource {
)}]`
)(objects.get(args.index));

const originAccessIdentity = new aws.cloudfront.OriginAccessIdentity(
`${name}_origin_access_identity`,
const s3Site = new aws.s3.BucketWebsiteConfigurationV2(
`${name}_bucket_website`,
{
comment:
'this is needed to setup s3 polices and make s3 not public.',
},
{
parent: this,
}
);

// Only allow cloudfront to access content bucket.
const bucketPolicy = new aws.s3.BucketPolicy(
`${name}_bucket_policy`,
{
bucket: bucket.id, // refer to the bucket created earlier
policy: pulumi
.all([originAccessIdentity.iamArn, bucket.arn])
.apply(([oaiArn, bucketArn]) =>
JSON.stringify({
Version: '2012-10-17',
Statement: [
{
Effect: 'Allow',
Action: ['s3:ListBucket'],
Principal: {
AWS: oaiArn,
},
Resource: [bucketArn],
},
{
Effect: 'Allow',
Principal: {
AWS: oaiArn,
}, // Only allow Cloudfront read access.
Action: ['s3:GetObject'],
Resource: [`${bucketArn}/*`], // Give Cloudfront access to the entire bucket.
},
],
})
),
bucket: bucket.bucket,
indexDocument: {
suffix: "index.html"
},
...(
errorDocumentObject != undefined?
{
errorDocument: {
key: pulumi.interpolate`/${errorDocumentObject.key}`
}
} : {})
},
{ parent: this }
);
{parent: this}
)

// response headers policy (http headers)

Expand Down Expand Up @@ -310,13 +283,9 @@ export class Website extends pulumi.ComponentResource {
{
origins: [
{
s3OriginConfig: {
originAccessIdentity:
originAccessIdentity.cloudfrontAccessIdentityPath,
},
domainName: bucket.bucketRegionalDomainName,
originId: `${name}_cloudfront_distribution`,
},
originId: bucket.arn,
domainName: s3Site.websiteEndpoint,
}
],
enabled: true,
isIpv6Enabled: true,
Expand All @@ -333,7 +302,7 @@ export class Website extends pulumi.ComponentResource {
{
errorCode: 404,
responseCode: 404,
responsePagePath: pulumi.interpolate`/${errorDocumentObject.key}`,
responsePagePath: pulumi.interpolate`/${errorDocumentObject.key}`
},
],
}
Expand Down Expand Up @@ -463,7 +432,6 @@ export class Website extends pulumi.ComponentResource {
this.registerOutputs({
distribution,
record,
bucketPolicy,
});
}
}
Expand Down
Loading