Skip to content

Commit

Permalink
Add slideshow origin to CloudFront distributions
Browse files Browse the repository at this point in the history
This commit introduces a new origin configuration for serving slideshow content from the S3 bucket in both production and staging CloudFront distributions. It ensures the `/slideshow/*` path pattern is handled appropriately, improving content delivery for slideshow files. Additionally, minor formatting adjustments were made for code clarity.
  • Loading branch information
miensol committed Feb 14, 2025
1 parent cf4fde8 commit d8ca20d
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions infrastructure/lib/website.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import * as cdk from 'aws-cdk-lib'
import { Arn, CfnOutput, Duration, RemovalPolicy } from 'aws-cdk-lib'
import { Bucket, IBucket } from 'aws-cdk-lib/aws-s3'
import cloudfront, {
import {
Behavior,
CloudFrontAllowedMethods,
CloudFrontWebDistribution, FunctionCode,
CloudFrontWebDistribution,
Function as CloudfrontFunction,
FunctionCode,
FunctionEventType,
OriginAccessIdentity,
OriginProtocolPolicy,
PriceClass,
SourceConfiguration,
ViewerCertificate,
Function as CloudfrontFunction,
PriceClass
ViewerCertificate
} from 'aws-cdk-lib/aws-cloudfront'
import { Effect, PolicyStatement, User } from 'aws-cdk-lib/aws-iam'
import { productionDomainNames, stagingDomainNames } from './domain-names'
Expand Down Expand Up @@ -102,6 +103,18 @@ export class Website extends cdk.Stack {
],
}

const slideshowDownloadOrigin: SourceConfiguration = {
s3OriginSource: {
s3BucketSource: props.ebooksBucket,
originAccessIdentity: props.ebooksOriginAccessIdentity,
},
behaviors: [
{
pathPattern: '/slideshow/*',
},
],
}

const apiOrigin: SourceConfiguration = {
customOriginSource: {
domainName: apiEndPointDomainName,
Expand Down Expand Up @@ -138,7 +151,10 @@ export class Website extends cdk.Stack {
outfile: pathWithExt(preserveUriParametersOnRedirectPath, '.js'),
platform: 'node',
})
const preserveUriParametersOnRedirectCompiled = fs.readFileSync(pathWithExt(preserveUriParametersOnRedirectPath, '.js'), 'utf-8')
const preserveUriParametersOnRedirectCompiled = fs.readFileSync(
pathWithExt(preserveUriParametersOnRedirectPath, '.js'),
'utf-8'
)

const staticContentBehavior: Behavior = {
isDefaultBehavior: true,
Expand All @@ -151,14 +167,15 @@ export class Website extends cdk.Stack {
function: new CloudfrontFunction(this, 'preserve-uri-parameters-on-redirect', {
code: FunctionCode.fromInline(preserveUriParametersOnRedirectCompiled),
}),
}
]
},
],
}

const productionWebDistribution = new CloudFrontWebDistribution(this, 'distribution', {
priceClass: PriceClass.PRICE_CLASS_ALL,
originConfigs: [
ebooksDownloadOrigin,
slideshowDownloadOrigin,
apiOrigin,
{
// we don't use s3 origin as gatsby-s3-deploy features will not work
Expand All @@ -184,6 +201,7 @@ export class Website extends cdk.Stack {
priceClass: PriceClass.PRICE_CLASS_ALL,
originConfigs: [
ebooksDownloadOrigin,
slideshowDownloadOrigin,
apiOrigin,
{
// we don't use s3 origin as gatsby-s3-deploy features will not work
Expand All @@ -193,9 +211,7 @@ export class Website extends cdk.Stack {
domainName: stagingBucket.bucketWebsiteDomainName,
originProtocolPolicy: OriginProtocolPolicy.HTTP_ONLY,
},
behaviors: [
staticContentBehavior,
],
behaviors: [staticContentBehavior],
},
],
viewerCertificate: ViewerCertificate.fromAcmCertificate(certificate, {
Expand Down Expand Up @@ -286,6 +302,6 @@ export class Website extends cdk.Stack {
}
}

function pathWithExt(filePath: string, ext: string ) {
function pathWithExt(filePath: string, ext: string) {
return path.format({ ...path.parse(filePath), base: '', ext })
}

0 comments on commit d8ca20d

Please sign in to comment.