How to reach S3 from a nodejs lambda which is inside a VPC? - node.js

My lambda is getting ETIMEDOUT for hostname s3.amazonaws.com. I figured out that is is probably caused by the fact that my lambda is inside a VPC.
I suspect that I need to use an AWS Endpoint to reach S3. I know that we have endpoint set up and in AWS Console I can see and endpoint associated with this VPC called dev-s3-gateway with service name com.amazonaws.us-east-1.s3
How do I tell my lambda to use this endpoint?

Inside a VPC, your Lambda can be in either a private subnet or a public subnet.
Lambda in Public Subnets CANNOT access the internet.
Having Lambda access the internet
Being in a Private Subnet, your Lambda cannot have "direct" access to the internet. You need to provision a Public Subnet in your VPC and have both an Internet Gateway as well as a NAT Gateway in that subnet.
Route the traffic to 0.0.0.0/0 from your Lambda's subnet via the NAT Gateway.
From the routing table of the Public Subnet, route traffic to 0.0.0.0/0 via the Internet Gateway.
Your Lambda should now have access to the internet (and to S3).
Feel free to also check out this more detailed guide.
Accessing AWS services through VPC endpoints
Several AWS services offer VPC endpoints. These allow you to connect and interact with the respective services without your traffic ever leaving the AWS network.
For more information on them, please check out their documentation.
EDIT: Expanding a bit on your specific S3 use case.
S3 offers Gateway and Interface VPC Endpoints. Based on the endpoint name you provided in the question, I'm going to guess this is a Gateway VPC Endpoint. Once you set up the endpoint in your VPC, the Security Group(s) associated with your Lambda must allow outbound traffic to the endpoint.
You have two options.
First and simplest (but perhaps less secure - depending on your use case), you allow outbound traffic to 0.0.0.0/0. This will effectively allow you to call anything. However, if the Lambda is in a public subnet, it won't be able to access the internet, as explained above, but rather only the Gateway VPC Endpoint ranges.
The second option is to allow outbound traffic to the known Prefix List of the regional Amazon S3 endpoint. You can retrieve the PrefixList ID (looks like pl-xxxxxx) by invoking the DescribePrefixLists and looking for that Prefix List for service com.amazonaws.us-east-1.s3. Once you have the ID of the Prefix List, you can add it to the destination of an outbound rule of your Security Group(s).

Related

How to make https request from a lambda function which is in a private VPC?

I currently have a lambda function which is inside a Private VPC because I am using it to access a ElasticCache Redis Cluster. However, I am unable to make https requests because of the private VPC.
I have greated a NAT gateway with one of the subnets but all https requests time out.
Should I be creating a second lambda function? How should I be doing this?
The configuration should be:
NAT Gateway connected to a public subnet
AWS Lambda function connected to a private subnet
Private subnet Route Table configured to point to the NAT Gateway for 0.0.0.0/0 destination
Security Group on the Lambda function with "Allow All" Outbound rules
Adding ip address of the server you are trying make an https to, to the outbound rules of the security group might help. The outgoing request must've been disabled.

I want to insert data into db table using sequelize and iam using lambda function to insert data

Already I have existing db along with table created, I want to insert data to postgres db using sequelize. What procedure I need to follow.
The following is an overview of the services that you need to setup. I've done this twice and this diagram is from the first time I did it. The only thing you don't need is the bastion and its connection, so you can just ignore that part:
These are the components:
VPC: This is Virtual Private Cloud a virtual network that is separated from the rest of AWS. Inside the VPC:
Public S.N.: This is a subnet to which an Internet Gateway (IGW) is attached. It allows access to traffic outside of the VPC, such as the internet at large.
Private S.N.: This is a subnet that isn't accessible to anything outside of itself unless configure to using a Network Address Translation (NAT) Gateway.
Route Table: This is a list of rules as to where traffic can go. The routing rules from the Private S.N. (the purple arrows from the Lambda and RDS) to the NAT Gateway is what allows traffic to pass from the Private S.N. to the Public S.N. The Route Table determines whether a subnet is private or public depending on the rules in the Route Table.
IGW: The Internet Gateway is added to the VPC and connected to the Public S.N. using the NAT Gateway
RDS: This will be your existing postgres db instance.
Lambda: This will contain your Sequelize instance, code to connect to the RDS, get records, etc. The Lambda and the RDS must be in the same subnet. There may be multiple subnets associated with the RDS, the Lambda doesn't have to be in all of them, but it has to be in at least one of them.
APIG: The API Gateway that is connected to your Lambda so it can be called. I'm not sure how you want to call your lambda but this is a pretty standard way if you want an API endpoint to call.
That's an overview of what's involved. Here are some resources that I used to help me set it up the first time I did it:
https://gist.github.com/reggi/dc5f2620b7b4f515e68e46255ac042a7
https://shontauro.medium.com/how-can-i-turn-my-restful-api-into-a-serverless-application-and-deploy-it-to-aws-lambda-step-by-8ff6cc97780f

My API on API Gatway returns error 502 every time I make a request to an external API

I have a NodeJS Rest API that is deployed on AWS throught Serverless, which automatically creates a Lambda function and a API on API Gateway for me.
Every time I try to make a HTTPS request to any external APIs, I get an error from API Gateway (502 - Internal Server Error), even thought everything works fine when I'm testing in my local PC. And the error only happens if I call the route that makes the external request, so I'm sure that's the problem.
I've already activated API Gateway logs with Cloudwatch (following this post), but the only important log I get is Endpoint response body before transformations: {"errorMessage":"2020-10-21T18:34:14.038Z 4cf0e078-fec9-4b9c-a199-26216a3951aa Task timed out after 6.01 seconds"} (complete logs in that image). The Lambda logs are less detailed, but here they are.
I also have set up a VPC and a Security Group for my Lambda function. My Security Group already alows all trafic for both inbound and outbound rules. My VPC may be the problem, since I don't understand very much about subnets and the configurations I got there. These are my Lambda VPC configurations.
Can someone tell me what's the problem? I'm available to add any more information that you may want/need.
--------- Edit 1:
I tried to follow the steps of this post, but it didn't work. Let me explain everything I did:
First of all, I created a NAT Gateway to my VPC and a new Route Table with the 0.0.0.0/0 destination routed to this NAT Gateway. Then I created a Public Subnet, assigned the new Route Table to it and turned on the Enable auto-assign public IPv4 address option. Finally, I assigned this new Public Subnet to my Lambda function, but the error was still there. I also tried to remove the Public Subnet from the Lambda function, 'cause someone said it would work on the post, but it still didn't work.
The only thing I couldn't do was to set my new Public Subnet as a default subnet. I don't know if it was a core thing to do and if it only didn't work because of that.
Am I forgetting something?
I just solved it.
I kept searching on the internet for possible solutions and I found this link that has a video on the right corner (right there) with the perfect tutorial.
The problem was that I only had Subnets connected to a Internet Gateway and no Subnets to a NAT Gateway, like #MarkB said. But I tried to solve it by changing my only 3 Private Subnets, that were assigned to both Lambda and RDS, to connect only with the NAT Gateway and ended up removing the Internet Gateway assignment from my RDS's.
I decided to create 3 new Private Subnets ONLY for my Lambda Functions, those connected to the NAT Gateway, and 1 Public Subnet, connected to the Internet Gateway. The previous Subnets that I already had were intact in the end, and it fits just well.

How to configured Default VPC IN AWS?

i want to send mail using node mailer in NodeJS if my lambda function is developed in default VPC because I have to access RDS too from the lambda function.
I am unable to send success mail for data successfully inserted in RDS if I deployed my lambda function in default VPC WHAT changes I need to do so I can send.
IF I choose NO vpc then I am unable to set data to database.
From https://docs.aws.amazon.com/lambda/latest/dg/configuration-vpc.html,
When you connect a function to a VPC in your account, it does not have access to the internet unless your VPC provides access.
I take this to mean that if you wish to access both RDS and the internet from lambda from within your VPC, you need a NAT gatway (or to spin up your own instance). In other words, lambda does not support internet access with a public IP through an Internet Gateway, which is the mechanism of internet access within your vpc.
If you don't mind the cost, about 4.5 cents an hour plus data transfer last I checked, the simplest solution is probably:
add another subnet to your VPC.
Add a NAT Gateway to your VPC.
Add a route table to the subnet that routes through the NAT Gateway
put your lambda in that subnet
This essentially creates a connection to the internet in that VPC without your lambda holding a Public IP address.

Securing outbound traffic rule from EC2 instances when using ECS

Even when I create EC2 instances in a private subnet, they must be able to send traffic to the Internet if I want to register them to a ECS cluster.
I am using a NAT gateway to do this, but I still feel insecure that the instances can send private information to anywhere in case of takeover.
What would be the most compact CIDR range that I can use for the instances' security group, instead of 0.0.0.0/0?
For the moment, you may have to rely on the list of public IP address ranges for AWS, allowing traffic bound for all the CIDR blocks associated with your region.
Part of design for resiliency of much of what AWS does relies on the ability of their service endpoints not to depend on static address assigments and instead to use DNS... but their service endpoints should always be on addresses associated with your region, since very few services violate their practice strict regional separation of service infrastructure.
(CloudFront, Route 53, and IAM do, maybe others, but these are provisioning endpoints, not operational ones. These provisioning-only endpoints do not need to be accessible for most applications to function normally.)
A super common pattern here is to create a proxy in a narrow CIDR and only allow outbound traffic to flow through the proxy. White-list AWS endpoints and log all traffic flowing through the proxy for monitoring/auditing.
AWS Endpoints = https://docs.aws.amazon.com/general/latest/gr/rande.html

Resources