serverless-application-model icon indicating copy to clipboard operation
serverless-application-model copied to clipboard

Circular dependency when specifying bucket reference in policy

Open Pelto opened this issue 9 years ago • 9 comments

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

Pelto avatar May 22 '17 17:05 Pelto

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.

sanathkr avatar Jun 28 '17 08:06 sanathkr

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/

expertcoder avatar Apr 21 '18 22:04 expertcoder

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}'

monkut avatar May 30 '19 06:05 monkut

This proposal over on the cloudformation coverage roadmap repo, would improve this situation: https://github.com/aws-cloudformation/aws-cloudformation-coverage-roadmap/issues/79

rosskarchner avatar Aug 06 '19 14:08 rosskarchner

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.

ztyree42 avatar Oct 16 '20 02:10 ztyree42

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

monkut avatar Mar 14 '21 02:03 monkut

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 avatar Aug 04 '21 00:08 kieronjmckenna

@kieronjmckenna a million thank you's for that nugget of wisdom!

benchaikin avatar Feb 08 '22 19:02 benchaikin

@kieronjmckenna very crucial nugget of wisdom indeed. Irrespective of the other solutions, not having the global environment variable seems necessary.

Viswa4599 avatar Feb 25 '24 06:02 Viswa4599