Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda: Unable to access SQS Queue from a Lambda function with VPC access

Tags:

aws-lambda

I have a Lambda function that needs to read messages from an SQS queue using it's URL. Then it needs to insert that data to Cassandra running on a server inside a VPC.

I am able to access the Cassandra server from my Lambda function, using it's private IP and configuring the security groups correctly.

However, I am not able to read messages from the SQS Queue. When I change the configuration of Lambda function to No VPC, then I am able to read the messages from the SQS Queue. However, with VPC settings, it just times out.

How can I overcome this ? I have checked the security group of my Lambda function has full outbound access to all IP addresses.

like image 593
Mandeep Singh Avatar asked Feb 16 '16 12:02

Mandeep Singh


People also ask

Can you put SQS in a VPC?

Amazon SQS now Supports Amazon VPC Endpoints using AWS PrivateLink. AWS customers can now access Amazon Simple Queue Service (Amazon SQS) from their Amazon Virtual Private Cloud (Amazon VPC) using VPC endpoints, without using public IPs, and without needing to traverse the public internet.

Can SQS trigger Lambda in VPC?

Unless you have SQS endpoint in your VPC, a lambda stay within VPC without NAT gateway won't be able to access SQS because SQS normal endpoint is public endpoint. Yes, Lambda can still be triggered, but not be able to communicate back to SQS for retrieving and manipulate messages.

How do I enable Lambda in access to SQS?

In order to grant a Lambda function access to an SQS queue, we have to attach an IAM policy to the function's execution role. The policy should grant permissions for all the Actions the function needs to perform on the queue.

Why my Lambda Cannot access Internet anymore from its AWS VPC?

Lambda in a VPC does not have access to internet. You need to setup internet gateway in public subnet and NAT gateway in private subnet with your lambda to be able to access internet. From docs: Connect your function to private subnets to access private resources.


2 Answers

At the end of 2018, AWS announced support for SQS endpoints which provide

connectivity to Amazon SQS without requiring an internet gateway, network address translation (NAT) instance, or VPN connection.

There is a tutorial for Sending a Message to an Amazon SQS Queue from Amazon Virtual Private Cloud

See also the SQS VPC Endpoints Documentation for more information.

Its important to note that if you want to access SQS within the Lambda VPC there are a couple other things you need to do:

  • Make sure to specify the SQS region in your code. For example, I had to set my endpoint_url to "https://sqs.us-west-2.amazonaws.com"
  • Make sure that you have attached a "wide open" security group to the SQS VPC Interface, otherwise SQS will not work.
  • Make sure that your subnets in your Lambda VPC match what you have set up for your SQS VPC Interface.
like image 84
Onema Avatar answered Sep 30 '22 00:09

Onema


Some services (e.g. S3) are offering VPC endpoints to solve this particular problem but SQS is not one of them. I think the only real solution to this problem is to run a NAT inside your VPC so the network traffic from the Lambda function can be routed to the outside world.

like image 31
garnaat Avatar answered Sep 29 '22 23:09

garnaat