问题描述
我在我的 aws lambda 中使用 javascript,它调用 SSM getParameter 并且 lambda 的执行角色对我希望获取其值的密钥具有适当的权限。
最初,我收到此特定错误:
ERROR AccessDeniedException: User: arn:aws:sts::123456789:assumed-role/my-role/my-lambda is not authorized to perform: ssm:GetParameter on resource: arn:aws:ssm:us-east-1:123456789:parameter/my/parameter
这个特定的错误是有道理的,所以我更正了我的策略,因为我不小心把 GetParameters 放在了 GetParameters 上。但是现在,我收到了这个可疑的错误(注意末尾的 *):
ERROR AccessDeniedException: User: arn:aws:sts::123456789:assumed-role/my-role/my-lambda is not authorized to perform: ssm:GetParameter on resource: arn:aws:ssm:us-east-1:123456789:*
这个错误对我来说没有任何意义。为什么我需要访问每个参数?我只需要我要的那个。
相关代码如下:
const data = await ssm.getParameter({Name: 'my/parameter',WithDecryption: true}).promise();
以及权限策略(在 CloudFormation 模板中创建)
MyLambdaRole:
Type: AWS::IAM::Role
Properties:
RoleName: my-role
Description: my-role description
Policies:
- PolicyName: cloudwatch-access
...
- PolicyName: parameter-store-access
PolicyDocument:
Statement:
- Effect: Allow
Action:
- "ssm:GetParameter"
Resource:
- !Sub "arn:aws:ssm:*:${AWS::AccountId}:parameter/my/parameter"
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- 'sts:AssumeRole'
编辑:
ERROR ValidationException: Parameter name: can't be prefixed with "ssm" (case-insensitive). If formed as a path,it can consist of sub-paths divided by slash symbol; each sub-path can be formed as a mix of letters,numbers and the following 3 symbols .-_
解决方法
看起来解决方案是在参数名称前加上“/”。
在我的示例中,我使用了 {Name: 'my/parameter'}
使用 {Name: '/my/parameter'}
后,一切似乎都正常。
谈论一个毫无意义的错误信息...