Created
June 17, 2024 21:15
-
-
Save magickatt/0f6d8df1a4dd8b795affb2a9dea47d8e to your computer and use it in GitHub Desktop.
Use ElasticMQ with AWS SDK v2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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