I am attaching the following resource policy to an SM secret
{
"Version" : "2012-10-17",
"Id" : "AllowAccessToSecretValue",
"Statement" : [ {
"Sid" : "AllowLambdaAccess",
"Effect" : "Allow",
"Principal" : {
"Service" : "lambda.amazonaws.com"
},
"Action" : [ "secretsmanager:GetSecretValue", "secretsmanager:UpdateSecret" ],
"Resource" : "arn:aws:secretsmanager:us-east-1:123456789:secret:my-secret-xkxkxk",
"Condition" : {
"ArnLike" : {
"aws:SourceArn" : "arn:aws:lambda:us-east-1:123456789:function:foo*"
}
}
} ]
}
Then I am trying to access it from a lambda
that has the following code:
secret_name = "my-secret"
def lambda_handler(event, context):
# Calling SecretsManager
get_secret_value_response = client.get_secret_value(
SecretId=secret_name
)
#Raw Response
#Extracting the key/value from the secret
secret = get_secret_value_response['SecretString']
print(secret)
put_secret_value_response = client.update_secret(
SecretId=secret_name,
SecretString='fofo'
)
secret = get_secret_value_response['SecretString']
print(secret)
The arn of the function is
arn:aws:lambda:us-east-1:123456789:function:foo-lala
The execution fails:
"errorMessage": "An error occurred (AccessDeniedException) when calling the GetSecretValue operation: User: arn:aws:sts::123456789:assumed-role/my-secret/foo-lala is not authorized to perform: secretsmanager:GetSecretValue on resource: my-secret because no identity-based policy allows the secretsmanager:GetSecretValue action",
Why is that?
update
Removing the Condition
clause entirely does not seem to work either
{
"Version" : "2012-10-17",
"Id" : "AllowAccessToSecretValue",
"Statement" : [ {
"Sid" : "AllowLambdaAccess",
"Effect" : "Allow",
"Principal" : {
"Service" : "lambda.amazonaws.com"
},
"Action" : [ "secretsmanager:GetSecretValue", "secretsmanager:UpdateSecret" ],
"Resource" : "*"
} ]
}
2
Answers
Resource is
"arn:aws:secretsmanager:us-east-1:123456789:secret:my-secret-xkxkxk"
but you are trying to accessmy-secret
!Update:
Your resource policy fails to grant the expected privileges because the Lambda service is not the caller. Rather, the Lambda service assumes the Lambda’s execution role and the *role* makes the call to Secrets Manager.
Set the Lambda role as the Principal and remove the condition:
You can match multiple roles with a pattern. Use the "Any" Principal with an
ArnLike
Condition with a wildcard pattern onaws:PrincipalArn
. This will match whatever role names match the pattern.