How to configured Default VPC IN AWS? - node.js

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.

Related

How to reach S3 from a nodejs lambda which is inside a VPC?

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).

Lambda function timeout

I have the following Lambda function set in AWS which have to return the params of
AWS Network Firewall:
import boto3
import json
session = boto3.session.Session()
nf_client = session.client(
service_name = "network-firewall",
endpoint_url= "https://network-firewall.eu-west-2.amazonaws.com"
)
def lambda_handler(event, context):
response = nf_client.describe_firewall(
FirewallName="DemoFirewall2")
return 1
What I have checked
It timeouts no matter the timeout limit
The alocated memory is enough
Both endpoints of the lambda and the firewall can be reached from one another(even checked with Network Analyzer)
Security groups are allowing all trafic in both directions.
The lambda is associated to the VPC and with the sub-net of the firewall.
CloudWatch is showing just timeout.
I have no additionl configration, no environment variables or such just what is defined in the lambda.
I have two subnets and 4 routing tables:
client subnet which has the following routing table and it is associated with the according subnet:
10.1.0.0/16 local
0.0.0.0/0 vpce-firewallEndpoint
Firewall subnet associated with the accordig subnet:
10.1.0.0/16 local
0.0.0.0/0 igw-InternetGateway
Gateway table with edge association to the gateway:
10.1.0.0/16 local
10.1.2.0/24 vpce-firewallEndpoint
the deafault main table:
10.1.0.0/16 local
The traffic from the client subnet is routed to the firewall subnet and then routed to the IGW.
Guess I am missing something with the session configarion because the lambda probably don't know which firewall to describe.
Lambda functions in a VPC never get assigned a public IP address, so they can't use an Internet Gateway directly. A Lambda function in a VPC has to be deployed in a private subnet, with a route to a NAT Gateway, in order to access things outside the VPC.
If your Lambda function doesn't need to access any VPC resources then there is no benefit to deploying the function inside the VPC, it only causes issues such as the one you are encountering.

GCP serverless VPC connection make 408 timeout

My cloud functions server needs to access to external service and ip adress needs to be applied to the system prior to access to the service. Since it is serlverless, cloud functions does not have static ip adress. I have searched the way to give static ip adress to cloud functions and it was serverless VPC access at GCP VPC network.
Image below shows my VPC creation.
Cloud functions VPC connection setting is at below
Cloud functions works fine before setting VPC connection. After VPC connection setup, error below is occured.
Do I need any other setting for VPC connection? or do I need to do something with my server code to get rid of 408 timeout error?
If you know anything about this problem, please share your knowledge. Thank you

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.

Resources