AWS lambda in VPC with 2 AZ failover strategy - amazon-web-services

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.

Related

Proper way of working with DocumentDB and Lambda

I am connecting to AWS DocumentDB from a Lambda function. In order to be able to do this I had to attach lambda to the default VPC (that's where DocumentDB cluster is running) and the default (public) subnets. But, this has caused my Lambda to timeout whenever trying make an outbound request, e.g. push message to SQS. This, I want to avoid.
So what is the recommended way of connecting to DocumentDB without loosing functionality that occurs when putting Lambda in the VPC? There's gotta be a simple solution.
Lambda functions in a VPC never get a public IP address. So if the function needs to access both VPC resources and other resources outside of the VPC the function has to be deployed only to private subnets with routes to a NAT Gateway.
Alternatively, if the only external resources you need to access are other AWS services, then you could add VPC Endpoints for those services to the VPC.

Data transfer costs when one lambda invokes another lambda

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.

Connecting AWS Lambda and RDS in different AWS Accounts

We have multiple AWS accounts for various departments. I want to give a Lambda function in one account access to an RDS instance in another.
If the Lambda function is not assigned to a VPC and the RDS is publicly accessible is there a way to limit the access to the RDS to only the Lambda function?
Is there a way to do this if the RDS instance is private and the Lambda function is not assigned to a VPC?
As best practice you should always keep your db in private subnet WITHOUT public ip and access. And then in your case assume all your AWS accounts are in a single organisation, you can share the subnets that the RDS located in account A to account B by https://aws.amazon.com/premiumsupport/knowledge-center/vpc-share-subnet-with-another-account/
Then in account B you config the Lambda to sit in the shared subnets from A, and set security group of Lambda functions, and in RDS instance security group you only allow connection from Lambda's security group, then all set.
May I know why you don't want to put Lambda into VPC? If you have concerns on Lambda cold start performance issue in VPC, notice that AWS already solve that in late 2019: https://aws.amazon.com/blogs/compute/announcing-improved-vpc-networking-for-aws-lambda-functions/
Public Amazon RDS database
If the Lambda function is not assigned to a VPC, then it is connected to the Internet. It can then connect to a publicly-available RDS database. However, there is no way to predict the IP address that will be used by the Lambda function.
Private Amazon RDS database
If the RDS database is only available within a VPC, then the Lambda function needs to be somehow connected to the VPC. Since the Lambda function is in a different AWS Account, you might be able to try:
VPC Peering to make the RDS database in one account accessible to the Lambda function in a different account
Shared VPCs: "In a shared VPC, each participant pays for their application resources including Amazon EC2 instances, Amazon Relational Database Service databases, Amazon Redshift clusters, and AWS Lambda functions."
The Shared VPC looks like it might be a great option for you, since (it seems) the AWS Lambda function from Account A could be placed into the same VPC as an Amazon RDS database from Account B.
See also: VPC sharing: A new approach to multiple accounts and VPC management | Networking & Content Delivery
Let us know if it worked for you!

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 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/