我使用无服务器框架,尝试构建存储在AWS Secrets Manager中的a Lambda function that periodically rotates a secret。
我在配置Secret Manager执行Lambda所需的角色时遇到麻烦。在我的serverless.yml
中,我定义了以下资源:
resources:
Resources:
RotateKeysRole:
Type: AWS::IAM::Role
Properties:
RoleName: rotate-keys-role
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
- secretsmanager.amazonaws.com
Action: sts:AssumeRole
将此角色附加到旋转Lambda上,如下所示:
functions:
rotateKeys:
handler: lambdas.rotate_keys.handler
role: RotateKeysRole
但是,当我尝试设置Secrets Manager来使用此Lambda旋转秘密时,我将收到以下错误消息:
Secrets Manager无法调用指定的Lambda函数。确保 功能策略授予对主体的访问权限 secretsmanager.amazonaws.com
这使我感到困惑,因为指定了这个主体。检查IAM控制台中的角色并没有发现我觉得有什么问题。
在这种情况下如何正确配置角色设置?
答案 0 :(得分:1)
我今天有同样的问题。我运行了它,它为我工作: AWS Lambda添加权限-函数名称ARN_of_lambda_function-主要secretsmanager.amazonaws.com --action lambda:InvokeFunction --statement-id SecretsManagerAccess
https://docs.aws.amazon.com/secretsmanager/latest/userguide/troubleshoot_rotation.html
答案 1 :(得分:0)
您的政策不正确。 该服务是secretsmanager,但是您定义的操作是来自AWS安全令牌服务的sts:AssumeRole。
完整的访问策略为:
Effect: "Allow"
Action: "secretsmanager:*"
Resource: "*"
但是您应该限制lambda可以使用的操作和资源。 为此,您可以使用IAM-> Policies中的policy builder。
在编辑器中创建策略后,您可以单击JSON选项卡并查看格式。然后,您需要使其适应您的无服务器Yaml格式。
希望我能帮助您!
多米尼克
答案 2 :(得分:0)
文档中介绍了为lambda函数设置权限的过程,该函数可旋转AWS Secrets Manager机密。 [1]
要概括地说,您需要执行两个步骤:
<function-name-with-first-letter-uppercase>LambdaFunction
。注意:函数名称在 DependsOn 属性中引用。在条件 StringEquals 和属性 FunctionName 中,它也被引用为:arn:aws:lambda:${self:custom.region}:${self:custom.accountId}:function:${self:service}-${self:provider.stage}-rotateKeys
。如果您更改函数名称,请记住要更改它们。
这是serverless.yml文件的外观:
service:
name: <your-service-name>
provider:
name: aws
region: '<your-region>'
custom:
region: ${self:provider.region}
accountId: <your-account-id>
resources:
Resources:
FunctionRole:
Type: AWS::IAM::Role
Properties:
RoleName: basic-function-role
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Policies:
- PolicyName: rotateKeysPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- secretsmanager:DescribeSecret
- secretsmanager:GetSecretValue
- secretsmanager:PutSecretValue
- secretsmanager:UpdateSecretVersionStage
Resource: '*'
Condition:
StringEquals:
'secretsmanager:resource/AllowRotationLambdaArn': "arn:aws:lambda:${self:custom.region}:${self:custom.accountId}:function:${self:service}-${self:provider.stage}-rotateKeys"
- Effect: Allow
Action:
- secretsmanager:GetRandomPassword
Resource: '*'
- Effect: Allow
Action:
- ec2:CreateNetworkInterface
- ec2:DeleteNetworkInterface
- ec2:DescribeNetworkInterfaces
Resource: '*'
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action: sts:AssumeRole
LambdaInvokePermission:
Type: AWS::Lambda::Permission
DependsOn: RotateKeysLambdaFunction
Properties:
FunctionName: "arn:aws:lambda:${self:custom.region}:${self:custom.accountId}:function:${self:service}-${self:provider.stage}-rotateKeys"
Action: lambda:InvokeFunction
Principal: 'secretsmanager.amazonaws.com'
functions:
rotateKeys:
handler: lambdas.rotate_keys.handler
role: FunctionRole
您必须替换<your-service-name>
,<your-region>
,<your-account-id>
,并使用例如package -> include
属性。
注意:有些lambda函数模板可更新机密。 [2] [3]
还请记住,为lambda函数正确配置VPC,使其能够通过网络访问AWS Secrets Manager服务。 [4]
[1] https://docs.aws.amazon.com/secretsmanager/latest/userguide/rotating-secrets-required-permissions.html
[2] https://docs.aws.amazon.com/secretsmanager/latest/userguide/rotating-secrets-create-generic-template.html
[3] https://github.com/aws-samples/aws-secrets-manager-rotation-lambdas
[4] https://docs.aws.amazon.com/secretsmanager/latest/userguide/rotation-network-rqmts.html