Home > Software design >  Avoid settings AWS credentials for local development with localstack on node
Avoid settings AWS credentials for local development with localstack on node

Time:01-15

Using the aws-sdk for node, I am initializing sqs

sqs = new AWS.SQS({
  region: 'us-east-1',
});

In the prod environment this works great because the role running this has the required permissions to connect with SQS. However, running locally is a problem because those prod permissions are not applied to my local dev environment.

I can fix this problem by adding them in

sqs = new AWS.SQS({
  region: 'us-east-1',
  accessKeyId: MY_AWS_ACCESS_KEY_ID,
  secretAccessKey: MY_AWS_SECRET_ACCESS_KEY,
});

But for local development, I don't make any requests to AWS since I'm using a localstack queue.

How can I initialize AWS.SQS so that it continues to function in prod without specifying the AWS keys for local development?

CodePudding user response:

The AWS SDK and CLI read credentials from multiple locations in a well-defined order.

For example you can create a local credential file, and SQS will automatically use credentials from it without making any changes to your original production code.

% cat ~/.aws/credentials 
[default]
aws_access_key_id=AAA
was_secret_access_key=BBB

If you have multiple environments, you can specify them in this file by name. The SDK and CLI will typically read the $AWS_PROFILE environmental variable and use the specified profile from your credentials (or [default] if the environmental var is missing).

CodePudding user response:

I am not using AWS resources when developing locally, so it should not be necessary to provide AWS key credentials at all.

If you want to work around this problem, you can just set bogus values.

const SQS = new AWS.SQS({
    region: 'us-east-1',
    accessKeyId: 'na',
    secretAccessKey: 'na',
});
  •  Tags:  
  • Related