Skip to content

Instantly share code, notes, and snippets.

@ultimagriever
Created May 2, 2019 21:35
Show Gist options
  • Save ultimagriever/c6252ad196b8ae2743ca59b84cc228a5 to your computer and use it in GitHub Desktop.
Save ultimagriever/c6252ad196b8ae2743ca59b84cc228a5 to your computer and use it in GitHub Desktop.
Lambda Node.js Example With Swagger Specification
exports.handler = async function (event) {
// event variables looks like this:
// {
// "body": "eyJ0ZXN0IjoiYm9keSJ9",
// "resource": "/{proxy+}",
// "path": "/path/to/resource",
// "httpMethod": "POST",
// "isBase64Encoded": true,
// "queryStringParameters": {
// "foo": "bar"
// },
// "pathParameters": {
// "proxy": "/path/to/resource"
// },
// "stageVariables": {
// "baz": "qux"
// },
// "headers": {
// "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
// "Accept-Encoding": "gzip, deflate, sdch",
// "Accept-Language": "en-US,en;q=0.8",
// "Cache-Control": "max-age=0",
// "CloudFront-Forwarded-Proto": "https",
// "CloudFront-Is-Desktop-Viewer": "true",
// "CloudFront-Is-Mobile-Viewer": "false",
// "CloudFront-Is-SmartTV-Viewer": "false",
// "CloudFront-Is-Tablet-Viewer": "false",
// "CloudFront-Viewer-Country": "US",
// "Host": "1234567890.execute-api.us-east-1.amazonaws.com",
// "Upgrade-Insecure-Requests": "1",
// "User-Agent": "Custom User Agent String",
// "Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)",
// "X-Amz-Cf-Id": "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==",
// "X-Forwarded-For": "127.0.0.1, 127.0.0.2",
// "X-Forwarded-Port": "443",
// "X-Forwarded-Proto": "https"
// },
// "requestContext": {
// "accountId": "123456789012",
// "resourceId": "123456",
// "stage": "prod",
// "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef",
// "requestTime": "09/Apr/2015:12:34:56 +0000",
// "requestTimeEpoch": 1428582896000,
// "identity": {
// "cognitoIdentityPoolId": null,
// "accountId": null,
// "cognitoIdentityId": null,
// "caller": null,
// "accessKey": null,
// "sourceIp": "127.0.0.1",
// "cognitoAuthenticationType": null,
// "cognitoAuthenticationProvider": null,
// "userArn": null,
// "userAgent": "Custom User Agent String",
// "user": null
// },
// "path": "/prod/path/to/resource",
// "resourcePath": "/{proxy+}",
// "httpMethod": "POST",
// "apiId": "1234567890",
// "protocol": "HTTP/1.1"
// }
// }
// Function must return an object like this
// If API responds with a JSON, headers prop is optional
return {
statusCode: 200, // int - HTTP Status Code
body: JSON.stringify({ myProperty: "myValue" }), // string - Response Body
headers: {
'Content-Type': 'application/json'
}
};
}
AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Resources:
MyLambda:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
CodeUri: .
Runtime: nodejs8.10
Environment:
Variables:
VARIABLE: value
# Add any external param here, such as endpoints, references to other resources etc.
# that this function should be aware of.
Events:
Api:
Type: Api
Properties:
Method: get
Path: /mypath
RestApiId: !Ref MyRestApi
MyRestApi:
Type: AWS::Serverless::Api
Properties:
StageName: v1
DefinitionBody:
openapi: '3.0.1'
info:
version: 1.0.0
title: ContactOne
paths:
/mypath:
get:
summary: My GET endpoint
parameters:
- in: query
name: id
description: Some ID
schema:
type: string
responses:
"200":
$ref: '#/components/responses/default'
x-amazon-apigateway-integration:
httpMethod: POST
type: aws_proxy
uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${MyLambda.Arn}/invocations
security:
- apiKey: []
components:
responses:
default:
description: OK
content:
application/json:
schema:
$ref: "#/components/schemas/MySchema"
schemas:
MySchema:
type: object
properties:
myProperty:
type: string
securitySchemes:
apiKey:
type: apiKey
name: x-api-key
in: header
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment