In addition to a basic Lambda function and Express server, the example directory includes a Swagger file, CloudFormation template with Serverless Application Model (SAM), and helper scripts to help you set up and manage your application.
This guide assumes you have already set up an AWS account and have the latest version of the AWS CLI installed.
- From your preferred project directory:
git clone https://github.com/awslabs/aws-serverless-express.git && cd aws-serverless-express/examples/basic-starter. - Run
npm run config -- --account-id="<accountId>" --bucket-name="<bucketName>" [--region="<region>" --function-name="<functionName>"]to configure the example, eg.npm run config -- --account-id="123456789012" --bucket-name="my-unique-bucket". This modifiespackage.json,simple-proxy-api.yamlandcloudformation.yamlwith your account ID, bucket, region and function name (region defaults tous-east-1and function name defaults toAwsServerlessExpressFunction). If the bucket you specify does not yet exist, the next step will create it for you. This step modifies the existing files in-place; if you wish to make changes to these settings, you will need to modifypackage.json,simple-proxy-api.yamlandcloudformation.yamlmanually. - Run
npm run setup(Windows users:npm run win-setup) - this installs the node dependencies, creates an S3 bucket (if it does not already exist), packages and deploys your serverless Express application to AWS Lambda, and creates an API Gateway proxy API. - After the setup command completes, open the AWS CloudFormation console https://console.aws.amazon.com/cloudformation/home and switch to the region you specified. Select the
AwsServerlessExpressStackstack, then click theApiUrlvalue under the Outputs section - this will open a new page with your running API. The API index lists the resources available in the example Express server (app.js), along with examplecurlcommands.
See the sections below for details on how to migrate an existing (or create a new) Node.js project based on this example. If you would prefer to delete AWS assets that were just created, simply run npm run delete-stack to delete the CloudFormation Stack, including the API and Lambda Function. If you specified a new bucket in the config command for step 1 and want to delete that bucket, run npm run delete-bucket.
To use this example as a base for a new Node.js project:
- Copy the files in the
examples/basic-starterdirectory into a new project directory (cp -r ./examples/basic-starter ~/projects/my-new-node-project). If you have not already done so, follow the steps for running the example (you may want to first modify some of the resource names to something more project-specific, eg. the CloudFormation stack, Lambda function, and API Gateway API). - After making updates to
app.js, simply runnpm run package-deploy(Windows users:npm run win-package-deploy).
To migrate an existing Node server:
- Copy the following files from the
exampledirectory:api-gateway-event.json,cloudformation.yaml,lambda.js, andsimple-proxy-api.yaml. Additionally, copy thescriptsandconfigsections ofexample/package.jsoninto your existingpackage.json- this includes many helpful commands to manage your AWS serverless assets and perform basic local simulation of API Gateway and Lambda. If you have not already done so, follow the steps for running the example (be sure to copy overscripts/configure.js. You may want to first modify some of the resource names to something more project-specific, eg. the CloudFormation stack, Lambda function, and API Gateway API). - From your existing project directory, run
npm install --save aws-serverless-express. - Modify
lambda.jsto import your own server configuration (eg. changerequire('./app')torequire('./server')). You will need to ensure you export your app configuration from the necessary file (eg.module.exports = app). This library takes your app configuration and listens on a Unix Domain Socket for you, so you can remove your call toapp.listen()(if you have aserver.listencallback, you can provide it as the second parameter in theawsServerlessExpress.createServermethod). - Modify the
CodeUriproperty of the Lambda function resource incloudformation.yamlto point to your application directory (e.g.CodeUri: ./src). If you are using a build tool (e.g. Gulp, Grunt, Webpack, Rollup, etc.), you will instead want to point to your build output directory. - Run
npm run package-deploy(Windows users:npm run win-package-deploy) to package and deploy your application.
To perform a basic, local simulation of API Gateway and Lambda with your Node server, update api-gateway-event.json with some values that are valid for your server (httpMethod, path, body etc.) and run npm run local. AWS Lambda uses NodeJS 4.3 LTS, and it is recommended to use the same version for testing purposes.
If you need to make modifications to your API Gateway API, modify simple-proxy-api.yaml and run npm run package-deploy. If your API requires CORS, be sure to modify the two options methods defined in the Swagger file, otherwise you can safely remove them. To modify your other AWS assets, make your changes to cloudformation.yaml and run npm run package-deploy. Alternatively, you can manage these assets via the AWS console.
This example was written against Node.js version 6.10