Search code examples
amazon-web-services.net-coreamazon-sqs

Send Message to Amazon SQS using credentials in code


Trying to post a message to an AWS SQS Queue that already exists using .Net Core.

Due to some deployment issues, I don't want to create a separate credentials file, I just want to manually add the credentials and add them to my client or request. I can't see how to do this using the documentation.

I've got a simple console app version of what I am trying to do below... I have created the Credentials, I just can't see how to inject them into the client or request so that it authenticates with my IAM user.

Currently the code just hangs creating the client.

var awsCreds = new BasicAWSCredentials("MYKEYGOESHERE", "MYSECRETGOESHERE");

var amazonSQSConfig = new AmazonSQSConfig();
                amazonSQSConfig.ServiceURL = "https://sqs.eu-west-1.amazonaws.com";

var amazonSQSClient = new AmazonSQSClient(amazonSQSConfig);
var sendRequest = new SendMessageRequest(); 

sendRequest.QueueUrl = "https://sqs.eu-west-1.amazonaws.com/[USERID]/[QUEUENAME]";
sendRequest.MessageBody = "{ 'message' : 'hello world' }";

var sendMessageResponse = amazonSQSClient.SendMessageAsync(sendRequest);

Solution

  • You have to pass the credentials to the AmazonSQSClient like so:

    var amazonSQSClient = new AmazonSQSClient(awsCreds, amazonSQSConfig);