-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #358 from Financial-Times/create-serverless-plugin
CPP:1051 - feat: add serverless plugin
- Loading branch information
Showing
9 changed files
with
8,026 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { SchemaOutput } from '../schema' | ||
|
||
export const ServerlessSchema = { | ||
configPath: 'string?', | ||
useVault: 'boolean?', | ||
ports: 'array.number?' | ||
} as const | ||
export type ServerlessOptions = SchemaOutput<typeof ServerlessSchema> | ||
|
||
export const Schema = ServerlessSchema |
Large diffs are not rendered by default.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"name": "@dotcom-tool-kit/serverless", | ||
"version": "0.1.0", | ||
"description": "a plugin to manage and deploy apps using AWS Serverless", | ||
"main": "lib", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [], | ||
"author": "FT.com Platforms Team <platforms-team.customer-products@ft.com>", | ||
"license": "ISC", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/financial-times/dotcom-tool-kit.git", | ||
"directory": "plugins/serverless" | ||
}, | ||
"bugs": "https://github.com/financial-times/dotcom-tool-kit/issues", | ||
"homepage": "https://github.com/financial-times/dotcom-tool-kit/tree/main/plugins/serverless", | ||
"files": [ | ||
"/lib", | ||
".toolkitrc.yml" | ||
], | ||
"peerDependencies": { | ||
"dotcom-tool-kit": "2.x", | ||
"serverless-offline": "^12.0.4" | ||
}, | ||
"dependencies": { | ||
"@dotcom-tool-kit/error": "^2.0.1", | ||
"@dotcom-tool-kit/state": "^2.0.1", | ||
"@dotcom-tool-kit/types": "^2.8.0", | ||
"@dotcom-tool-kit/vault": "^2.0.12", | ||
"get-port": "^5.1.1", | ||
"tslib": "^2.3.1", | ||
"wait-port": "^0.2.9" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# @dotcom-tool-kit/serverless | ||
|
||
Tool Kit plugin to manage Lambdas with [AWS serverless](https://www.serverless.com/framework/docs/getting-started/). | ||
|
||
## Installation & usage | ||
|
||
With Tool Kit [already set up](https://github.com/financial-times/dotcom-tool-kit#installing-and-using-tool-kit), install this plugin as a dev dependency: | ||
|
||
```sh | ||
npm install --save-dev @dotcom-tool-kit/serverless | ||
``` | ||
|
||
And add it to your repo's `.toolkitrc.yml`: | ||
|
||
```yml | ||
plugins: | ||
- '@dotcom-tool-kit/serverless' | ||
``` | ||
## Options | ||
| Key | Description | Default value | | ||
|-|-|-| | ||
| `configPath` | [optional] path to your serverless config file. If this is not provided aws defaults to `./serverless.yml` but [other config fomats are accepted](https://www.serverless.com/framework/docs/providers/aws/guide/intro#alternative-configuration-format)| | | ||
| `useVault` | option to run the application with environment variables from Vault | `true` | | ||
| `ports` | ports to try to bind to for this application | `[3001, 3002, 3003]` | | ||
|
||
## Tasks | ||
|
||
| Task | Description | Default hooks | | ||
|-|-|-| | ||
| `ServerlessRun` | Run application with `serverless` | `run:local` | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import ServerlessRun from './tasks/run' | ||
|
||
export const tasks = [ServerlessRun] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { Task } from '@dotcom-tool-kit/types' | ||
import { ServerlessOptions, ServerlessSchema } from '@dotcom-tool-kit/types/lib/schema/serverless' | ||
import { spawn } from 'child_process' | ||
import { VaultEnvVars } from '@dotcom-tool-kit/vault' | ||
import { hookConsole, hookFork } from '@dotcom-tool-kit/logger' | ||
import getPort from 'get-port' | ||
import waitPort from 'wait-port' | ||
|
||
export default class ServerlessRun extends Task<typeof ServerlessSchema> { | ||
static description = 'Run serverless functions locally' | ||
|
||
static defaultOptions: ServerlessOptions = { | ||
useVault: true, | ||
ports: [3001, 3002, 3003] | ||
} | ||
|
||
async run(): Promise<void> { | ||
const { useVault, ports, configPath } = this.options | ||
|
||
let vaultEnv = {} | ||
|
||
if (useVault) { | ||
const vault = new VaultEnvVars(this.logger, { | ||
environment: 'development' | ||
}) | ||
|
||
vaultEnv = await vault.get() | ||
} | ||
|
||
const port = | ||
Number(process.env.PORT) || | ||
(await getPort({ | ||
port: ports | ||
})) | ||
|
||
this.logger.verbose('starting the child serverless process...') | ||
const args = ['offline', 'start', '--host', 'local.ft.com', '--httpPort', `${port}`] | ||
if (configPath) { | ||
args.push('--config', './serverless.yml') | ||
} | ||
|
||
const child = spawn('serverless', args, { | ||
env: { | ||
...vaultEnv, | ||
PORT: port.toString(), | ||
...process.env | ||
} | ||
}) | ||
|
||
hookFork(this.logger, 'serverless', child) | ||
|
||
const unhook = hookConsole(this.logger, 'wait-port') | ||
try { | ||
await waitPort({ | ||
host: 'localhost', | ||
port: port | ||
}) | ||
} finally { | ||
unhook() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"extends": "../../tsconfig.settings.json", | ||
"compilerOptions": { | ||
"outDir": "lib", | ||
"rootDir": "src" | ||
}, | ||
"references": [ | ||
{ | ||
"path": "../../lib/types" | ||
}, | ||
{ | ||
"path": "../../lib/vault" | ||
}, | ||
{ | ||
"path": "../../lib/logger" | ||
} | ||
], | ||
"include": ["src/**/*"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,6 +108,9 @@ | |
}, | ||
{ | ||
"path": "plugins/typescript" | ||
}, | ||
{ | ||
"path": "plugins/serverless" | ||
} | ||
] | ||
} |