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

feat: use custom domain linkdex.dag.haus #4

Merged
merged 4 commits into from Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env
Expand Up @@ -4,3 +4,7 @@
# uncomment to use existing bucket (in same region)
# if left commented out, we create a new, empty bucket.
# BUCKET_NAME=dotstorage-staging-0

# uncomment to try out deploying the api under a custom domain.
# the value should match a hosted zone configured in route53 that your aws account has access to.
# HOSTED_ZONE=linkdex.dag.haus
22 changes: 18 additions & 4 deletions stacks/LinkdexStack.ts
@@ -1,14 +1,18 @@
import { StackContext, Api, Bucket } from "@serverless-stack/resources"
import { aws_s3 as s3 } from 'aws-cdk-lib'

export function LinkdexStack({ app,stack }: StackContext) {
export function LinkdexStack({ app, stack }: StackContext) {
// either import an existing bucket or create a new one for dev.
// see: https://docs.sst.dev/advanced/importing-resources
const bucket = (process.env.BUCKET_NAME)
? new Bucket(stack, 'existing-cars', { cdk: { bucket: s3.Bucket.fromBucketName(stack, 'imported-cars', process.env.BUCKET_NAME) }})
const bucketName = process.env.BUCKET_NAME
const bucket = (bucketName)
? new Bucket(stack, 'existing-cars', { cdk: { bucket: s3.Bucket.fromBucketName(stack, 'imported-cars', bucketName) }})
: new Bucket(stack, 'cars')

const customDomain = getCustomDomain(app.stage, process.env.HOSTED_ZONE)

const api = new Api(stack, "api", {
customDomain,
defaults: {
function: {
permissions: [bucket],
Expand All @@ -21,7 +25,17 @@ export function LinkdexStack({ app,stack }: StackContext) {
"GET /": "functions/linkdex.handler",
}
})

stack.addOutputs({
ApiEndpoint: api.url
ApiEndpoint: api.url,
CustomDomain: customDomain ? `https://${customDomain.domainName}` : 'Set HOSTED_ZONE in env to deploy to a custom domain'
})
}

function getCustomDomain (stage: string, hostedZone?: string) {
if (!hostedZone) {
return undefined
}
const domainName = stage === 'prod' ? hostedZone : `${stage}.${hostedZone}`
return { domainName, hostedZone }
}