Circular dependency when specifying bucket reference in policy
I'm attempting to set up a serverless stack that sets up a lambda function that listens to S3 events while maintaining least privilege rights to the bucket. When I deploy the template I receive a circular dependency error between the function, the bucket, role and the lambda permission. A shorter example can be seen below:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: AWS Serverless bug
Resources:
Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub "{AWS::StackName}-${AWS::Region}-${AWS::AccountId}"
Function:
Type: AWS::Serverless::Function
Properties:
CodeUri: lambda/
Runtime: nodejs6.10
Handler: index.handler
Policies:
- Version: 2012-10-17
Statement:
- Effect: Allow
Action: s3:GetObject*
Resource: !Sub "arn:aws:s3:::${Bucket}*"
Events:
Upload:
Properties:
Bucket:
Ref: Bucket
Events: s3:ObjectCreated:*
Type: S3
The above stack gives the following error message when trying to deploy:
Failed to create the changeset: Waiter ChangeSetCreateComplete failed: Waiter encountered a terminal failure state Status: FAILED. Reason: Circular dependency between resources: [Function, Bucket, FunctionRole, FunctionUploadPermission]
I have tried a few different approaches to this, such as moving the permission into a separate role and referencing the role and by managed policies. However all those attempts resulted in the same circular dependency error.
However, if I instead set the resource in the policy to !Sub "arn:aws:s3:::${AWS::StackName}-${AWS::Region}-${AWS::AccountId}*" then the circular dependency is gone and the stack deploys as expected.
Example code for the bug can be found here: https://github.com/Pelto/aws-serverless-bucket-policy-permission-bug
Yeah this one is a bit tricky. Here is the loop:
Bucket -> BucketNotificationConfiguration ->LambdaFunction -> ExecutionRole -> Bucket
You have to break the loop somehow, and setting the bucket name like how you do is the best option.
It seems like this limitation (or similar one) has been documented and work arounds proposed by AWS. https://aws.amazon.com/premiumsupport/knowledge-center/unable-validate-circular-dependency-cloudformation/
Just want to note I had the same issue using the template policies.
Where if I referenced the template bucket object for the BucketName I would get a circular dependency error, but if I changed the reference to use !Sub 'bucket-${id} in place of !Ref bucket the deployment proceeded.
Example:
- S3CrudPolicy:
BucketName: !Sub 'mybucket-${RCSVPCStageName}-${ProjectIdentifierSuffix}'
This proposal over on the cloudformation coverage roadmap repo, would improve this situation: https://github.com/aws-cloudformation/aws-cloudformation-coverage-roadmap/issues/79
Worth noting that the solution that @Pelto described w/ breaking cycle via hard-coding naming no longer works apparently. Which is too bad since it leaves only the incredibly ugly custom resource hack.
Another note:
Hit the same issue again, and in the process of trying to resolve it set 'DependsOn' in the function definition to the Bucket. This caused a circular dependency issue even when only setting the Event without a policy defined.
This causes a circular dependency issue:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: AWS Serverless bug
Resources:
Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub "{AWS::StackName}-${AWS::Region}-${AWS::AccountId}"
Function:
Type: AWS::Serverless::Function
DependsOn: Bucket
Properties:
CodeUri: lambda/
Runtime: nodejs6.10
Handler: index.handler
Events:
Upload:
Properties:
Bucket:
Ref: Bucket
Events: s3:ObjectCreated:*
Type: S3
In case it saves someone some time.
A global environment variable that references the Bucket resource will also cause a circular dependency. cfn-lint will not point you towards the global env variable, it will only point you to the Bucket and Lambda resources.
@kieronjmckenna a million thank you's for that nugget of wisdom!
@kieronjmckenna very crucial nugget of wisdom indeed. Irrespective of the other solutions, not having the global environment variable seems necessary.