Data transfer costs when one lambda invokes another lambda - amazon-web-services

I have two lambdas. LambdaA is the parent lambda that invokes LambdaB in parallel using the Event InvocationType (boto3). In every invocation, LambdaA sends a payload of 5MB to LambdaB. Both the lambdas are in the same VPC and in the same two private subnets (and same security group).
Now, assuming that LambdaA invokes LambdaB 5000 times in parallel for further invocation, a total payload of 25GB would be transferred between LambdaA and LambdaB.
I am trying to find out if I would be charged for the 50GB of data transfer as a data transfer cost, given that the data transfer is within the same VNet and the same two private subnets (and same security group).
Would I also be charged if there are in the same VPC and in the same private subnet (only one and same security group)?

When an AWS Lambda function invokes another AWS Lambda function, it would be sending traffic to the endpoint of the AWS Lambda service (not to the other Lambda function itself). Since your first Lambda function is connected to a VPC and the AWS Lambda service endpoint is on the Internet, the request would need to exit the VPC to access the Internet.
From EC2 On-Demand Instance Pricing – Amazon Web Services:
Data transferred “in” to and “out” from public or Elastic IPv4 address is charged at $0.01/GB in each direction.
However, if your first Lambda function was not connected to a VPC, then there would be no such charge since the Lambda function would be directly connected to the Internet. Typically, you should only connect an AWS Lambda function to a VPC if it specifically needs to access resources in that VPC (eg an Amazon RDS database).
Alternatively, you could use a VPC Endpoint to directly connect to to the AWS Lambda service. From Configuring interface VPC endpoints for Lambda - AWS Lambda:
If you use Amazon Virtual Private Cloud (Amazon VPC) to host your AWS resources, you can establish a connection between your VPC and Lambda. You can use this connection to invoke your Lambda function without crossing the public internet.
This would allow your Lambda function to connect to the VPC, but also connect to the AWS Lambda service without 'exiting' the VPC, thereby avoiding the 1c/GB charge.
The main thing to realise is that the two Lambda functions are not directly communicating. Rather, the communication is to the AWS Lambda service, which is then responsible for provisioning and invoking the second Lambda function.

Yes, you will be charged the EC2 AZ to AZ ingress and egress cost.
If the data was downloaded via S3 there would be no cost.

Related

Access AWS Batch from a lambda instance

I'm getting timeout errors when trying to create AWS Batch jobs using the AWS Javascript SDK from a Lambda.
How can I connect to AWS Batch from a Lambda instance besides creating public/private subnets with a NAT gateway?
The only other way is to not put your lambda in VPC. If you really need your function to be in VPC, the only way is to place it in private subnet and then use NAT in public subnet. This is because there is no VPC interface endpoint for AWS Batch.
You could also have second lambda function, not in VPC. So the main function in the VPC, invokes the second one which operates on AWS Batch. This will work, because AWS Lambda has interface endpoint which allows you to invoke functions from VPC without internet access.

AWS lambda in VPC with 2 AZ failover strategy

I want to create lambda in VPC, also want to spin these lambda's in two different subnets in different AZ's and while failure of one AZ traffic should automatically route to different AZ. I am planning to trigger the above lambda function through API gateway. Will API gateway detect AZ failover and run lambda in another AZ?
From AWS documentation.
High availability – Lambda runs your function in multiple Availability Zones to ensure that it is available to process events in case of a service interruption in a single zone. If you configure your function to connect to a virtual private cloud (VPC) in your account, specify subnets in multiple Availability Zones to ensure high availability.
The API gateway itself triggers a Lambda function whenever it receives a request, currently there is no option for health check of a Lambda function, but reading above mention from AWS, it's safe to say that the AZ/health check is done inside Lambda underlying infrastructure, rather than service level.

How to reduce NAT traffic costs - Lambda and SQS

Currently i'm working on app that collects data and processes them. All data collection is handled by AWS Lambda.First lambda get data from APIs, data is processed and sent to SQS. Everything works fine, but unfortunately NAT costs are higher than expected. Lambda downloads around 10TB monthly (I'm planning to increase that number), so i'm charged about 500$ monthly just for NAT traffic. Other services don't use NAT. Is there any NAT alternative or way to reduce costs? What i thought about is to replace SQS with ElasticCache in private subnet, but that's 'just' 3 TB of traffic less.
If an AWS Lambda function is connected to a VPC, it can communicate with resources in the VPC. For example, it might need to connect to an Amazon RDS database in the VPC.
To access the Internet, the Lambda function would need to be connected to a private subnet and then communicate with the Internet via a NAT Gateway or NAT Instance. (A NAT Instance is cheaper than a NAT Gateway, but is less reliable.)
However, if the Lambda function does not require access to resources inside the VPC, then do not associate a VPC with the Lambda function. This will provide direct access to the Internet.

AWS - Send Message to an SQS from a Lambda function inside a VPC

I have hosted a Lambda function using AWS Chalice inside a VPC since I want it to access a Serverless Aurora DB Instance. Now I also want this function to send_message() to an SQS.
I followed Tutorial: Sending a Message to an Amazon SQS Queue from Amazon Virtual Private Cloud and was able to call the SQS from inside my EC2. But even then I could not use my Lambda function to call the SQS.
It would be very helpful if someone could actually tell me how to do the whole thing manually rather than using the CloudFormation stack, or at least tell me how to get the SQS Endpoint working.
It appears that your situation is:
An Amazon VPC with an Amazon Aurora database
An AWS Lambda function that wants to communicate with the Aurora database AND an Amazon SQS queue
An AWS Lambda function can be configured as:
Connected to a subnet in a VPC, or
Not connected to a VPC, which means it is connected to the Internet
If you wish to have an AWS Lambda function communicate with resources inside a VPC AND the Internet, then you will need:
The Lambda function connected to a private subnet
A NAT Gateway in a public subnet
An Internet Gateway connected to the public subnet (it is most probably already in your VPC)
Alternatively, you can use a VPC Endpoint for SQS, which allows the Lambda function to access SQS without going to the Internet. If you are wanting to connect to multiple service (eg S3, SNS, SQS), it is probably easier just to use a NAT Gateway rather than creating VPC Endpoints for each service.
You either need to add a VPC Endpoint for SQS to your VPC, or place the Lambda function in subnets with a route to a NAT Gateway.

AWS lambda call dynamo db through private network or bypass internet traffic

I have a lambda function which runs every 15 minutes and saves some data in DynamoDB.
Now I want to secure the DynamoDB call made by my lambda so that the request does not go via the Internet, rather through Amazon internal network. There is no EC2 instance involved here though.
I have seen a few recommendations for using PrivateLink which binds the Dynamo to VPC endpoints so that calls made from EC2 instances always go via internal network bypassing Internet.
I was wondering such a configuration is possible for lamda calling DynamoDB since lamda itself does not run in any EC2 instance and is rather serverless?
The first thing I would say is that all of your traffic between Lambda and DynamoDB is signed and encrypted, so that's typically sufficient.
There are use cases, most typically compliance reasons, when this is not sufficient. In that case you can deploy the Lambda function into a VPC of your making and configure the VPC with a private VPC endpoint for DynamoDB. Typically, the VPC would be configured without an internet gateway or NAT so that it has no egress route to the public internet. Be aware that your Lambda function startup latency will be higher than usual, because each Lambda function environment needs to attach an ENI for access to the private endpoint.
See Configuring a Lambda Function to Access Resources in an Amazon VPC.
If you don't need to access resources in a VPC, AWS recommends not to run AWS Lambda functions in a VPC. From AWS Lambda Best Practices:
Don't put your Lambda function in a VPC unless you have to. There is no benefit outside of using this to access resources you cannot expose publicly, like a private Amazon Relational Database instance. Services like Amazon Elasticsearch Service can be secured over IAM with access policies, so exposing the endpoint publicly is safe and wouldn't require you to run your function in the VPC to secure it.
Running Lambda functions in VPC adds additionally complexity, which can negatively effect scalability and performance. Each Lambda function in a VPC needs an Elastic Network Interface (ENI). Provisioning ENI's is slow and the amount of ENI's you can have is limited, so when you scale up you can run into a shortage of ENI's, preventing your Lambda functions to scale up further.
This is one way to do it.
Step 1) Deploy your lambda inside VPC.
Step 2) Create VPC Endpoint to the DynamoDB.
This should help: https://aws.amazon.com/blogs/aws/new-vpc-endpoints-for-dynamodb/