'AWS SAM: How to initialize S3Client credentials from a lambda function handler

I am building an HTTP API using sam local start-api. Each endpoint of this API is mapped to a lambda handler I have written in Javascript code.

One of these lambda handlers requires to download and upload files from S3, for which I am using this S3Client from @aws-sdk/client-s3. I have tried to initialize such client as follows:

const s3Client = new S3Client({
    region: "eu-west-1"
});

expecting that it reads the credentials from my ~/.aws/credentials file, but it does not. All operations via this client fail due to lack of permissions.

I would like to know what is the correct way of using this S3Client from within a lambda handler that I am testing locally using sam local.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source