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:
- Declare a template with Layers, something like
Layers:
- !Sub "arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:layer:XRayLayer:${XRayLayerVersion}" - 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.
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.