Skip to content

Commit

Permalink
new release
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeshay committed Jul 18, 2024
1 parent a46b577 commit dcf3dd1
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 16 deletions.
5 changes: 3 additions & 2 deletions apps/infra/src/bin/infra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Object.entries(ENVIRONMENT_PROPS).forEach(([environment, environmentProps]) => {
},
)

if (websiteProps.redirectAliases && websiteProps.hostedZoneName) {
if (websiteProps.redirectAliases) {
new RedirectStack(
app,
createStackName(
Expand All @@ -55,7 +55,8 @@ Object.entries(ENVIRONMENT_PROPS).forEach(([environment, environmentProps]) => {
{
...environmentProps,
aliases: websiteProps.redirectAliases,
hostedZoneName: websiteProps.hostedZoneName,
hostedZoneName: websiteProps.hostedZoneName!,
targetAlias: websiteProps.aliases![0],
},
)
}
Expand Down
2 changes: 1 addition & 1 deletion apps/infra/src/lib/constants/environments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ const ENVIRONMENT_PROPS: Record<Environment, EnvironmentProps> = {
hostedZoneName: "astro-aws.org",
mode: "static",
app: "apps/docs",
redirectAliases: ["", "www", "docs", "www.docs"],
redirectAliases: ["", "docs", "www.docs"],
runtime: "nodejs20",
},
],
Expand Down
50 changes: 39 additions & 11 deletions apps/infra/src/lib/stacks/redirect-stack.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import * as path from "node:path"

import { Stack } from "aws-cdk-lib/core"
import { RemovalPolicy, Stack } from "aws-cdk-lib/core"
import {
CloudFrontWebDistribution,
Distribution,
Function,
FunctionCode,
FunctionEventType,
OriginProtocolPolicy,
PriceClass,
ViewerCertificate,
ViewerProtocolPolicy,
} from "aws-cdk-lib/aws-cloudfront"
import type { Construct } from "constructs"
Expand All @@ -17,14 +21,20 @@ import {
} from "aws-cdk-lib/aws-route53"
import { CloudFrontTarget } from "aws-cdk-lib/aws-route53-targets"
import { S3Origin } from "aws-cdk-lib/aws-cloudfront-origins"
import { BlockPublicAccess, Bucket, BucketEncryption } from "aws-cdk-lib/aws-s3"
import {
BlockPublicAccess,
Bucket,
BucketEncryption,
RedirectProtocol,
} from "aws-cdk-lib/aws-s3"

import type { AstroAWSStackProps } from "../types/astro-aws-stack-props.js"
import { CrossRegionCertificate } from "../constructs/cross-region-certificate.js"

export type RedirectStackProps = AstroAWSStackProps & {
hostedZoneName: string
aliases: [string, ...string[]]
targetAlias: string
}

export class RedirectStack extends Stack {
Expand All @@ -44,6 +54,9 @@ export class RedirectStack extends Stack {
})

const [domainName, ...alternateNames] = domainNames
const targetDomainName = [props.targetAlias, "astro-aws.org"]
.filter(Boolean)
.join(".")

const { certificate } = new CrossRegionCertificate(this, "Certificate", {
alternateNames,
Expand All @@ -55,23 +68,38 @@ export class RedirectStack extends Stack {
blockPublicAccess: BlockPublicAccess.BLOCK_ALL,
encryption: BucketEncryption.S3_MANAGED,
enforceSSL: true,
removalPolicy: RemovalPolicy.DESTROY,
versioned: true,
websiteRedirect: {
hostName: targetDomainName,
protocol: RedirectProtocol.HTTPS,
},
})

const distribution = new Distribution(this, "WWWRedirectDistribution", {
certificate,
defaultBehavior: {
functionAssociations: [
const distribution = new CloudFrontWebDistribution(
this,
"WWWRedirectDistribution",
{
defaultRootObject: "",
originConfigs: [
{
eventType: FunctionEventType.VIEWER_REQUEST,
function: wwwRedirectFunction,
behaviors: [{ isDefaultBehavior: true }],
customOriginSource: {
domainName: bucket.bucketWebsiteDomainName,
originProtocolPolicy: OriginProtocolPolicy.HTTP_ONLY,
},
},
],
origin: new S3Origin(bucket),
viewerCertificate: ViewerCertificate.fromAcmCertificate(certificate, {
aliases: domainNames,
}),
comment: `Redirect to ${targetDomainName} from ${domainNames.join(
", ",
)}`,
priceClass: PriceClass.PRICE_CLASS_ALL,
viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
},
domainNames,
})
)

const hostedZone = HostedZone.fromLookup(this, "HostedZone", {
domainName: hostedZoneName,
Expand Down
1 change: 0 additions & 1 deletion apps/infra/support/cross-region-certificate/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { randomUUID } from "node:crypto"

import {
ACMClient,
DeleteCertificateCommand,
DescribeCertificateCommand,
RequestCertificateCommand,
} from "@aws-sdk/client-acm"
Expand Down
2 changes: 1 addition & 1 deletion apps/infra/support/redirect/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function handler(event) {
statusDescription: "Temporary Redirect",
headers: {
location: {
value: "https://www.docs.astro-aws.org" + event.request.uri + qs,
value: "https://www.astro-aws.org" + event.request.uri + qs,
},
},
}
Expand Down

0 comments on commit dcf3dd1

Please sign in to comment.