|
| 1 | +# Example |
| 2 | + |
| 3 | +In addition to a basic Lambda function and Express server, the `example` |
| 4 | +directory includes a [Swagger file](http://swagger.io/specification/), |
| 5 | +[CloudFormation |
| 6 | +template](https://aws.amazon.com/cloudformation/aws-cloudformation-templates/) |
| 7 | +with [Serverless Application Model |
| 8 | +(SAM)](https://github.com/awslabs/serverless-application-model), and helper |
| 9 | +scripts to help you set up and manage your application. |
| 10 | + |
| 11 | +## Steps for running the example |
| 12 | + |
| 13 | +This guide assumes you have already [set up an AWS |
| 14 | +account](http://docs.aws.amazon.com/AmazonSimpleDB/latest/DeveloperGuide/AboutAWSAccounts.html) |
| 15 | +and have the latest version of the [AWS CLI](https://aws.amazon.com/cli/) and |
| 16 | +[AWS SAM](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) |
| 17 | +installed. |
| 18 | + |
| 19 | +1. From your preferred project directory: |
| 20 | + ```sh |
| 21 | + git clone https://github.com/vendia/serverless-express.git && cd serverless-express/examples/basic-starter-api-gateway-v2-typescript |
| 22 | + ``` |
| 23 | +1. Update the `config` section of `package.json` with your `s3BucketName` and |
| 24 | + `region` (optionally, change the `cloudFormationStackName`). If the bucket |
| 25 | + you specify does not yet exist, the next step will create it for you. |
| 26 | +1. (optional: domain) If you want to use a custom domain name for your |
| 27 | + application/API, specify it in the `config.domain` section of `package.json`. |
| 28 | + This example assumes the domain is registered in Route53 in the same account |
| 29 | + you're deploying the example to. |
| 30 | +1. Run `npm run setup` - this installs the node dependencies, creates an S3 |
| 31 | + bucket (if it does not already exist), packages and deploys your serverless |
| 32 | + Express application to AWS Lambda, and creates an API Gateway proxy API. |
| 33 | +1. (optional: domain) If you specify a domain, the example will create an SSL |
| 34 | + Certificate via Amazon Certificate Manager; create an API Gateway Domain Name |
| 35 | + record which maps the domain to the API and Stage; and create a Route53 |
| 36 | + HostedZone and RecordSet with an A record pointing at the API Gateway Domain |
| 37 | + Name's CloudFront Distribution. |
| 38 | + 1. During deployment you should receive an email at one of the registered |
| 39 | + email addresses for the domain. Approve the SSL Certificate by clicking |
| 40 | + the link in the email. The stack creation will pause while waiting for |
| 41 | + this approval. |
| 42 | + 1. Wait for stack creation to complete and update Route53 Domain Name to use |
| 43 | + the Name Servers from the created Hosted Zone NS Record (don't include the |
| 44 | + trailing '.') via the AWS console. |
| 45 | + 1. It may take several hours before the DNS records propagate. |
| 46 | +1. After the setup command completes, open the AWS CloudFormation console |
| 47 | + https://console.aws.amazon.com/cloudformation/home and switch to the region |
| 48 | + you specified. Select the `ServerlessExpressStack` stack (or the stack name |
| 49 | + you specified for `cloudFormationStackName`), then click the `ApiUrl` value |
| 50 | + under the __Outputs__ section - this will open a new page with your running |
| 51 | + API. The API index lists the resources available in the example Express |
| 52 | + server (`app.js`), along with example `curl` commands. |
| 53 | +1. (optional) To enable the `invoke-lambda` `package.json` `script`: copy the |
| 54 | + `LambdaFunctionName` from the CloudFormation Outputs and paste it into the |
| 55 | + `package.json` `config`'s `functionName` property. |
| 56 | + Run `npm run invoke-lambda` to invoke the Lambda Function with the payload |
| 57 | + specified in `api-gateway-event.json`. |
| 58 | + |
| 59 | +See the sections below for details on how to migrate an existing (or create a |
| 60 | +new) Node.js project based on this example. If you would prefer to delete AWS |
| 61 | +assets that were just created, simply run `npm run delete-stack` to delete the |
| 62 | +CloudFormation Stack, including the API and Lambda Function. If you specified a |
| 63 | +new bucket in the `config` command for step 1 and want to delete that bucket, |
| 64 | +run `npm run delete-bucket`. |
| 65 | + |
| 66 | +## Creating or migrating a Node.js project based on the example |
| 67 | + |
| 68 | +To use this example as a base for a new Node.js project: |
| 69 | + |
| 70 | +1. Copy the files in the `examples/basic-starter-api-gateway-v2-typescript` |
| 71 | + directory into a new project directory (`cp -r |
| 72 | + ./examples/basic-starter-api-gateway-v2-typescript |
| 73 | + ~/projects/my-new-node-project`). If you have not already done so, follow the |
| 74 | + [steps for running the example](#steps-for-running-the-example) (you may want |
| 75 | + to first modify some of the resource names to something more |
| 76 | + project-specific, eg. the CloudFormation stack, Lambda function, and API |
| 77 | + Gateway API). |
| 78 | +1. After making updates to `app.js`, simply run `npm run package-deploy`. |
| 79 | + |
| 80 | +To migrate an existing Node server: |
| 81 | + |
| 82 | +1. Copy the following files from this directory: `api-gateway-event.json`, |
| 83 | + `sam-template.yaml`, and `lambda.js`. Additionally, copy the `scripts` and |
| 84 | + `config` sections of `example/package.json` into your existing |
| 85 | + `package.json` - this includes many helpful commands to manage your AWS |
| 86 | + serverless assets and perform _basic_ local simulation of API Gateway and |
| 87 | + Lambda. If you have not already done so, follow the [steps for running the |
| 88 | + example](#steps-for-running-the-example). |
| 89 | +1. From your existing project directory, run |
| 90 | + ```sh |
| 91 | + npm install --save @vendia/serverless-express |
| 92 | + ``` |
| 93 | +1. Modify `lambda.ts` to import your own server configuration (eg. change |
| 94 | + `require('./app')` to `require('./server')`). You will need to ensure you |
| 95 | + export your app configuration from the necessary file (eg. `module.exports = |
| 96 | + app`). This library takes your app configuration and listens on a Unix Domain |
| 97 | + Socket for you, so you can remove your call to `app.listen()`. |
| 98 | +1. Modify the `CodeUri` property of the Lambda function resource in |
| 99 | + `sam-template.yaml` to point to your application directory (e.g. `CodeUri: |
| 100 | + ./src`). If you are using a build tool (e.g. Gulp, Grunt, Webpack, Rollup, |
| 101 | + etc.), you will instead want to point to your build output directory. |
| 102 | +1. Run `npm run package-deploy`. |
| 103 | + |
| 104 | +To perform a basic, local simulation of API Gateway and Lambda with your Node |
| 105 | +server, update `api-gateway-event.json` with some values that are valid for your |
| 106 | +server (`httpMethod`, `path`, `body` etc.) and run `npm run local`. |
| 107 | + |
| 108 | +If you need to make modifications to your API Gateway API or other AWS |
| 109 | +resources, modify `sam-template.yaml` and run `npm run package-deploy`. |
| 110 | + |
| 111 | +## Node.js version |
| 112 | + |
| 113 | +This example was written against Node.js 12 |
| 114 | + |
| 115 | +## Development |
| 116 | + |
| 117 | +To update this example against the latest local changes to |
| 118 | +@vendia/serverless-express: |
| 119 | + |
| 120 | +```bash |
| 121 | +npm i ../.. |
| 122 | +npm run build |
| 123 | +npm run local |
| 124 | +``` |
0 commit comments