How to make an external api call within an AWS Lambda function
This is your solution, please read.
https://forums.developer.amazon.com/questions/95692/invoking-a-rest-api-from-within-a-lambda-function.html
Further
This code sample shows how to call and receive external rest service data, within your skill Lambda code. https://github.com/robm26/SkillsDataAccess/blob/master/src/CallService/index.js
You don't have a VPC set, right? And if you do, have to check that you have a NAT gateway attached if you're on a private subnet.
This is an old question but I spent a few hours banging my head figuring out how to set this up properly, so here's hoping someone else's time will be saved :)
If your lambdas are in a VPC they need to go through a NAT Gateway in order to reach external services.
One way to achieve this is:
- configure your lambda to use a (or multiple) specific subnets (the Lambda console will suggest you associate at least 2 subnets in different availability zones to your lambda to ensure availability)
- create a NAT Gateway in a different subnet
- have the route table associated with your lambda's subnets send all outbound traffic (0.0.0.0/0) to the NAT Gateway you created (you do that by choosing the NAT in the
target
field) - have the route table in the NAT's subnet send all outbound traffic to an Internet Gateway
This will route traffic from your lambdas properly through the NAT and Internet Gateway so they can reach external services.
Note that if you're also using RDS from your lambdas AND you intend to also connect to RDS from outside (e.g. to manage the DB from your local machine), you can't put the RDS instance in the lambdas' subnet, or you won't be able to connect to it from outside (the NAT is outbound only). In that case you should make sure your RDS instance is associated with a subnet that is accessible, for instance the one the NAT is in (i.e. the one that sends outbound traffic to the Internet Gateway).