question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

sam local invoke executes with AWS CLI profile instead of provided IAM role

See original GitHub issue

Description:

A developer on our sam project attempted to build and locally invoke a Function resource. The (Python 3.8) function in question calls list_users. Her IAM user role does not have the appropriate permissions; however, the ARN of a role that does was provided in the function’s Role in the project’s template.yaml. This role has been used in other functions and seems to be correctly associated with them on deployment.

Her local invocation failed, however, with the following error: An error occurred (AccessDeniedException) when calling the ListUsers operation: User: [her IAM user ARN] is not authorized to perform: cognito-idp:ListUsers on resource: [the user pool ARN]

Steps to reproduce:

Using the IAM console, create an IAM role with the appropriate permissions for a Lambda function to assume it and to call list_users on a user pool. Create an IAM user without those permissions. Sign into the AWS CLI as the IAM user without list_users. Add a function to a sam project and pass the ARN of the list_users role as its Role parameter. sam build and sam local invoke that function.

Observed result:

AccessDeniedException

Expected result:

The function should execute with the provided role

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

  1. OS: OS X Big Sur version 11.1
  2. sam --version: 1.23.0
  3. AWS region: us-east-1

I am aware that it’s not the best practice to grant developers the ability to assume permissions through Lambda functions that they wouldn’t be granted normally. In this case, I am not concerned with the scope of access but concerned that a more-permissive IAM user role might let a function with a restrictive IAM role work locally but hit AccessDeniedException on deployment.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
zhan9sancommented, Oct 21, 2021

@AbeGellis

Could you please try the following solution? It seems there is no need to introduce any change in sam but aws config

  1. Please ensure the aws config is in the following format.
$ cat ~/.aws/config

[profile user-without-list-users-permission]
region=us-west-2

[profile lambda]
role_arn = role_arn = arn:aws:iam::123456789012:role/lambda-execution-role-with-list-users-permission
source_profile = user-without-list-users-permission
region=us-west-2
  1. Test
sam local invoke --profile lambda

Reference: Specifies the Amazon Resource Name (ARN) of an IAM role that you want to use to perform operations requested using this profile.

0reactions
ssenchenkocommented, Dec 9, 2021

Closing it as the solution cannot be provided now in a form of fix.

Read more comments on GitHub >

github_iconTop Results From Across the Web

sam local invoke - AWS Serverless Application Model
The sam local invoke command is useful for developing serverless functions that handle asynchronous events, such as Amazon Simple Storage Service (Amazon S3)...
Read more >
AWS SAM Local Invoke with Lambda Role - Brian Pfeil
Running lambda locally with SAM in the lambda role security context. ... with the default credentials you have configured for the AWS CLI....
Read more >
Invoking AWS Lambda functions locally using AWS SAM CLI
The command to invoke Lambda locally is sam local invoke and -e flag is used to specify the path to the Lambda event....
Read more >
Access AWS Credentials with AWS SAM Local (Python)
SAM CLI will invoke functions with your locally configured IAM credentials. This is how I access the S3 bucket when deployed on AWS:...
Read more >
Set up a local serverless environment using the AWS SAM ...
The AWS SAM CLI will provide an AWS Lambda like execution environment using which you can run your code locally and get the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found