Skip to content
Advertisement

AWS Lambda S3.getObject throws “Access Denied”, but only when running locally

I am using AWS Lambda and serverless framework to build a service which uses S3 to store a file.
The lambda function (“hello“) works perfectly when deployed to the cloud (it has an http endpoint, I invoke it from the browser).
On the contrary, when invoked locally (serverless invoke local --function hello) I get “access denied” error:

JavaScript

This is my simplified function:

JavaScript

This is my serveless.yml (the relevant parts):

JavaScript

I did read somewhere that IAM role statements differ for local and cloud execution, but if it right, I’m a bit confused…
Can anybody clarify?
How should I correct my serverless.yml to avoid local invocation errors?

Advertisement

Answer

To quote the documentation for invoke-local:

When you use serverless invoke local, the situation is quite different: the role isn’t available (the function is executed on your local machine), so unless you set a different user directly in the code (or via a key pair of environment variables), the AWS SDK will use the default profile specified inside your AWS credential configuration file.

So, for local invocation, either configure local AWS credentials appropriately in ~/.aws/credentials or provide them via environment variables.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement