-
Notifications
You must be signed in to change notification settings - Fork 672
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add lambda edge support and example
- Loading branch information
1 parent
2290128
commit 230c9c5
Showing
17 changed files
with
10,156 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] | ||
} | ||
} | ||
} | ||
} | ||
] | ||
} |
Oops, something went wrong.