Skip to content

Instantly share code, notes, and snippets.

@magickatt
Created June 17, 2024 21:15
Show Gist options
  • Save magickatt/0f6d8df1a4dd8b795affb2a9dea47d8e to your computer and use it in GitHub Desktop.
Save magickatt/0f6d8df1a4dd8b795affb2a9dea47d8e to your computer and use it in GitHub Desktop.
Use ElasticMQ with AWS SDK v2
ctx := context.TODO()
queueName := "something"
endpointURL := "http://localhost:9324"
messageBody := "test"
// Create a custom endpoint resolver for ElasticMQ
elasticmqResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
if service == sqs.ServiceID {
return aws.Endpoint{URL: endpointURL}, nil
}
return aws.Endpoint{}, &aws.EndpointNotFoundError{}
})
// Create AWS SDK configuration using the custom endpoint resolver
sdkConfig, err := sqsconfig.LoadDefaultConfig(context.TODO(), sqsconfig.WithEndpointResolverWithOptions(elasticmqResolver))
if err != nil {
panic("aws sdk v2 configuration error, " + err.Error())
}
// Create an ElasticMQ client using the AWS SDK
client := sqs.NewFromConfig(sdkConfig)
queueURL := endpointURL + "/queue/" + queueName
// Prepare the message
input := sqs.SendMessageInput{
MessageBody: &messageBody,
QueueUrl: aws.String(queueURL),
DelaySeconds: 1,
}
// Send the message
output, err := client.SendMessage(ctx, &input)
if err != nil {
panic("could not send message to queue, " + err.Error())
}
fmt.Printf("message ID: %s", *output.MessageId)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment