|
1 |
| -# replace this |
| 1 | +# Serverless DNA Constructs |
| 2 | + |
| 3 | +A library containing a set of re-usable AWS CDK constructs. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +npm install @serverless-dna/constructs |
| 9 | +``` |
| 10 | + |
| 11 | +## The Constructs |
| 12 | + |
| 13 | +### SocketApi |
| 14 | + |
| 15 | +This construct creates an AWS WebSocket API with routes that are handled by AWS Lambda functions. A WebSocket can only exist when there are one or more valid integrations defined. If you do not define any integrations the WebSocket API will not be deployed. |
| 16 | + |
| 17 | +#### Getting Started |
| 18 | + |
| 19 | +The SocketAPI accepts an array of route definitions of type **ISocketFunction**. |
| 20 | + |
| 21 | +```typescript |
| 22 | +export interface ISocketFunction { |
| 23 | + readonly route: string; |
| 24 | + readonly type?: SocketApiIntegrationType; |
| 25 | + readonly integration: Function; |
| 26 | + readonly returnResponse?: boolean; |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +To create a WebSocket API you will need to provide a Lambda handler construct to execute your WebSocket API action. The following example shows how to setup a WebSocket API with a **test** action route which is handled by the **test-func** lambda handler. |
| 31 | + |
| 32 | +```typescript |
| 33 | +import { SocketApi } from '@serverless-dna/constructs'; |
| 34 | +import { Duration, Stack, StackProps } from 'aws-cdk-lib'; |
| 35 | +import { Runtime } from 'aws-cdk-lib/aws-lambda'; |
| 36 | +import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs'; |
| 37 | +import { Construct } from 'constructs'; |
| 38 | +import { IntegrationHandlers } from './integrations'; |
| 39 | + |
| 40 | +export class ApplicationStack extends Stack { |
| 41 | + constructor(scope: Construct, id: string, props?: StackProps) { |
| 42 | + super(scope, id, props); |
| 43 | + |
| 44 | + const handler = new NodejsFunction(this, `test-func`, { |
| 45 | + entry: `${__dirname}/integrations.ts`, |
| 46 | + handler: IntegrationHandlers.noOp, |
| 47 | + runtime: Runtime.NODEJS_18_X, |
| 48 | + timeout: Duration.seconds(3), |
| 49 | + }); |
| 50 | + |
| 51 | + new SocketApi(this, 'socket-api', { |
| 52 | + routes: [{ route: 'test', integration: handler, returnResponse: true }], |
| 53 | + }); |
| 54 | + } |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +#### SocketTasks |
| 59 | + |
| 60 | +This construct inherits from the SocketAPI and creates a complete Asycnrhonouse Task Execution framework using WebSockets and AWS Lambda for running long-running tasks. |
| 61 | + |
| 62 | +#### Getting Started |
| 63 | + |
| 64 | +```typescript |
| 65 | +import { SocketTasks } from '@serverless-dna/constructs'; |
| 66 | +import { Duration, Stack, StackProps } from 'aws-cdk-lib'; |
| 67 | +import { Runtime } from 'aws-cdk-lib/aws-lambda'; |
| 68 | +import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs'; |
| 69 | +import { Construct } from 'constructs'; |
| 70 | +import { IntegrationHandlers } from './integrations'; |
| 71 | + |
| 72 | +export class ApplicationStack extends Stack { |
| 73 | + constructor(scope: Construct, id: string, props?: StackProps) { |
| 74 | + super(scope, id, props); |
| 75 | + |
| 76 | + const handlerOne = new NodejsFunction(this, `handler-one`, { |
| 77 | + entry: `${__dirname}/integrations.ts`, |
| 78 | + handler: IntegrationHandlers.taskHandler, |
| 79 | + runtime: Runtime.NODEJS_18_X, |
| 80 | + timeout: Duration.seconds(3), |
| 81 | + }); |
| 82 | + |
| 83 | + const handlerTwo = new NodejsFunction(this, `handler-two`, { |
| 84 | + entry: `${__dirname}/integrations.ts`, |
| 85 | + handler: IntegrationHandlers.taskFail, |
| 86 | + runtime: Runtime.NODEJS_18_X, |
| 87 | + timeout: Duration.seconds(3), |
| 88 | + }); |
| 89 | + |
| 90 | + new SocketTasks(this, `tasks`, { |
| 91 | + taskFunctions: [ |
| 92 | + { |
| 93 | + type: ['task-type'], |
| 94 | + func: handlerOne, |
| 95 | + }, |
| 96 | + { |
| 97 | + type: ['task-type-2'], |
| 98 | + func: handlerTwo, |
| 99 | + }, |
| 100 | + ], |
| 101 | + }); |
| 102 | + } |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +## Contributing |
| 107 | + |
| 108 | +This project uses Projen to construct the project. Altering project dependencies or configurations is all done in the .projenrc.ts file. Please refer to the [Projen Documentation](https://projen.io/) for details on Projen. |
| 109 | + |
| 110 | + |
0 commit comments