mirror of
https://github.com/ivabus/www
synced 2024-11-22 23:45:06 +03:00
test init sls lambdas
This commit is contained in:
parent
8deb4e0fa7
commit
6b0a43c885
18 changed files with 3753 additions and 1 deletions
10
.github/workflows/staging.yml
vendored
10
.github/workflows/staging.yml
vendored
|
@ -79,6 +79,16 @@ jobs:
|
||||||
run: yarn deploy
|
run: yarn deploy
|
||||||
working-directory: .cdk
|
working-directory: .cdk
|
||||||
|
|
||||||
|
- uses: borales/actions-yarn@v3.0.0
|
||||||
|
with:
|
||||||
|
cmd: install
|
||||||
|
working-directory: lambdas
|
||||||
|
|
||||||
|
- name: deploy
|
||||||
|
uses: Teakowa/serverless-action@master
|
||||||
|
with:
|
||||||
|
args: --stage prod deploy
|
||||||
|
|
||||||
- name: Seal Deployment
|
- name: Seal Deployment
|
||||||
uses: bobheadxi/deployments@v1
|
uses: bobheadxi/deployments@v1
|
||||||
if: always()
|
if: always()
|
||||||
|
|
|
@ -16,7 +16,8 @@ The tea logo and wordmark are registered trademarks of tea.inc.
|
||||||
* Repeated components are in [`./src/layouts/partials/`].
|
* Repeated components are in [`./src/layouts/partials/`].
|
||||||
|
|
||||||
## Getting Started
|
## Getting Started
|
||||||
Just once assuming there are no updates in `/src/data/packages.json`. This will create the package detail pages.
|
The detail pages of each package are not committed to the repository for the simple reason of that would be too much to much repeating data.
|
||||||
|
Execute the following command just once per version of `/src/data/packages.json`. This will create the package detail pages in `/src/content/packages/[package_slug].md`.
|
||||||
```sh
|
```sh
|
||||||
.github/build-package-pages.sh src/data/packages.json src/content/packages
|
.github/build-package-pages.sh src/data/packages.json src/content/packages
|
||||||
```
|
```
|
||||||
|
|
9
lambdas/.gitignore
vendored
Normal file
9
lambdas/.gitignore
vendored
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
# package directories
|
||||||
|
node_modules
|
||||||
|
jspm_packages
|
||||||
|
|
||||||
|
# Serverless directories
|
||||||
|
.serverless
|
||||||
|
|
||||||
|
# esbuild directories
|
||||||
|
.esbuild
|
1
lambdas/.nvmrc
Normal file
1
lambdas/.nvmrc
Normal file
|
@ -0,0 +1 @@
|
||||||
|
lts/fermium
|
90
lambdas/README.md
Normal file
90
lambdas/README.md
Normal file
|
@ -0,0 +1,90 @@
|
||||||
|
# Serverless - AWS Node.js Typescript
|
||||||
|
|
||||||
|
This project has been generated using the `aws-nodejs-typescript` template from the [Serverless framework](https://www.serverless.com/).
|
||||||
|
|
||||||
|
For detailed instructions, please refer to the [documentation](https://www.serverless.com/framework/docs/providers/aws/).
|
||||||
|
|
||||||
|
## Installation/deployment instructions
|
||||||
|
|
||||||
|
Depending on your preferred package manager, follow the instructions below to deploy your project.
|
||||||
|
|
||||||
|
> **Requirements**: NodeJS `lts/fermium (v.14.15.0)`. If you're using [nvm](https://github.com/nvm-sh/nvm), run `nvm use` to ensure you're using the same Node version in local and in your lambda's runtime.
|
||||||
|
|
||||||
|
### Using Yarn
|
||||||
|
|
||||||
|
- Run `yarn` to install the project dependencies
|
||||||
|
- Run `yarn sls deploy` to deploy this stack to AWS
|
||||||
|
|
||||||
|
## Test your service
|
||||||
|
|
||||||
|
This template contains a single lambda function triggered by an HTTP request made on the provisioned API Gateway REST API `/hello` route with `POST` method. The request body must be provided as `application/json`. The body structure is tested by API Gateway against `src/functions/hello/schema.ts` JSON-Schema definition: it must contain the `name` property.
|
||||||
|
|
||||||
|
- requesting any other path than `/hello` with any other method than `POST` will result in API Gateway returning a `403` HTTP error code
|
||||||
|
- sending a `POST` request to `/hello` with a payload **not** containing a string property named `name` will result in API Gateway returning a `400` HTTP error code
|
||||||
|
- sending a `POST` request to `/hello` with a payload containing a string property named `name` will result in API Gateway returning a `200` HTTP status code with a message saluting the provided name and the detailed event processed by the lambda
|
||||||
|
|
||||||
|
> :warning: As is, this template, once deployed, opens a **public** endpoint within your AWS account resources. Anybody with the URL can actively execute the API Gateway endpoint and the corresponding lambda. You should protect this endpoint with the authentication method of your choice.
|
||||||
|
|
||||||
|
### Locally
|
||||||
|
|
||||||
|
In order to test the hello function locally, run the following command:
|
||||||
|
|
||||||
|
- `npx sls invoke local -f hello --path src/functions/hello/mock.json` if you're using NPM
|
||||||
|
- `yarn sls invoke local -f hello --path src/functions/hello/mock.json` if you're using Yarn
|
||||||
|
|
||||||
|
Check the [sls invoke local command documentation](https://www.serverless.com/framework/docs/providers/aws/cli-reference/invoke-local/) for more information.
|
||||||
|
|
||||||
|
### Remotely
|
||||||
|
|
||||||
|
Copy and replace your `url` - found in Serverless `deploy` command output - and `name` parameter in the following `curl` command in your terminal or in Postman to test your newly deployed application.
|
||||||
|
|
||||||
|
```
|
||||||
|
curl --location --request POST 'https://myApiEndpoint/dev/hello' \
|
||||||
|
--header 'Content-Type: application/json' \
|
||||||
|
--data-raw '{
|
||||||
|
"name": "Frederic"
|
||||||
|
}'
|
||||||
|
```
|
||||||
|
|
||||||
|
## Template features
|
||||||
|
|
||||||
|
### Project structure
|
||||||
|
|
||||||
|
The project code base is mainly located within the `src` folder. This folder is divided in:
|
||||||
|
|
||||||
|
- `functions` - containing code base and configuration for your lambda functions
|
||||||
|
- `libs` - containing shared code base between your lambdas
|
||||||
|
|
||||||
|
```
|
||||||
|
.
|
||||||
|
├── src
|
||||||
|
│ ├── functions # Lambda configuration and source code folder
|
||||||
|
│ │ ├── hello
|
||||||
|
│ │ │ ├── handler.ts # `Hello` lambda source code
|
||||||
|
│ │ │ ├── index.ts # `Hello` lambda Serverless configuration
|
||||||
|
│ │ │ ├── mock.json # `Hello` lambda input parameter, if any, for local invocation
|
||||||
|
│ │ │ └── schema.ts # `Hello` lambda input event JSON-Schema
|
||||||
|
│ │ │
|
||||||
|
│ │ └── index.ts # Import/export of all lambda configurations
|
||||||
|
│ │
|
||||||
|
│ └── libs # Lambda shared code
|
||||||
|
│ └── apiGateway.ts # API Gateway specific helpers
|
||||||
|
│ └── handlerResolver.ts # Sharable library for resolving lambda handlers
|
||||||
|
│ └── lambda.ts # Lambda middleware
|
||||||
|
│
|
||||||
|
├── package.json
|
||||||
|
├── serverless.ts # Serverless service file
|
||||||
|
├── tsconfig.json # Typescript compiler configuration
|
||||||
|
├── tsconfig.paths.json # Typescript paths
|
||||||
|
└── webpack.config.js # Webpack configuration
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3rd party libraries
|
||||||
|
|
||||||
|
- [json-schema-to-ts](https://github.com/ThomasAribart/json-schema-to-ts) - uses JSON-Schema definitions used by API Gateway for HTTP request validation to statically generate TypeScript types in your lambda's handler code base
|
||||||
|
- [middy](https://github.com/middyjs/middy) - middleware engine for Node.Js lambda. This template uses [http-json-body-parser](https://github.com/middyjs/middy/tree/master/packages/http-json-body-parser) to convert API Gateway `event.body` property, originally passed as a stringified JSON, to its corresponding parsed object
|
||||||
|
- [@serverless/typescript](https://github.com/serverless/typescript) - provides up-to-date TypeScript definitions for your `serverless.ts` service file
|
||||||
|
|
||||||
|
### Advanced usage
|
||||||
|
|
||||||
|
Any tsconfig.json can be used, but if you do, set the environment variable `TS_NODE_CONFIG` for building the application, eg `TS_NODE_CONFIG=./tsconfig.app.json npx serverless webpack`
|
30
lambdas/package.json
Normal file
30
lambdas/package.json
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
{
|
||||||
|
"name": "lambdas",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "Serverless aws-nodejs-typescript template",
|
||||||
|
"main": "serverless.ts",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=14.15.0"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@middy/core": "^3.4.0",
|
||||||
|
"@middy/http-json-body-parser": "^3.4.0"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@serverless/typescript": "^3.0.0",
|
||||||
|
"@types/aws-lambda": "^8.10.71",
|
||||||
|
"@types/node": "^14.14.25",
|
||||||
|
"esbuild": "^0.14.11",
|
||||||
|
"json-schema-to-ts": "^1.5.0",
|
||||||
|
"serverless": "^3.0.0",
|
||||||
|
"serverless-esbuild": "^1.23.3",
|
||||||
|
"ts-node": "^10.4.0",
|
||||||
|
"tsconfig-paths": "^3.9.0",
|
||||||
|
"typescript": "^4.1.3"
|
||||||
|
},
|
||||||
|
"author": "The serverless webpack authors (https://github.com/elastic-coders/serverless-webpack)",
|
||||||
|
"license": "MIT"
|
||||||
|
}
|
38
lambdas/serverless.ts
Normal file
38
lambdas/serverless.ts
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
import type { AWS } from '@serverless/typescript';
|
||||||
|
|
||||||
|
import hello from '@functions/hello';
|
||||||
|
|
||||||
|
const serverlessConfiguration: AWS = {
|
||||||
|
service: 'lambdas',
|
||||||
|
frameworkVersion: '3',
|
||||||
|
plugins: ['serverless-esbuild'],
|
||||||
|
provider: {
|
||||||
|
name: 'aws',
|
||||||
|
runtime: 'nodejs14.x',
|
||||||
|
apiGateway: {
|
||||||
|
minimumCompressionSize: 1024,
|
||||||
|
shouldStartNameWithService: true,
|
||||||
|
},
|
||||||
|
environment: {
|
||||||
|
AWS_NODEJS_CONNECTION_REUSE_ENABLED: '1',
|
||||||
|
NODE_OPTIONS: '--enable-source-maps --stack-trace-limit=1000',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
// import the function via paths
|
||||||
|
functions: { hello },
|
||||||
|
package: { individually: true },
|
||||||
|
custom: {
|
||||||
|
esbuild: {
|
||||||
|
bundle: true,
|
||||||
|
minify: false,
|
||||||
|
sourcemap: true,
|
||||||
|
exclude: ['aws-sdk'],
|
||||||
|
target: 'node14',
|
||||||
|
define: { 'require.resolve': undefined },
|
||||||
|
platform: 'node',
|
||||||
|
concurrency: 10,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = serverlessConfiguration;
|
14
lambdas/src/functions/hello/handler.ts
Normal file
14
lambdas/src/functions/hello/handler.ts
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
import type { ValidatedEventAPIGatewayProxyEvent } from '@libs/api-gateway';
|
||||||
|
import { formatJSONResponse } from '@libs/api-gateway';
|
||||||
|
import { middyfy } from '@libs/lambda';
|
||||||
|
|
||||||
|
import schema from './schema';
|
||||||
|
|
||||||
|
const hello: ValidatedEventAPIGatewayProxyEvent<typeof schema> = async (event) => {
|
||||||
|
return formatJSONResponse({
|
||||||
|
message: `Hello ${event.body.name}, welcome to the exciting Serverless world!`,
|
||||||
|
event,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export const main = middyfy(hello);
|
19
lambdas/src/functions/hello/index.ts
Normal file
19
lambdas/src/functions/hello/index.ts
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
import schema from './schema';
|
||||||
|
import { handlerPath } from '@libs/handler-resolver';
|
||||||
|
|
||||||
|
export default {
|
||||||
|
handler: `${handlerPath(__dirname)}/handler.main`,
|
||||||
|
events: [
|
||||||
|
{
|
||||||
|
http: {
|
||||||
|
method: 'post',
|
||||||
|
path: 'hello',
|
||||||
|
request: {
|
||||||
|
schemas: {
|
||||||
|
'application/json': schema,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
6
lambdas/src/functions/hello/mock.json
Normal file
6
lambdas/src/functions/hello/mock.json
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
{
|
||||||
|
"headers": {
|
||||||
|
"Content-Type": "application/json"
|
||||||
|
},
|
||||||
|
"body": "{\"name\": \"Frederic\"}"
|
||||||
|
}
|
7
lambdas/src/functions/hello/schema.ts
Normal file
7
lambdas/src/functions/hello/schema.ts
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
export default {
|
||||||
|
type: "object",
|
||||||
|
properties: {
|
||||||
|
name: { type: 'string' }
|
||||||
|
},
|
||||||
|
required: ['name']
|
||||||
|
} as const;
|
1
lambdas/src/functions/index.ts
Normal file
1
lambdas/src/functions/index.ts
Normal file
|
@ -0,0 +1 @@
|
||||||
|
export { default as hello } from './hello';
|
12
lambdas/src/libs/api-gateway.ts
Normal file
12
lambdas/src/libs/api-gateway.ts
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
import type { APIGatewayProxyEvent, APIGatewayProxyResult, Handler } from "aws-lambda"
|
||||||
|
import type { FromSchema } from "json-schema-to-ts";
|
||||||
|
|
||||||
|
type ValidatedAPIGatewayProxyEvent<S> = Omit<APIGatewayProxyEvent, 'body'> & { body: FromSchema<S> }
|
||||||
|
export type ValidatedEventAPIGatewayProxyEvent<S> = Handler<ValidatedAPIGatewayProxyEvent<S>, APIGatewayProxyResult>
|
||||||
|
|
||||||
|
export const formatJSONResponse = (response: Record<string, unknown>) => {
|
||||||
|
return {
|
||||||
|
statusCode: 200,
|
||||||
|
body: JSON.stringify(response)
|
||||||
|
}
|
||||||
|
}
|
3
lambdas/src/libs/handler-resolver.ts
Normal file
3
lambdas/src/libs/handler-resolver.ts
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
export const handlerPath = (context: string) => {
|
||||||
|
return `${context.split(process.cwd())[1].substring(1).replace(/\\/g, '/')}`
|
||||||
|
};
|
6
lambdas/src/libs/lambda.ts
Normal file
6
lambdas/src/libs/lambda.ts
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
import middy from "@middy/core"
|
||||||
|
import middyJsonBodyParser from "@middy/http-json-body-parser"
|
||||||
|
|
||||||
|
export const middyfy = (handler) => {
|
||||||
|
return middy(handler).use(middyJsonBodyParser())
|
||||||
|
}
|
24
lambdas/tsconfig.json
Normal file
24
lambdas/tsconfig.json
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
{
|
||||||
|
"extends": "./tsconfig.paths.json",
|
||||||
|
"compilerOptions": {
|
||||||
|
"lib": ["ESNext"],
|
||||||
|
"moduleResolution": "node",
|
||||||
|
"noUnusedLocals": true,
|
||||||
|
"noUnusedParameters": true,
|
||||||
|
"removeComments": true,
|
||||||
|
"sourceMap": true,
|
||||||
|
"target": "ES2020",
|
||||||
|
"outDir": "lib"
|
||||||
|
},
|
||||||
|
"include": ["src/**/*.ts", "serverless.ts"],
|
||||||
|
"exclude": [
|
||||||
|
"node_modules/**/*",
|
||||||
|
".serverless/**/*",
|
||||||
|
".webpack/**/*",
|
||||||
|
"_warmup/**/*",
|
||||||
|
".vscode/**/*"
|
||||||
|
],
|
||||||
|
"ts-node": {
|
||||||
|
"require": ["tsconfig-paths/register"]
|
||||||
|
}
|
||||||
|
}
|
9
lambdas/tsconfig.paths.json
Normal file
9
lambdas/tsconfig.paths.json
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"baseUrl": ".",
|
||||||
|
"paths": {
|
||||||
|
"@functions/*": ["src/functions/*"],
|
||||||
|
"@libs/*": ["src/libs/*"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3472
lambdas/yarn.lock
Normal file
3472
lambdas/yarn.lock
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue