问题
I have Lambda function when invoked it creates SNS topic, adds subscribers to it and then publishes a message to it. After publishing the messages it deletes the topic. The name of the topic to be created and the subscribers are supplied to the lambda function as payload.
Sometimes it works and sometimes it fails with Task timed out after x seconds
I have increased the lambda timeout and still same issue.
I dug a little and found out that
sns.createTopic(params, function(err, data) {
if(err) {
console.log('Error Creating SNS Topic:',err);
} else {
console.log('SNS Topic Created Successfully:',data);
}
}
never returns, no error no data and I don't see result of console.log()
When it works, everything is good but when it fails I can't see to find the issue.
EDIT:
So I did a little more digging, I decreased the timeout of SNS topic creation it was 5 minutes by default now it is 5 seconds. When the failing happens I get this"
{ [TimeoutError: Connection timed out after 5000ms]
message: 'Connection timed out after 5000ms',
code: 'NetworkingError',
time: Thu Mar 30 2017 15:35:20 GMT+0000 (UTC),
region: 'us-east-1',
hostname: 'sns.us-east-1.amazonaws.com',
retryable: true }
回答1:
I think I figured the issue out, my Lambada is in VPC and I had selected a couple of subnets under my Lambada configuration. Some of the selected subnets didn't have access to the internet and I think that was the reason when the lambda container was being created under these subnets(with no internet) it was not able to access SNS and timing out. After removing the violating subnets it starts working. I haven't see the issue since then.
回答2:
For a lambda function to have internet access (while also being attached to a VPC)
- Select only private subnets for your lambda function
- Security group can be default.
- The private subnet should be attached to a route table which has a route 0.0.0.0/0 to a NAT gateway (X)
- The tricky part is that NAT gateway X should be attached to a public subnet(AWS console asks for a subnet while creation)
- The trickier part is that the public subnet we just used in step 4 should be attached to a route table which has a route 0.0.0.0/0 to a IGW. (The configuration of this route makes it a public subnet)
There is a great video by Amazon on this and it was very helpful for someone like me who had 0 knowledge on all the AWS jargon. https://www.youtube.com/watch?v=JcRKdEP94jM#action=share
来源:https://stackoverflow.com/questions/43121829/aws-sns-creation-times-out