Skip to content

Instantly share code, notes, and snippets.

Avatar
⚒️
Hacking Cloud Foundry

Lakshman Diwaakar lakshmantgld

⚒️
Hacking Cloud Foundry
View GitHub Profile
View how-to-setup-lambda-to-talk-to-internet-and-vpc.md

How to setup AWS lambda function to talk to the internet and VPC

I'm going to walk you through the steps for setting up a AWS Lambda to talk to the internet and a VPC. Let's dive in.

So it might be really unintuitive at first but lambda functions have three states.

  1. No VPC, where it can talk openly to the web, but can't talk to any of your AWS services.
  2. VPC, the default setting where the lambda function can talk to your AWS services but can't talk to the web.
  3. VPC with NAT, The best of both worlds, AWS services and web.
View serverless.yml
service: stakinet-wakeup
package:
exclude:
provider:
name: aws
runtime: nodejs6.10
cfLogs: true
timeout: 20
View handler.js
if (!global._babelPolyfill) require('babel-polyfill');
import { Promise as bbPromise } from 'bluebird';
import AWS from 'aws-sdk';
const lambda = new AWS.Lambda({
region: 'ap-northeast-1'
});
module.exports.wakeup = (event, context, callback) => {
@lakshmantgld
lakshmantgld / cloudSettings
Last active Oct 23, 2017
Visual Studio Code Settings Sync Gist
View cloudSettings
{"lastUpload":"2017-10-23T11:31:35.410Z","extensionVersion":"v2.8.3"}
View try
╔════════════════╦═══════════════════════════════════════════════════════════════╦════════════════════════════════════════════════════════════════════════════╗
║ Features ║ lambda-proxy ║ lambda ║
╠════════════════╬═══════════════════════════════════════════════════════════════╬════════════════════════════════════════════════════════════════════════════╣
║ General ║ This is a simple, but powerful integration. All the request ║ This is complex, but offers more control over transmission data. ║
║ Description ║ and response to the APIGateway URL is forwarded ║ The request and response can be modified before it is sent to ║
║ ║ straight to the Lambda. ║ lambda. This can be done by mapping templates which transforms ║
║ ║ i.e No modifications to the request(query par
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. ║
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 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 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 lambda-proxy-serverless.yml
functions:
hello:
handler: handler.hello
events:
- http:
path: hello
method: get