|
| 1 | +# Serving Dynamic HTML via API Gateway Example |
| 2 | + |
| 3 | +This example illustrates how to hookup an API Gateway endpoint to a Lambda function to render HTML on a `GET` request. |
| 4 | + |
| 5 | +## Use-cases |
| 6 | + |
| 7 | +- Landing pages for marketing activities |
| 8 | +- Single use dynamic webpages |
| 9 | + |
| 10 | +## How it works |
| 11 | + |
| 12 | +Instead of returning the default `json` from a request, you can display custom dynamic HTML by setting the `Content-Type` header to `text/html`. |
| 13 | + |
| 14 | +```js |
| 15 | +const response = { |
| 16 | + statusCode: 200, |
| 17 | + headers: { |
| 18 | + 'Content-Type': 'text/html', |
| 19 | + }, |
| 20 | + body: html, |
| 21 | +}; |
| 22 | +// callback will send HTML back |
| 23 | +callback(null, response); |
| 24 | +``` |
| 25 | + |
| 26 | +## Deploy |
| 27 | + |
| 28 | +In order to deploy the you endpoint simply run |
| 29 | + |
| 30 | +```bash |
| 31 | +serverless deploy |
| 32 | +``` |
| 33 | + |
| 34 | +The expected result should be similar to: |
| 35 | + |
| 36 | +```bash |
| 37 | +Serverless: Creating Stack... |
| 38 | +Serverless: Checking Stack create progress... |
| 39 | +..... |
| 40 | +Serverless: Stack create finished... |
| 41 | +Serverless: Packaging service... |
| 42 | +Serverless: Uploading CloudFormation file to S3... |
| 43 | +Serverless: Uploading service .zip file to S3 (1.01 KB)... |
| 44 | +Serverless: Updating Stack... |
| 45 | +Serverless: Checking Stack update progress... |
| 46 | +........................... |
| 47 | +Serverless: Stack update finished... |
| 48 | + |
| 49 | +Service Information |
| 50 | +service: serve-dynamic-html-via-http-endpoint |
| 51 | +stage: dev |
| 52 | +region: us-east-1 |
| 53 | +api keys: |
| 54 | + None |
| 55 | +endpoints: |
| 56 | + GET - https://nzkl1kas89.execute-api.us-east-1.amazonaws.com/dev/landing-page |
| 57 | +functions: |
| 58 | + serve-dynamic-html-via-http-endpoint-dev-landingPage: arn:aws:lambda:us-east-1:377024778620:function:serve-dynamic-html-via-http-endpoint-dev-landingPage |
| 59 | +``` |
| 60 | + |
| 61 | +## Usage |
| 62 | + |
| 63 | +You can now send an HTTP request directly to the endpoint using a tool like curl |
| 64 | + |
| 65 | +```bash |
| 66 | +curl https://nzkl1kas89.execute-api.us-east-1.amazonaws.com/dev/landing-page?name=Nik%20Graf |
| 67 | +``` |
| 68 | + |
| 69 | +The expected result should be similar to: |
| 70 | + |
| 71 | +```bash |
| 72 | +<html> |
| 73 | + <style> |
| 74 | + h1 { color: #73757d; } |
| 75 | + </style> |
| 76 | + <body> |
| 77 | + <h1>Landing Page</h1> |
| 78 | + <p>Hey Nik Graf!</p> |
| 79 | + </body> |
| 80 | +</html>% |
| 81 | +``` |
| 82 | +
|
| 83 | +Of course you can visit the URL in your browser and this is how it should look like: |
| 84 | +
|
| 85 | + |
| 86 | +
|
| 87 | +To greet a specific person provide attach the query parameter with the name of that person e.g. `?name=Nik%20Graf`. The response should now contain the provided name: |
| 88 | +
|
| 89 | + |
| 90 | +
|
| 91 | +## Scaling |
| 92 | +
|
| 93 | +By default, AWS Lambda limits the total concurrent executions across all functions within a given region to 100. The default limit is a safety limit that protects you from costs due to potential runaway or recursive functions during initial development and testing. To increase this limit above the default, follow the steps in [To request a limit increase for concurrent executions](http://docs.aws.amazon.com/lambda/latest/dg/concurrent-executions.html#increase-concurrent-executions-limit). |
0 commit comments