I have this Lambda and this DynamoDB table
my_lambda = lambda_.Function(
self,
"my_lambda",
function_name="my_lambda",
description="A Lambda to test permissions",
code=lambda_code,
memory_size=512,
handler="my_lambda.main",
runtime=lambda_.Runtime.PYTHON_3_9,
architecture=lambda_.Architecture.ARM_64,
timeout=Duration.minutes(1),
)
table = dynamodb.Table(
self,
'test_table',
partition_key=dynamodb.Attribute(
name="id",
type=dynamodb.AttributeType.STRING,
),
)
Now, if I want to give the Lambda access to write in the DynameDB table I do this.
table.grant_full_access(my_lambda)
This works perfectly. Now, if I want to give this same Lambda access to the table be getting a reference to it it doesn't work.
lambda_by_arn = lambda_.Function.from_function_arn(
self,
"my lambda by arn",
my_lambda.function_arn
)
table.grant_full_access(lambda_by_arn)
The above doesn't work and the Lambda has no access whatsoever to the DynamoDB function.
If you have the DynamoDB creation in a different stack than the Lambda, you cannot do it any other way (at least, to my knowledge) than by the function_from_arn
method.
I tried getting the Lambda from a different method: function_from_attributes
but this resulted in the same way: No access.
TL;DR - The iam.Grant methods like
grant_full_access
*sometimes* work on externally referenced resources returned fromSomething.fromSomethingAttributes
methods. Unfortunately, *not* for DynamoDB Table resources.You should be seeing a warning produced by the CDK CLI when you
synth
the app:This is telling you the CDK didn't grant access - do it yourself! The CDK made a design decision to warn, but not to throw an error1.
Under what conditions can an externally reference
ISomething
construct successfully be granted IAM privileges?ISomething
's role reference must be passed toSomething.fromSomethingAttributes
This table summarizes what happens in various case. Your case is on the bottom right:
fromSomethingAttributes
+role: IRole
fromSomethingAttributes
, norole
resource imported without a role
fromSomethingArn
resource imported without a role
cdk synth --strict