Skip to content

Commit

Permalink
feat: add lambda edge support and example
Browse files Browse the repository at this point in the history
  • Loading branch information
brettstack committed Jun 27, 2019
1 parent 2290128 commit 230c9c5
Show file tree
Hide file tree
Showing 17 changed files with 10,156 additions and 11 deletions.
2 changes: 1 addition & 1 deletion examples/basic-starter/scripts/local.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const lambdaFunction = require('../lambda.js')
const lambdaFunction = require('../src/lambda.js')
const apiGatewayEvent = require('../api-gateway-event.json')

const context = {
Expand Down
4 changes: 4 additions & 0 deletions examples/lambda-edge/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
lambda-invoke-response.json
sam-template.packaged.yaml
aws-serverless-express-*.tgz
dist/
42 changes: 42 additions & 0 deletions examples/lambda-edge/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Lambda@Edge + aws-serverless-express Example

In addition to a basic Lambda function and Express server, this example includes a [Serverless Application Model (SAM)](https://github.com/awslabs/serverless-application-model) template, and helper scripts to help you set up and manage your application.

## Steps for running the example

This guide assumes you have already [set up an AWS account](http://docs.aws.amazon.com/AmazonSimpleDB/latest/DeveloperGuide/AboutAWSAccounts.html) and have the latest version of the [AWS CLI](https://aws.amazon.com/cli/) installed.

1. From your preferred project directory: `git clone https://github.com/awslabs/aws-serverless-express.git && cd aws-serverless-express/examples/basic-starter`.
2. Update the `config` section of `package.json` with your `s3BucketName` and `region` (optionally, change the `cloudFormationStackName`). If the bucket you specify does not yet exist, the next step will create it for you.
3. Run `npm run 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.
4. 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 `ServerlessExpressEdge` stack (or the stack name you specified for `cloudFormationStackName`), then click the `ApiUrl` value 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 example `curl` commands.
5. (optional) To enable the `invoke-lambda` `package.json` `script`: copy the `LambdaFunctionName` from the CloudFormation Outputs and paste it into the `package.json` `config`'s `functionName` property. Run `npm run invoke-lambda` to invoke the Lambda Function with the payload specified in `api-gateway-event.json`.

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`.

## Edge Limitations

Lambda@Edge has several limitations that you should be aware of. These include:

* 1MB code size. As a result, you'll need to use a build tool like Webpack (used in this example). This minimal example with sourcemaps comes in at ~0.2MB after bundling/minification.
* 40KB Response body size (53.2KB for base64 encoded body).
* 128MB Function RAM allocation.

* https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-requirements-limits.html
* https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/cloudfront-limits.html

## Development

To update this example against the latest local changes to aws-serverless-express:

```bash
npm pack ../..
npm install ./aws-serverless-express-3.3.5.tgz
npm install
npm run build
npm run local
```

## TODO:

Test using origin-request event. origin-* events have fewer limitations, however, we likely lose most advantages of edge-compute since it needs to go to the origin on each request. We will also need to force the object to never be cached.
5 changes: 5 additions & 0 deletions examples/lambda-edge/app.local.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const app = require('./app')
const port = 3000

app.listen(port)
console.info(`listening on http://localhost:${port}`)
42 changes: 42 additions & 0 deletions examples/lambda-edge/lambda-edge-event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"Records": [
{
"cf": {
"config": {
"distributionId": "EXAMPLE"
},
"request": {
"headers": {
"host": [
{
"key": "Host",
"value": "d123.cf.net"
}
],
"user-name": [
{
"key": "User-Name",
"value": "CloudFront"
}
]
},
"clientIp": "2001:cdba::3257:9652",
"uri": "/users",
"method": "GET"
},
"response": {
"status": "200",
"statusDescription": "OK",
"headers": {
"x-cache": [
{
"key": "X-Cache",
"value": "Hello from Cloudfront"
}
]
}
}
}
}
]
}
Loading

0 comments on commit 230c9c5

Please sign in to comment.