Skip to content

Instantly share code, notes, and snippets.

Avatar
⚒️
Hacking Cloud Foundry

Lakshman Diwaakar lakshmantgld

⚒️
Hacking Cloud Foundry
View GitHub Profile
@lakshmantgld
lakshmantgld / handler.js
Created Feb 29, 2020
Lambda snippet to upload file to S3
View handler.js
'use strict';
const AWS = require('aws-sdk');
// Set the Region
AWS.config.update({
accessKeyId: '***********',
secretAccessKey: '*************',
region: 'us-east-1',
signatureVersion: 'v4'
View lambda-integration-serverless.yml
functions:
hello:
handler: handler.hello
events:
- http:
path: users
method: get
integration: lambda
request:
template:
View error-lambda-integration.js
module.exports.hello = (event, context, callback) => {
const errorResponse = {
message: "Error! Dude You sent the wrong parameters"
};
// Use JSON.Stringify(), else Integration Response
// cannot read your error message.
// If not used, the Integration Response will read
// the response as [Object Object] which will make it
// impossible to detect the status code.
View success-lambda-integration.js
module.exports.hello = (event, context, callback) => {
const response = {
message: "Success!! You invoked a sleeping Lambda"
};
callback(null, response);
};
View lambda-proxy-serverless.yml
functions:
hello:
handler: handler.hello
events:
- http:
path: hello
method: get
View error-lambda-proxy.js
module.exports.hello = (event, context, callback) => {
const errorResponse = {
statusCode: 400,
headers: {
"x-custom-header" : "my custom header value"
},
body: JSON.stringify({
message: 'Bad request Dude',
}),
};
View success-lambda-proxy.js
module.exports.hello = (event, context, callback) => {
const response = {
statusCode: 200,
headers: {
"x-custom-header" : "my custom header value"
},
body: JSON.stringify({
message: 'Go Serverless v1.0! Your function executed successfully!',
input: event,
}),
View Dis-Advantages
╔════════════════════════════════════════════════╦═══════════════════════════════════════════════════════╗
║ Lambda-Proxy ║ Lambda ║
╠════════════════════════════════════════════════╬═══════════════════════════════════════════════════════╣
║ 1. Prone to human errors, as status code ║ 1. Involves lot of work in setting up the integration ║
║ and response message will be in code. ║ request template, response template, status code ║
║ This is subjective, but a point to stay in ║ pattern. All these need some extra knowledge on ║
║ the dis-advantage. ║ VTL(Velocity Template Language) and JSON Path. ║
║ ║ ║
║ 2. Everything rests in the code. Headers, ║ ║
║ status codes, messages are hidden in the ║
View Advantages
╔══════════════════════════════════════════════════════╦═════════════════════════════════════════════════════════════════════════╗
║ Lambda-Proxy ║ Lambda ║
╠══════════════════════════════════════════════════════╬═════════════════════════════════════════════════════════════════════════╣
║ 1. Very Easy to setup. Status code and the response ║ 1. It completely decouples the APIGateway from Lambda's request ║
║ message are in lambda's control. ║ and response payload, its headers and statusCodes. Thus, you ║
║ ║ have more control over the workflow. ║
║ ║ ║
║ 2. Easy for rapid prototyping, as you don't want ║ 2. Provides a procedural way of defining various s
View Request & Response
╔════════════════════════════════════════════════╦════════════════════════════════════════════════╗
║ Lambda-Proxy ║ Lambda ║
╠════════════════════════════════════════════════╬════════════════════════════════════════════════╣
║ - Request from the client are sent directly to ║ - Request and Response can be altered/modified ║
║ lambda. In the same way, responses are ║ when it is sent to and from the lambda. ║
║ sent directly from the lambda. API Gateway ║ API Gateway has full control of the request ║
║ has no part in the transmission data. ║ and responses. ║
║ ║ ║
║ - Status code of the response message such ║ - Status Code of the response message are set ║
║ as 2XX, 4XX & 5XX are set by the lambda. ║ by the API Gateway. ║