question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Circular dependency when adding s3 upload event

See original GitHub issue

Description:

I’m trying to do just as what you have here in this example, I always encounter this error:

Error: Failed to create changeset for the stack: sls-my-sample-app, ex: Waiter ChangeSetCreateComplete failed: Waiter encountered a terminal failure state Status: FAILED. Reason: Circular dependency between resources: [forgotPasswordConfirmApiEventPermissionStage, mainBackendApi, changeProfilePictureApiEventPermissionStage, resendEmailCodeApiEventPermissionStage, verifyEmailApiEventPermissionStage, profilePictureUploadedUploadPermission, mainBackendApiDeploymentad50fa5e91, forgotPasswordSendApiEventPermissionStage, changeProfilePicture, loginApiEventPermissionStage, changePersonalInfoApiEventPermissionStage, usersS3Bucket, mainBackendApiStage, profilePictureUploaded, profilePictureUploadedRole, changeProfilePictureRole, registerApiEventPermissionStage]

This is what my template looks like:

AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: >
  sls-my-sample-app

  Sample SAM Template for sls-my-sample-app
  
Parameters:
  Stage:
    Description: Required staged of the current build
    Type: String
    Default: dev

# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
  Function:
    MemorySize: 512
    Timeout: 60
    Runtime: nodejs12.x
    Environment:
      Variables:
        STAGE: !Ref Stage
        USERS_BUCKET: !Ref usersS3Bucket
  Api:
    OpenApiVersion: 3.0.1

Resources:
  register:
    Type: AWS::Serverless::Function
    Properties:
      Handler: src/functions/guest/register.handler
      Layers:
        - !Ref dependencies
      Policies:
        - Statement:
          - Action:
              - ses:SendEmail
            Effect: Allow
            Resource: "*"
      Events:
        ApiEvent:
          Type: Api
          Properties:
            Method: post
            Path: /register
            RestApiId:
              Ref: mainBackendApi
  forgotPasswordSend:
    Type: AWS::Serverless::Function
    Properties:
      Handler: src/functions/guest/forgotPasswordSend.handler
      Layers:
        - !Ref dependencies
      Policies:
        - Statement:
            - Action:
                - ses:SendEmail
              Effect: Allow
              Resource: "*"
      Events:
        ApiEvent:
          Type: Api
          Properties:
            Method: post
            Path: /forgot-password-send
            RestApiId:
              Ref: mainBackendApi
  forgotPasswordConfirm:
    Type: AWS::Serverless::Function
    Properties:
      Handler: src/functions/guest/forgotPasswordConfirm.handler
      Layers:
        - !Ref dependencies
      Policies:
        - Statement:
            - Action:
                - ses:SendEmail
              Effect: Allow
              Resource: "*"
      Events:
        ApiEvent:
          Type: Api
          Properties:
            Method: post
            Path: /forgot-password-confirm
            RestApiId:
              Ref: mainBackendApi
  login:
    Type: AWS::Serverless::Function
    Properties:
      Handler: src/functions/guest/login.handler
      Layers:
        - !Ref dependencies
      Events:
        ApiEvent:
          Type: Api
          Properties:
            Method: post
            Path: /login
            RestApiId:
              Ref: mainBackendApi
  verifyEmail:
    Type: AWS::Serverless::Function
    Properties:
      Handler: src/functions/auth/verifyEmail.handler
      Layers:
        - !Ref dependencies
      Policies:
        - Statement:
          - Action:
              - ses:SendEmail
            Effect: Allow
            Resource: "*"
      Events:
        ApiEvent:
          Type: Api
          Properties:
            Method: post
            Path: /verify-email
            RestApiId:
              Ref: mainBackendApi
  resendEmailCode:
    Type: AWS::Serverless::Function
    Properties:
      Handler: src/functions/auth/resendEmailCode.handler
      Layers:
        - !Ref dependencies
      Policies:
        - Statement:
          - Action:
              - ses:SendEmail
            Effect: Allow
            Resource: "*"
      Events:
        ApiEvent:
          Type: Api
          Properties:
            Method: post
            Path: /resend-email-code
            RestApiId:
              Ref: mainBackendApi
  changePersonalInfo:
    Type: AWS::Serverless::Function
    Properties:
      Handler: src/functions/auth/changePersonalInfo.handler
      Layers:
        - !Ref dependencies
      Events:
        ApiEvent:
          Type: Api
          Properties:
            Method: post
            Path: /change-personal-info
            RestApiId:
              Ref: mainBackendApi
  changeProfilePicture:
    Type: AWS::Serverless::Function
    Properties:
      Handler: src/functions/auth/changeProfilePicture.handler
      Layers:
        - !Ref dependencies
      Policies:
        - Statement:
          - Action:
              - s3:PutObject
              - s3:PutObjectAcl
            Effect: Allow
            Resource:
              - !Join
                - ""
                - - !GetAtt usersS3Bucket.Arn
                  - "/*.jpg"
              - !Join
                - ""
                - - !GetAtt usersS3Bucket.Arn
                  - "/*.jpeg"
              - !Join
                - ""
                - - !GetAtt usersS3Bucket.Arn
                  - "/*.png"
      Events:
        ApiEvent:
          Type: Api
          Properties:
            Method: post
            Path: /change-profile-picture
            RestApiId:
              Ref: mainBackendApi
  profilePictureUploaded:
    Type: AWS::Serverless::Function
    Properties:
      Handler: src/events/s3/profilePictureUploaded.handler
      MemorySize: 2048
      Layers:
        - !Ref dependencies
      Policies:
        - Statement:
          - Action:
              - s3:GetObject
            Effect: Allow
            Resource:
              - !Join
                - ""
                - - !GetAtt usersS3Bucket.Arn
                  - "/*.jpg"
              - !Join
                - ""
                - - !GetAtt usersS3Bucket.Arn
                  - "/*.jpeg"
              - !Join
                - ""
                - - !GetAtt usersS3Bucket.Arn
                  - "/*.png"
      Events:
        Upload:
          Type: S3
          Properties:
            Bucket: !Ref usersS3Bucket
            Events: s3:ObjectCreated:Put
            Filter:
              S3Key:
                Rules:
                  - Name: prefix
                    Value: newProfilePicture_
  dependencies:
    Type: AWS::Serverless::LayerVersion
    Properties:
      CompatibleRuntimes:
        - nodejs12.x
      ContentUri: src/dependencies
      LayerName: sls-my-sample-app
      RetentionPolicy: Retain
  mainBackendApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: !Ref Stage
  usersS3Bucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Join
        - "-"
        - - "sls-my-sample-app-users"
          - !Ref Stage
      CorsConfiguration:
        CorsRules:
        - AllowedHeaders:
            - "*"
          AllowedMethods:
            - GET
            - PUT
            - HEAD
          AllowedOrigins:
            - "*"
      Tags:
        - Key: "what"
          Value: "sls-my-sample-app users s3 bucket"

The error is coming from profilePictureUploaded function, probably because of the S3 event, can you help me out?

Steps to reproduce:

Follow the template above.

Observed result:

Throws circular dependency.

Expected result:

Should not throw circular dependency

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

  1. OS: ubuntu-latest on Github Actions
  2. sam --version: SAM CLI, version 1.15.0

Add --debug flag to command you are running

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
aprilmintacpinedacommented, Jan 21, 2021

I changed the policy to:

Policies:
  - S3ReadPolicy:
      BucketName: !Join
        - "-"
        - - "my-app-users-s3-bucket"
          - !Ref Stage

And on the environment variable to:

usersBucket: !Join
  - "-"
  - - "my-app-users-s3-bucket"
    - !Ref Stage

And it worked, I can see the function there now. I still don’t know what went wrong there, how come when I added an s3 event the !Ref myBucket just fails…

Also, I’ve been looking for a way to declare variables, so I don’t have to copy-paste

!Join
  - "-"
  - - "my-app-users-s3-bucket"
    - !Ref Stage

multiple times.

0reactions
github-actions[bot]commented, Aug 5, 2021

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Resolving circular dependency in provisioning of Amazon S3 ...
In this article, I present a mechanism to resolve the circular dependency while preserving the desired outcome of auto-generated S3 bucket names ...
Read more >
Getting around circular CloudFormation Dependencies: S3 ...
Getting around circular CloudFormation dependencies. Several posts complain about the inability of CloudFormation to apply a Lambda event ...
Read more >
Circular dependency between resources AWS::S3/LAMBDA
Ref BlobsBucket Events: s3:ObjectCreated:* This will fail with: Error: Failed to create changeset for the stack: blob, ex: Waiter ...
Read more >
Avoiding Circular References - Educative.io
So in order to set up the upload bucket, it would need to know which function reference is expecting bucket events. So the...
Read more >
AWS::S3::Bucket NotificationConfiguration - 亚马逊云科技
If you create the target resource and related permissions in the same template, you might have a circular dependency. For example, you might...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found