How to configure vpc for a lambda and dynamodb? - amazon-web-services

I'm creating a VPC in aws, and plan to add dynamodb and lambda to access to dynamo. If dynamodb is in a private subnet, what else do i need for lambda to connect to the dynamo. If lambda is invoked by API gateway , can lambda and dynamodb be under same private subnet?

DynamoDB is not located in your VPC. If you want to access DynamoDB via the private network from Lambda, you can use the DynamoDB VPC Gateway endpoint. A route will be added to your subnet route table, all traffic to DynamoDB will route through AWS internal network.
Things to setup:
Private subnet
DynamoDB VPC endpoint (Gateway type, use interface type (cost $) if you want to connect via private IP)
VPC Lambda
If you want the API Gateway also be private, setup private API Gateway and API Gateway VPC endpoint

Related

Can we use both DynamoDb and VPC to an aws lambda?

I am working on a AWS lambda in which i want to use DynamoDB and a VPC(Amazon elasticcache for redis). But I was getting time out error when trigger lambda
You either need to create a DyanmoDB VPC Endpoint in the VPC, or add a NAT Gateway to the VPC, and only deploy the Lambda function in subnets that have a route to the NAT Gateway.
Depending on the VPC configuration subnet/Security Group of your lambda,
If your lambda is in a private subnet :
If you want a secure internal way, you can pass with VPC endpoint
Else if you want to pass with internet, you must have a NAT Gateway.
Check also the security groups/NACLS....

One VPC Endpoint for multiple services in AWS

I have VPC with two private subnets in two different AZ and
I am trying to create a VPC Endpoint for connecting lambda to my database in private one of subnet
But I need SNS also for sending message to users based on my lambda function
Do I need to create another endpoint for SNS in that subnet or I can attach multiple services to the same endpoint
I know NAT gateway is an option but it is costly than endpoints
Please suggest best way to do this
Thanks,
Monika
No. A VPC Endpoint is always for a specific service.
By the way, you do not require a VPC Endpoint for Lambda in your situation. Simply configure the AWS Lambda function to connect to the private subnets in the VPC. This is an in-built capability of Lambda functions and the do not require a VPC Endpoint to access the subnets.

AWS lambda cannot access DynamoDB after being placed in a private subnet?

As the title suggests, I placed my Lambda function in a private subnet and now It cannot access or timeout when scanning the DB. Prior to this, it could access and scan the DB. What should I do?
Your DynamoDB resources are not in your VPC. Since you've configured your Lambda functions to connect to your VPC, you need to setup a NAT Gateway or NAT Instance to allow your private resources to access the internet. As the docs state:
AWS Lambda uses the VPC information you provide to set up ENIs that
allow your Lambda function to access VPC resources. Each ENI is
assigned a private IP address from the IP address range within the
Subnets you specify, but is not assigned any public IP addresses.
Therefore, if your Lambda function requires Internet access (for
example, to access AWS services that don't have VPC endpoints), you
can configure a NAT instance inside your VPC or you can use the Amazon
VPC NAT gateway. You cannot use an Internet gateway attached to your
VPC, since that requires the ENI to have public IP addresses.
AWS Lambda Doc
Validate the following:
The route table for the Lambda has a NAT Gateway for internet traffic that resides in public subnet.
DynamoDB Gateway endpoint exists? Check its policy to ensure that it is not limited to specific sources
Outbound access is allowed via the security group and NACL

Inter VPC internet access between lambdas

I am working on a project where my main lambda function is in a VPC in private subnet and some sister lambda functions in a different VPC which are in their own private subnets. How can I go about calling these sister lambdas's from the main lambda across VPC without giving internet access to each of them via a NAT gateway linking to a public subnet which has an internet gateway attached to it.
Other AWS services that my main lambda invokes are:
1. S3
2. Dynamodb
3. Autoscaling
4. ECS
5. RDS
This can be done, but there are some complex steps involved.
First of all, when you use aws-sdk, the calls are made through the internet. To avoid this situation and access the services within the AWS network, The AWS has introduced some private VPC endpoints. I have only used S3 and API gateway private endpoints to date. But there is more type of VPC endpoints.
This is how I would do today,
setup a private API gateway API to invoke lambda - The private API's are only accessible through a private VPC endpoint for API gateway.
create a private VPC endpoint for API gateway.
setup VPC peering between the VPCs
(from the sister lambda on other VPC) invoke the API through the VPC endpoints public DNS URL
The drawback of adding an API in front of the lambda is, the API has a hard timeout of 29 seconds.
hope this helps.

How to connect elasticache and dynamoDb from aws-lambda without using NAT Gateway

I need to connect dynamoDb and elasticache from aws-lambda (otherthan using NAT Gateway).
ElastiCache provides essential caching methods along with help in making the Lambda state-ful. The concern is that for Lambda to work nice with DynamoDB it should be set to NoVPC.
If we have to use ElastiCache, Lambda and both have to be in the same VPC.TO use Both ElastiCache and DynamoDB together is quite a challenge specially with Lambda. Given the VPC challenges.Do you have any suggestions to make this easier?
A Lambda function would have to have VPC access to connect to ElastiCache, and it would have to have access to resources outside the VPC to access DynamoDB so it would require a NAT gateway. There is no way to provide access to both of those services to a single Lambda function without enabling VPC access and setting up a NAT gateway.
If you just need a Redis server and aren't required specifically to use ElasiCache, then you could use a RedisLabs instance which wouldn't require you to enable VPC access on your Lambda function.
There is now a relatively easy solution for DynamoDb access from a VPC: VPC Endpoints.
"Previously, if you wanted your EC2 (elroy: or lambda) instances in your VPC to be able to access DynamoDB, you had two options. You could use an Internet Gateway (with a NAT Gateway or assigning your instances public IPs) or you could route all of your traffic to your local infrastructure via VPN or AWS Direct Connect and then back to DynamoDB."
"A VPC endpoint for DynamoDB enables Amazon EC2 instances in your VPC to use their private IP addresses to access DynamoDB with no exposure to the public Internet...Your EC2 instances do not require public IP addresses, and you do not need an Internet gateway, a NAT device, or a virtual private gateway in your VPC. You use endpoint policies to control access to DynamoDB. Traffic between your VPC and the AWS service does not leave the Amazon network. "
The above quotes come from the links below. Note the the references to "EC2 instances" apply to lambda contexts as well.
See https://aws.amazon.com/blogs/aws/new-vpc-endpoints-for-dynamodb/
and
http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/vpc-endpoints-dynamodb.html