Instantly share code, notes, and snippets.

Embed
What would you like to do?

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.

I'm gonna walk you through the steps to set up number 3.

Note: This tutorial isn't exactly in order of steps, you may need to create one thing (subnet, nat, route table) then go back into the settings for something previously created and edit it to use a newly thing.

Creating Subnets

VPC Dashboard > Subnets

This is what I had to start with, my existing vpc that I wanted to connect to already had 4 subnets. Here I noticed I had a couple of subnets already set up. Below is a totally fake ip I pulled from the internet. But the patten of increments of 16 is recreated here.

Note: DO NOT use 131.179.0.0/16 it's just an example.

VPC CIDR
vpc-████████ (131.179.0.0/16) 131.179.0.0/20
vpc-████████ (131.179.0.0/16) 131.179.16.0/20
vpc-████████ (131.179.0.0/16) 131.179.32.0/20
vpc-████████ (131.179.0.0/16) 131.179.48.0/20

Here I created three four new subnets.

VPC CIDR name
vpc-████████ (131.179.0.0/16) 131.179.64.0/20 lambda-subnet-point-to-nat-1
vpc-████████ (131.179.0.0/16) 131.179.80.0/20 lambda-subnet-point-to-nat-2
vpc-████████ (131.179.0.0/16) 131.179.96.0/20 lambda-subnet-point-to-nat-3
vpc-████████ (131.179.0.0/16) 131.179.112.0/20 lambda-subnet-point-to-igw

Note: Here igw stands for Internet Gateway and nat stands for network address translation gateway (NAT Gateway).

Three of them will point to the nat and one points to the igw.

Let's create the Route Tables now.

Creating Route Tables

VPC Dashboard > Route Tables

Your going to want to set up two Route Tables.

One that points to your nat let's call this lambda-rt-to-nat:

Destination Target
131.179.0.0/16 local
0.0.0.0/0 nat-█████████████████

One that points to your igw let's call this lambda-rt-to-igw:

Destination Target
131.179.0.0/16 local
0.0.0.0/0 igw-████████

Your gonna want to go into each of the subnet and assign them to their corresponding route table.

subnet name route table name
lambda-subnet-point-to-nat-1 lambda-rt-to-nat
lambda-subnet-point-to-nat-2 lambda-rt-to-nat
lambda-subnet-point-to-nat-3 lambda-rt-to-nat
lambda-subnet-point-to-igw lambda-rt-to-igw

Set your lambda up

Lambda > Functions > my-function > Configuration > Advanced Settings

Now you want to set up your lambda function to use the subnets you created.

Setup your lambda to use your VPC.

VPC

vpc-████████ (131.179.0.0/16)

Here you setup lambda to use the subnets that point directly to your nat.

Subnets*

subnet name
lambda-subnet-point-to-nat-1
lambda-subnet-point-to-nat-2
lambda-subnet-point-to-nat-3

Create a NAT

VPC Dashboard > NAT Gateways > Create NAT Gateway

Your going to want click Create NAT Gateway and set the Subnet* to lambda-subnet-point-to-igw, and Create New EIP.

Fin

That should be it! Your lambda should be able to talk to both the VPS and the web through a NAT! Comment below if you need help or want to clarify anything here!

Links

Shameless SEO terms

  • amazon lambda nat
  • aws lambda vpc web
  • aws lambda rds and web
  • aws lambda rds and http request
  • lambda timeout
  • AWS lambda timeout random vpc
@scott2b

This comment has been minimized.

scott2b commented Aug 17, 2016

I am confused about why you have associated the lambda-subnet-point-to-igw subnet with the NAT gateway, rather than the 3 nat subnets

@vineus

This comment has been minimized.

vineus commented Aug 22, 2016

Thank you for the very clear tuto, works like a charm and way clearer than the doc. I used it to answer SO question here: http://stackoverflow.com/a/39082826/219265

@DanielRuskin1

This comment has been minimized.

DanielRuskin1 commented Aug 23, 2016

@scott2b I think it's because:

  1. The Lambda is in one of the "point to NAT" subnets, and as such points traffic to the NAT
  2. When the NAT gets traffic, it needs something to send it out to the internet with (the IGW). So we put it in the IGW's subnet.
@rydogjones

This comment has been minimized.

rydogjones commented Sep 17, 2016

Just want to say thanks for the clear, step by step...we need more AWS docs like this!

@lukeggchapman

This comment has been minimized.

lukeggchapman commented Sep 22, 2016

This helped me a lot. Thank you!

@leedjones

This comment has been minimized.

leedjones commented Oct 10, 2016

This fails for me setting up the route tables. It wont let me add another route with the error

There are not any Internet Gateway, Network Interface, or Virtual Private Gateway targets.

@mmmnt

This comment has been minimized.

mmmnt commented Dec 1, 2016

Thanks man, saved my day :-)

@SnowSprout

This comment has been minimized.

SnowSprout commented Dec 12, 2016

Thank you!!

@dparlar

This comment has been minimized.

dparlar commented Dec 14, 2016

This solution works perfectly. Thank you for writing this up :)

@Asoul

This comment has been minimized.

Asoul commented Jan 12, 2017

Thank you so so so much!!!!! This work well 👯‍♂️

@postme

This comment has been minimized.

postme commented Jan 13, 2017

Thank you very much, this really helped me!

@JonathanBAdams

This comment has been minimized.

JonathanBAdams commented Jan 26, 2017

I'll second leedjones' comment. I don't recall this being a problem the last time I created a VPC. Beside the "Add another route" button, you'll see the warning, "There are not any Internet Gateway, Network Interface, or Virtual Private Gateway targets."

Actually, the problem is suggested in the tutorial, which has us point a route to the nat and igw before having us create the nat (creating the igw is not mentioned.)

I was able to get around this by creating the NAT Gateway before editing the route tables. You'll need to do the same thing for an IGW, and don't forget to attach it to the appropriate VPC before trying to edit the route tables. (Actually, you need to do the IGW first or the NAT Gateway creation fails.)

@mp3il

This comment has been minimized.

mp3il commented Jan 30, 2017

Wooha! Thats super useful. thanks!

@bdecotes-pl

This comment has been minimized.

bdecotes-pl commented Feb 15, 2017

This helped me out quite a bit. Thank you!

@sandeshbsuvarna

This comment has been minimized.

sandeshbsuvarna commented Feb 21, 2017

Life saver !!!!!

@pizza-r0b

This comment has been minimized.

pizza-r0b commented Feb 24, 2017

👍 Thanks!

@Dambre

This comment has been minimized.

Dambre commented Mar 13, 2017

OMG, It is working!

@eastjavabaker

This comment has been minimized.

eastjavabaker commented Mar 24, 2017

NAT gateway must created first before create new route-tables with destination to NAT gateway

@jigarcaptain

This comment has been minimized.

jigarcaptain commented Apr 12, 2017

Thanks a lot.

This is probably the best description I’ve seen so far. ~ My Manager

@ihsanhaikalz

This comment has been minimized.

ihsanhaikalz commented May 22, 2017

I have followed your instruction here but still my lambda can't send post request to my server, my code is like this https://stackoverflow.com/questions/43998130/send-aws-s3-events-in-cloudwatch-to-my-server-using-lambda-in-java but not sure where it is wrong

@hovhanneskhalatyan

This comment has been minimized.

hovhanneskhalatyan commented May 24, 2017

Thank you very much!!!

@cagataygurturk

This comment has been minimized.

cagataygurturk commented May 28, 2017

2. VPC, the default setting where the lambda function can talk to your AWS services but can't talk to the web.

This is not correct. AWS services are also on Public Internet so you have NAT Gateway and you can access AWS API's and other Internet resources or you can't connect to none of them.

@b-nroths

This comment has been minimized.

b-nroths commented Jun 20, 2017

Thank you!

@jeanmidevacc

This comment has been minimized.

jeanmidevacc commented Jul 17, 2017

Awesome tutorial, very clear thanks !!

@KMNowak

This comment has been minimized.

KMNowak commented Jul 24, 2017

Thank you for this guide. Works perfect!

@romaninsh

This comment has been minimized.

romaninsh commented Aug 16, 2017

I had to implement above Lambda VPC setup with Internet access inside a CloudFormation template:

https://gist.github.com/romaninsh/81d4ee778c1e20f709f3518c22521ba4

@voho

This comment has been minimized.

voho commented Aug 24, 2017

Hi, first - thanks for a great tutorial!!!
Some tips:

  • Could you please add a section on how to test this setup? Maybe simple JS/Python Lambda code?
  • I also second that the NAT must be created just after the subnets, before the routing tables.
@kevupton

This comment has been minimized.

kevupton commented Aug 29, 2017

WOW. 24 hours later, you life saver
Why is it so hard to find any information on this.

@mikehains

This comment has been minimized.

mikehains commented Oct 16, 2017

Much much appreciated. Simple instructions, followed easily. Thank you !

@codekipple

This comment has been minimized.

codekipple commented Oct 26, 2017

Thanks so much. I was struggling to get to get my lambda setup and this helped me understand what my problem was.

@seydabenzer

This comment has been minimized.

seydabenzer commented Nov 16, 2017

You saved another day. Thanks a lot! <3

@scottishbee

This comment has been minimized.

scottishbee commented Nov 19, 2017

I'm trying to get my Lambda function to talk to another service that requires whitelisting. Which of these IPs can I give them to whitelist?

@caitlinosh

This comment has been minimized.

caitlinosh commented Dec 8, 2017

This is great! I was wondering if after you did this you could access your rds from the command line? I have my inbound and outbound ports set to all traffic in my rds security group, but I still get this error "ERROR 2003 (HY000): Can't connect to MySQL server on 'thursdaydatabase.chtwndzhnycc.us-east-1.rds.amazonaws.com' (60)".

@joshhornby

This comment has been minimized.

joshhornby commented Jan 6, 2018

This works perfectly. Thank you so much!

@seaweasel

This comment has been minimized.

seaweasel commented Jan 17, 2018

I just want you to know I'm naming my firstborn after you.

@liuxuc

This comment has been minimized.

liuxuc commented Jan 19, 2018

Works like a charm! Really appreciate the article. Thanks!

@igorescobar

This comment has been minimized.

igorescobar commented Jan 31, 2018

It works! <3

@ramirezzRK

This comment has been minimized.

ramirezzRK commented Feb 17, 2018

Thanks so much for this tutorial. Lot of head scratching about this one

Struggled a bit with very last step "Create a nat" which I found hard to imagine in the context (I know, begginer :/) This is super important step as it creates bridge between nat and igw subnets!!!

I Imagine it works something like this:

https://imgur.com/Xro1YqO

@xkguq007

This comment has been minimized.

xkguq007 commented Feb 26, 2018

Great works, time saver! ;D

@velopert

This comment has been minimized.

velopert commented Mar 6, 2018

Thanks!

It is important to select lambda-subnet-point-to-igw when you create NAT Gateway. When I saw that, I thought it was a typo and I selected one of lambda-subnet-point-to-nat.

;( that was totally a mistake. In order for NAT to access to public internet, you HAVE to select lambda-subnet-point-to-igw.

By the way, you have to create NAT first before you configure lambda-rt-to-nat.

@murtyjones

This comment has been minimized.

murtyjones commented Mar 9, 2018

Do these newly created subnets all need to be in the same availability zone (e.g us-east-1a)?

@ybendek

This comment has been minimized.

ybendek commented Mar 22, 2018

You are my hero, it works like a charm. Why is not this into the official documentation??? thank you thank you

@oscarnevarezleal

This comment has been minimized.

oscarnevarezleal commented Mar 23, 2018

So many thanks for this. I couldn´t find any information like this in the official documentation.
And yes, NAT gateway must created first.

@benjaminlong

This comment has been minimized.

benjaminlong commented Apr 19, 2018

I wish I'd seen this tuto...

@zachstegall

This comment has been minimized.

zachstegall commented Apr 26, 2018

Spot on, man. I spent hours on this. Thank you.

@apautrot

This comment has been minimized.

apautrot commented Apr 26, 2018

Same here... Iv'e spent hours trying to understand how I could setup all of this.
I couldn't have made it properly without your tuto !
So much thanks to you !

@bgarciaentornos

This comment has been minimized.

bgarciaentornos commented May 15, 2018

Hi, thanks a lot, this was very useful.

Now I'm having the same problem as caitlinosh, I can't connect to my RDS anymore via MySQL Workbench (it used to work). Any help would be much much appreciated.

Thanks.

@ciferkey

This comment has been minimized.

ciferkey commented Jun 4, 2018

How could these steps be modified to use a ec2 instance running one of amazon's NAT AMIs? I'm looking to use that as a cost saving measure for a small project.

@AlexanderCollins

This comment has been minimized.

AlexanderCollins commented Jun 25, 2018

@caitlinosh @bgarciaentornos Did either of you manage to resolve the issue of connecting to your RDS? I'm having the same issue.

[EDIT]
@caitlinosh @bgarciaentornos I've solved our problem. To allow lambdas to have internet access, RDS access and to allow the RDS to be publicly available (hopefully within a specific ip range - i.e your static IP), your RDS's subnet group must contain only public subnets; that is subnets which have a route table that points 0.0.0.0/0 to an IGW which is attached to your VPC. You must create a subnet group in Amazon RDS > Subnet Groups which contains the public subnets and then launch an RDS instance with this new subnet group (or configure your current system to these specifications).

In my case I have 6 subnets, 3 are private pointing to a nat gateway, the other three public pointing to an IGW as described above. It's important to note that as long as the Lambda function and the RDS instance are in the same VPC, then the Lambda function will have access to the RDS instance.

@efi-mk

This comment has been minimized.

efi-mk commented Jul 18, 2018

Guys, I've created a CloudFormation that creates the appropriate network resources - have a look at https://gist.github.com/efi-mk/d6586669a472be8ea16b6cf8e9c6ba7f

@a1anw2

This comment has been minimized.

a1anw2 commented Aug 9, 2018

This is a wonderful guide, thank you.
One question I am pondering - I have the majority of my Lambda's NOT using the NAT, as I deploy them to a separate subnet from the NAT. That is all fine, so Lambda's that are needing S3/DynamoDB access are going out through the service endpoints. (no extra cost). I guess the Lambda's I have pointed at my NAT subnet, if they need S3 access will be incurring the extra overhead of going out over that instead of the service endpoint.

I do "love" how Amazon builds a restriction then charges you more to get around it. Running a Lambda inside a secure VPC shouldn't have to have this many hoops to jump through.

@markosorec

This comment has been minimized.

markosorec commented Sep 12, 2018

This is awesome. I have been trying to setup Lambda inside VPC with internet access for a while now. Follow this guide and everything will work flawlessly.

Thanks! 👍

@chefren

This comment has been minimized.

chefren commented Sep 18, 2018

Thanks for the great tutorial.

Just as a note, please be aware that VPC is free, but NAT GWs do have an hourly cost

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment