Skip to content

Commit

Permalink
Merge pull request #358 from Financial-Times/create-serverless-plugin
Browse files Browse the repository at this point in the history
CPP:1051 - feat: add serverless plugin
  • Loading branch information
jkerr321 authored Feb 9, 2023
2 parents 08a9555 + d777c8a commit 443b93f
Show file tree
Hide file tree
Showing 9 changed files with 8,026 additions and 113 deletions.
10 changes: 10 additions & 0 deletions lib/types/src/schema/serverless.ts
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
7,974 changes: 7,861 additions & 113 deletions package-lock.json

Large diffs are not rendered by default.

Empty file.
36 changes: 36 additions & 0 deletions plugins/serverless/package.json
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"
}
}
32 changes: 32 additions & 0 deletions plugins/serverless/readme.md
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` |
3 changes: 3 additions & 0 deletions plugins/serverless/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import ServerlessRun from './tasks/run'

export const tasks = [ServerlessRun]
62 changes: 62 additions & 0 deletions plugins/serverless/src/tasks/run.ts
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()
}
}
}
19 changes: 19 additions & 0 deletions plugins/serverless/tsconfig.json
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/**/*"]
}
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@
},
{
"path": "plugins/typescript"
},
{
"path": "plugins/serverless"
}
]
}

0 comments on commit 443b93f

Please sign in to comment.