I am trying to send a message from my lambda function to a sqs queue that is already created. When I run the code, it literally stops the execution and no feedback is provided by aws-sdk.
I also have a function to read from the queue when I insert the messages manually, I use the same code to create the session. Which I believe can be used on both situations.
Then I tried to use the code provided by amazon but the outcome was the same.
https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/sqs-example-receive-message.html
The only difference on my code is how I create the session. Like I mentioned above, that’s the same way I use to read messages when they are inserted manually in the queue. On that function everything seems perfect.
func sendToOrderQueue(rID string, c Course) error {
log.Println(1)
var err error
sess := session.Must(session.New(&aws.Config{
Region: aws.String("eu-central-1"),
}), err)
svc := sqs.New(sess)
log.Println(2)
url := "https://sqs.eu-central-1.amazonaws.com/XXXXXX/myqueue"
log.Println(3)
result, err := svc.SendMessage(&sqs.SendMessageInput{
DelaySeconds: aws.Int64(10),
MessageAttributes: map[string]*sqs.MessageAttributeValue{
"Title": &sqs.MessageAttributeValue{
DataType: aws.String("String"),
StringValue: aws.String("The Whistler"),
},
"Author": &sqs.MessageAttributeValue{
DataType: aws.String("String"),
StringValue: aws.String("John Grisham"),
},
"WeeksOn": &sqs.MessageAttributeValue{
DataType: aws.String("Number"),
StringValue: aws.String("6"),
},
},
MessageBody: aws.String("Information about current NY Times fiction bestseller for week of 12/11/2016."),
QueueUrl: &url,
})
log.Println(4)
if err != nil {
log.Println("Error", err)
return err
}
log.Println(5, *result.MessageId, err)
return err
}
Also, my serverless.yaml
service: client
frameworkVersion: ">=1.28.0 <2.0.0"
provider:
name: aws
runtime: go1.x
vpc: ${file(../security.yaml):vpc}
package:
exclude:
- ./**
include:
- ./bin/**
functions:
postFunction:
handler: bin/post
environment:
REDIS_URL: ${file(../env.yaml):environment.REDIS_URL}
HASH_KEY: ${file(../env.yaml):environment.HASH_KEY}
events:
- http:
path: /func
method: post
cors: ${file(../cors.yaml):cors}
Checking the cloudwatch’s logs the execution prints 1, 2, 3 and nothing else. No 4, no Error and no 5.
What am I doing wrong here?
I have the same issue.
Look in CloudWatch labmda’s logs. There is an error like
Task timed out after 10.01 seconds
This is a lambda’s timeout. You have no errors regarding sqs because timeout of lambda is smaller than default timeout of http.Client inside svc.SendMessage
(sendMessage is just a POST request to aws api) and lambda is terminated before it gets any response from sqs. 10 seconds for lambda and 30 seconds for http request in my case. Add LogLevel
to aws.Config
like this:
&aws.Config{
LogLevel: aws.LogLevel(aws.LogDebug),
}
and you will see this http request in CloudWatch logs. Also you can set lambda’s timeout to 2-3 times bigger than http.Client timeout and you will see retries in logs (3 retries by default).
Looks like lambda can’t resolve the host of sqs or something like this because I get the same errors when VPC is configured wrong.
UPD
Fixed the problem. If you use VPC in your lambda it should have special configuration to access to SQS from VPC. Take a look here https://docs.aws.amazon.com/en_us/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-sending-messages-from-vpc.html