Skip to content
This repository was archived by the owner on Feb 1, 2025. It is now read-only.

Commit b75c9c2

Browse files
committed
✨ feat(region & remove): allow for specifying of region and add remove command (fix #68, fix #69)
fix #68, fix #69
1 parent a5ef4ba commit b75c9c2

File tree

7 files changed

+106
-18
lines changed

7 files changed

+106
-18
lines changed

Diff for: lib/cdk/app.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import { App } from 'aws-cdk-lib'
22
import path from 'path'
33
import { NextStandaloneStack } from './stack'
4-
import {
5-
DEFAULT_TIMEOUT as IMAGE_LAMBDA_DEFAULT_TIMEOUT,
6-
DEFAULT_MEMORY as IMAGE_LAMBDA_DEFAULT_MEMORY,
7-
} from './utils/imageLambda'
4+
import { DEFAULT_TIMEOUT as IMAGE_LAMBDA_DEFAULT_TIMEOUT, DEFAULT_MEMORY as IMAGE_LAMBDA_DEFAULT_MEMORY } from './utils/imageLambda'
85
import { handler, name, optimizerCodePath, optimizerLayerPath, version } from '@sladg/imaginex-lambda'
6+
import { DEFAULT_TIMEOUT as SERVER_LAMBDA_DEFAULT_TIMEOUT, DEFAULT_MEMORY as SERVER_LAMBDA_DEFAULT_MEMORY } from './utils/serverLambda'
97

108
const app = new App()
119

@@ -32,16 +30,16 @@ new NextStandaloneStack(app, process.env.STACK_NAME, {
3230
apigwServerPath: '/_server',
3331
apigwImagePath: '/_image',
3432

35-
lambdaTimeout: process.env.LAMBDA_TIMEOUT ? Number(process.env.LAMBDA_TIMEOUT) : 15,
36-
lambdaMemory: process.env.LAMBDA_MEMORY ? Number(process.env.LAMBDA_MEMORY) : 1024,
33+
lambdaTimeout: process.env.LAMBDA_TIMEOUT ? Number(process.env.LAMBDA_TIMEOUT) : SERVER_LAMBDA_DEFAULT_TIMEOUT,
34+
lambdaMemory: process.env.LAMBDA_MEMORY ? Number(process.env.LAMBDA_MEMORY) : SERVER_LAMBDA_DEFAULT_MEMORY,
3735
imageLambdaTimeout: process.env.LAMBDA_TIMEOUT ? Number(process.env.IMAGE_LAMBDA_TIMEOUT) : IMAGE_LAMBDA_DEFAULT_TIMEOUT,
3836
imageLambdaMemory: process.env.LAMBDA_MEMORY ? Number(process.env.IMAGE_LAMBDA_MEMORY) : IMAGE_LAMBDA_DEFAULT_MEMORY,
3937
hostedZone: process.env.HOSTED_ZONE ?? undefined,
4038
dnsPrefix: process.env.DNS_PREFIX ?? undefined,
4139
customApiDomain: process.env.CUSTOM_API_DOMAIN ?? undefined,
4240
env: {
4341
account: process.env.CDK_DEFAULT_ACCOUNT,
44-
region: process.env.CDK_DEFAULT_REGION,
42+
region: process.env.AWS_REGION ?? process.env.CDK_DEFAULT_REGION,
4543
},
4644
})
4745

Diff for: lib/cdk/utils/imageLambda.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ export interface SetupImageLambdaProps {
1212
timeout?: number
1313
}
1414

15-
export const DEFAULT_MEMORY = 256
15+
export const DEFAULT_MEMORY = 512
1616
export const DEFAULT_TIMEOUT = 10
1717

18-
export const setupImageLambda = (scope: Stack, { assetsBucket, codePath, handler, layerPath, lambdaHash, memory = DEFAULT_MEMORY, timeout = DEFAULT_TIMEOUT }: SetupImageLambdaProps) => {
18+
export const setupImageLambda = (
19+
scope: Stack,
20+
{ assetsBucket, codePath, handler, layerPath, lambdaHash, memory = DEFAULT_MEMORY, timeout = DEFAULT_TIMEOUT }: SetupImageLambdaProps,
21+
) => {
1922
const depsLayer = new LayerVersion(scope, 'ImageOptimizationLayer', {
2023
code: Code.fromAsset(layerPath, {
2124
assetHash: lambdaHash + '_layer',

Diff for: lib/cdk/utils/serverLambda.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ export interface SetupServerLambdaProps {
1010
timeout: number
1111
}
1212

13-
export const setupServerLambda = (scope: Stack, { basePath, codePath, dependenciesPath, handler, memory, timeout }: SetupServerLambdaProps) => {
13+
export const DEFAULT_MEMORY = 1024
14+
export const DEFAULT_TIMEOUT = 20
15+
16+
export const setupServerLambda = (
17+
scope: Stack,
18+
{ basePath, codePath, dependenciesPath, handler, memory = DEFAULT_MEMORY, timeout = DEFAULT_TIMEOUT }: SetupServerLambdaProps,
19+
) => {
1420
const depsLayer = new LayerVersion(scope, 'DepsLayer', {
1521
// This folder does not use Custom hash as depenendencies are most likely changing every time we deploy.
1622
code: Code.fromAsset(dependenciesPath),

Diff for: lib/cli.ts

+48-7
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ import packageJson from '../package.json'
66
import { deployHandler } from './cli/deploy'
77
import { packHandler } from './cli/pack'
88
import { wrapProcess } from './utils'
9-
import {
10-
DEFAULT_TIMEOUT as IMAGE_LAMBDA_DEFAULT_TIMEOUT,
11-
DEFAULT_MEMORY as IMAGE_LAMBDA_DEFAULT_MEMORY,
12-
} from './cdk/utils/imageLambda'
9+
import { DEFAULT_TIMEOUT as IMAGE_LAMBDA_DEFAULT_TIMEOUT, DEFAULT_MEMORY as IMAGE_LAMBDA_DEFAULT_MEMORY } from './cdk/utils/imageLambda'
10+
import { removeHandler } from './cli/remove'
11+
import { DEFAULT_REGION } from './consts'
1312

1413
const commandCwd = process.cwd()
1514
const program = new Command()
@@ -46,6 +45,7 @@ program
4645
.action(async (options) => {
4746
console.log('Our config is: ', options)
4847
const { standaloneFolder, publicFolder, handlerPath, outputFolder } = options
48+
4949
wrapProcess(packHandler({ commandCwd, handlerPath, outputFolder, publicFolder, standaloneFolder }))
5050
})
5151

@@ -55,17 +55,58 @@ program
5555
.option('--stackName <name>', 'Name of the stack to be deployed.', 'StandaloneNextjsStack-Temporary')
5656
.option('--appPath <path>', 'Absolute path to app.', path.resolve(__dirname, '../dist/cdk/app.js'))
5757
.option('--bootstrap', 'Bootstrap CDK stack.', false)
58+
.option('--region <region>', 'AWS region to deploy to.', DEFAULT_REGION)
5859
.option('--lambdaTimeout <sec>', 'Set timeout for lambda function handling server requests.', Number, 15)
5960
.option('--lambdaMemory <mb>', 'Set memory for lambda function handling server requests.', Number, 512)
6061
.option('--imageLambdaTimeout <sec>', 'Set timeout for lambda function handling image optimization.', Number, IMAGE_LAMBDA_DEFAULT_TIMEOUT)
6162
.option('--imageLambdaMemory <mb>', 'Set memory for lambda function handling image optimization.', Number, IMAGE_LAMBDA_DEFAULT_MEMORY)
6263
.option('--hostedZone <domainName>', 'Hosted zone domain name to be used for creating DNS records (example: example.com).', undefined)
6364
.option('--domainNamePrefix <prefix>', 'Prefix for creating DNS records, if left undefined, hostedZone will be used (example: app).', undefined)
64-
.option('--customApiDomain <domain>', 'Domain to forward the requests to /api routes, if left undefined, API routes will be handled by the server lambda.', undefined)
65+
.option('--customApiDomain <domain>', 'Domain to forward the requests to /api routes, by default API routes will be handled by the server lambda.', undefined)
66+
.action(async (options) => {
67+
console.log('Our config is: ', options)
68+
const {
69+
stackName,
70+
appPath,
71+
bootstrap,
72+
region,
73+
lambdaTimeout,
74+
lambdaMemory,
75+
imageLambdaMemory,
76+
imageLambdaTimeout,
77+
hostedZone,
78+
domainNamePrefix,
79+
customApiDomain,
80+
} = options
81+
82+
wrapProcess(
83+
deployHandler({
84+
stackName,
85+
appPath,
86+
bootstrap,
87+
region,
88+
lambdaTimeout,
89+
lambdaMemory,
90+
imageLambdaMemory,
91+
imageLambdaTimeout,
92+
hostedZone,
93+
domainNamePrefix,
94+
customApiDomain,
95+
}),
96+
)
97+
})
98+
99+
program
100+
.command('remove')
101+
.description('Remove Next application via CDK')
102+
.option('--stackName <name>', 'Name of the stack to be deployed.', 'StandaloneNextjsStack-Temporary')
103+
.option('--appPath <path>', 'Absolute path to app.', path.resolve(__dirname, '../dist/cdk/app.js'))
104+
.option('--region <region>', 'AWS region to deploy to.', DEFAULT_REGION)
65105
.action(async (options) => {
66106
console.log('Our config is: ', options)
67-
const { stackName, appPath, bootstrap, lambdaTimeout, lambdaMemory, imageLambdaMemory, imageLambdaTimeout, hostedZone, domainNamePrefix, customApiDomain } = options
68-
wrapProcess(deployHandler({ stackName, appPath, bootstrap, lambdaTimeout, lambdaMemory, imageLambdaMemory, imageLambdaTimeout, hostedZone, domainNamePrefix, customApiDomain }))
107+
const { stackName, appPath, region } = options
108+
109+
wrapProcess(removeHandler({ stackName, appPath, region }))
69110
})
70111

71112
program.parse(process.argv)

Diff for: lib/cli/deploy.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ interface Props {
44
stackName: string
55
appPath: string
66
bootstrap: boolean
7+
region: string
78
lambdaMemory?: number
89
lambdaTimeout?: number
910
imageLambdaMemory?: number
@@ -15,13 +16,26 @@ interface Props {
1516

1617
const cdkExecutable = require.resolve('aws-cdk/bin/cdk')
1718

18-
export const deployHandler = async ({ stackName, appPath, bootstrap, lambdaMemory, lambdaTimeout, imageLambdaMemory, imageLambdaTimeout, domainNamePrefix, hostedZone, customApiDomain }: Props) => {
19+
export const deployHandler = async ({
20+
stackName,
21+
appPath,
22+
bootstrap,
23+
region,
24+
lambdaMemory,
25+
lambdaTimeout,
26+
imageLambdaMemory,
27+
imageLambdaTimeout,
28+
domainNamePrefix,
29+
hostedZone,
30+
customApiDomain,
31+
}: Props) => {
1932
// All paths are absolute.
2033
const cdkApp = `node ${appPath}`
2134
const cdkCiFlags = `--require-approval never --ci`
2235

2336
const variables = {
2437
STACK_NAME: stackName,
38+
...(region && { AWS_REGION: region }),
2539
...(lambdaMemory && { LAMBDA_MEMORY: lambdaMemory.toString() }),
2640
...(lambdaTimeout && { LAMBDA_TIMEOUT: lambdaTimeout.toString() }),
2741
...(imageLambdaMemory && { IMAGE_LAMBDA_MEMORY: imageLambdaMemory.toString() }),

Diff for: lib/cli/remove.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { executeAsyncCmd } from '../utils'
2+
3+
interface Props {
4+
stackName: string
5+
appPath: string
6+
region: string
7+
}
8+
9+
const cdkExecutable = require.resolve('aws-cdk/bin/cdk')
10+
11+
export const removeHandler = async ({ appPath, stackName, region }: Props) => {
12+
const cdkApp = `node ${appPath}`
13+
const cdkCiFlags = `--force --ci`
14+
15+
const variables = {
16+
STACK_NAME: stackName,
17+
AWS_REGION: region,
18+
}
19+
20+
await executeAsyncCmd({
21+
cmd: `${cdkExecutable} destroy --app "${cdkApp}" ${cdkCiFlags}`,
22+
env: variables,
23+
})
24+
}

Diff for: lib/consts.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
export const nextServerConfigRegex = /(?<=conf: )(.*)(?=,)/
2+
3+
export const DEFAULT_REGION = 'eu-central-1'

0 commit comments

Comments
 (0)