-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/platform 696: accept custom cloudfront distribution #33
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
base: main
Are you sure you want to change the base?
Changes from all commits
fb8b46e
c8971da
8202721
ceb644b
22c1b21
13d8dad
5af47e9
d17b73c
e6d3f7f
f47986b
22fdd5e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,21 @@ | ||
import { ElasticBeanstalk } from '@aws-sdk/client-elastic-beanstalk' | ||
import { S3 } from '@aws-sdk/client-s3' | ||
import { CloudFront } from '@aws-sdk/client-cloudfront' | ||
import { CloudFront, GetDistributionCommandOutput } from '@aws-sdk/client-cloudfront' | ||
import fs from 'node:fs' | ||
import childProcess from 'node:child_process' | ||
import path from 'node:path' | ||
import { buildApp, OUTPUT_FOLDER } from '../build/next' | ||
import { NextRenderServerStack, type NextRenderServerStackProps } from '../cdk/stacks/NextRenderServerStack' | ||
import { NextCloudfrontStack, type NextCloudfrontStackProps } from '../cdk/stacks/NextCloudfrontStack' | ||
import { getAWSCredentials, uploadFolderToS3, uploadFileToS3, AWS_EDGE_REGION, emptyBucket } from '../common/aws' | ||
import { | ||
getAWSCredentials, | ||
uploadFolderToS3, | ||
uploadFileToS3, | ||
AWS_EDGE_REGION, | ||
emptyBucket, | ||
updateDistribution, | ||
getCloudFrontDistribution | ||
} from '../common/aws' | ||
import { AppStack } from '../common/cdk' | ||
import { getProjectSettings } from '../common/project' | ||
import loadConfig from './helpers/loadConfig' | ||
|
@@ -21,6 +29,8 @@ export interface DeployConfig { | |
region?: string | ||
profile?: string | ||
} | ||
cloudFrontId?: string | ||
skipDefaultBehavior?: boolean | ||
} | ||
|
||
export interface DeployStackProps { | ||
|
@@ -53,9 +63,10 @@ const createOutputFolder = () => { | |
export const deploy = async (config: DeployConfig) => { | ||
let cleanNextApp | ||
try { | ||
const { siteName, stage = 'development', aws } = config | ||
const { siteName, stage = 'development', aws, cloudFrontId, skipDefaultBehavior } = config | ||
const credentials = await getAWSCredentials({ region: config.aws.region, profile: config.aws.profile }) | ||
const region = aws.region || process.env.REGION | ||
let customCFDistribution: GetDistributionCommandOutput | undefined | ||
|
||
if (!credentials.accessKeyId || !credentials.secretAccessKey) { | ||
throw new Error('AWS Credentials are required.') | ||
|
@@ -97,6 +108,10 @@ export const deploy = async (config: DeployConfig) => { | |
} | ||
const siteNameLowerCased = siteName.toLowerCase() | ||
|
||
if (cloudFrontId) { | ||
customCFDistribution = await getCloudFrontDistribution(cloudfrontClient, cloudFrontId) | ||
} | ||
|
||
const nextRenderServerStack = new AppStack<NextRenderServerStack, NextRenderServerStackProps>( | ||
`${siteNameLowerCased}-server`, | ||
NextRenderServerStack, | ||
|
@@ -132,7 +147,8 @@ export const deploy = async (config: DeployConfig) => { | |
cacheConfig, | ||
env: { | ||
region: AWS_EDGE_REGION // required since Edge can be deployed only here. | ||
} | ||
}, | ||
customCloudFrontDistribution: customCFDistribution?.Distribution | ||
} | ||
) | ||
const nextCloudfrontStackOutput = await nextCloudfrontStack.deployStack() | ||
|
@@ -189,6 +205,19 @@ export const deploy = async (config: DeployConfig) => { | |
VersionLabel: versionLabel | ||
}) | ||
|
||
// if custom cf distribution, update it | ||
if (customCFDistribution) { | ||
await updateDistribution(cloudfrontClient, customCFDistribution, { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should do that inside stack, otherwise we will keep changing cloudfront with each deployment There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as the above comment. I searched and there is no way to update the distribution inside the stack. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we check at least that those origins and rules were already added, so we can skip that step? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will create a function to check this then. |
||
longCachePolicyId: nextCloudfrontStackOutput.LongCachePolicyId!, | ||
splitCachePolicyId: nextCloudfrontStackOutput.SplitCachePolicyId!, | ||
routingFunctionArn: nextCloudfrontStackOutput.RoutingFunctionArn!, | ||
checkExpirationFunctionArn: nextCloudfrontStackOutput.CheckExpirationFunctionArn!, | ||
staticBucketName: nextCloudfrontStackOutput.StaticBucketRegionalDomainName!, | ||
addAdditionalBehaviour: true, | ||
skipDefaultBehavior | ||
}) | ||
} | ||
|
||
await cloudfrontClient.createInvalidation({ | ||
DistributionId: nextCloudfrontStackOutput.CloudfrontDistributionId!, | ||
InvalidationBatch: { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we also need to add new rules and lambda to existing cloudfront
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can call
this.cf.addBehaviour
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, this isn't supported by the cloudformation stack. we can only add existing id to the cloud formation template (which is what I did here by setting it to the output).
aws/aws-cdk#12524