aws / aws/aws-sam-cli

sam local invoke fails with AccessDeniedException when working with Layers

Open
#1,736 8 comments 3 reactions 0 assignees View on GitHub
maintainer/need-response type/feature
Dominant language
Python
Stars
6.7k
Forks
1.2k
Avg merge
1d 10h
Merged PRs (30d)
52

Description

**Description:**

When you use sam local invoke on a Lambda having layers, it fails with

Error: Credentials provided are missing lambda:Getlayerversion policy that is needed to download the layer or you do not have permission to download the layer

The --profile used to run the command had access lambda:* access but sam still fails with this error. Access is verified with aws commandline.

**Steps to reproduce the issue:**
1. Declare a template with Layers, something like
Layers:
- !Sub "arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:layer:XRayLayer:${XRayLayerVersion}"
2. When Lambda is locally invoked for testing :
sam local invoke -e .\test-data\test-sqs-event.json myservice --profile default --debug --region us-east-1
function invocation fails with

Error: Credentials provided are missing lambda:Getlayerversion policy that is needed to download the layer or you do not have permission to download the layer
3. Running the function in debug mode does not help, it does not reveal any additional details.
4. Running the function with start-lambda in debug mode and invoking the lambda with aws cli gives more insights, and there is a bug

sam local start-lambda --profile default --debug --region us-east-1

GetLayer call in the background is making call to a dummy account **123456789012** and not the actual account you are working with : log shows something like this

botocore.exceptions.ClientError: An error occurred (AccessDeniedException) when calling the GetLayerVersion operation: User: arn:aws:sts::[account]:assumed-role/[Role]/user is not authorized to perform: lambda:GetLayerVersion on resource: arn:aws:lambda:us-east-1:**123456789012**:layer:XRayLayer:6 and that's the bug
it should be using the account from aws --profile

See attached log for both sam local start-lambda and local invoke giving details.

[sam bugreport.txt](https://github.com/awslabs/serverless-application-model/files/4078469/sam.bugreport.txt)

**Observed result:**
AccessDeniedException

**Expected result:**
sam is supposed to download the layer specified in the sam template.

Workaround:
if you hardcode the account in layer ARN in template, it downloads the layer and then subsequent runs.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.