0

I followed this tutorial to setup an AWS Lambda function that is invoked upon an upload to S3 and populates DynamoDB.

I'm trying to achieve the same with AWS SAM for which I need to define a template.yaml file with the configuration information. I keep getting this error when deploying with Cloudformation -

Failed to create the changeset: Waiter ChangeSetCreateComplete failed: Waiter encountered a terminal failure state Status: FAILED. Reason: Circular dependency between resources: [LambdaPerm]

I could not find a lot of information on this so I'm struggling to debug. What's causing this error and how can I resolve this? Here's my template configuration -

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
  Gradebook:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: LambdaGradebookServerless
      Handler: serverless.LambdaGradebook
      Runtime: java8
      CodeUri: ./target/serverless-0.0.1-SNAPSHOT.jar
      Role: arn:aws:iam::xxxxxxxxxxxx:role/lambda-s3-execution-role
  LambdaPerm:
    Type: AWS::Lambda::Permission
    Properties:
      Action: lambda:InvokeFunction
      FunctionName:
        Ref: Gradebook
      Principal: s3.amazonaws.com
      SourceAccount:
        Ref: AWS::xxxxxxxxxxxx
      SourceArn:
        Fn::Join:
        - ':'
        - - arn
          - aws
          - s3
          - ''
          - ''
          - Ref: gradebookBucket
  gradebookBucket:
    Type: AWS::S3::Bucket
    Properties:
      Bucket: gradebook-lambda
      NotificationConfiguration:
        LambdaConfigurations:
        - Event: s3:ObjectCreated:*
          Function:
            Ref: Gradebook
Anish Sana
  • 518
  • 1
  • 12
  • 30
  • 1
    BTW: You can use !Ref "AWS::AccountId" rather than hard-coding your account ID. Not the cause of your circular dependency though. – Ken Krueger Jan 03 '18 at 01:43

3 Answers3

1

To avoid this circular dependency, create the S3 bucket and the Lambda function independently, then update the stack with an S3 notification configuration.

jarmod
  • 71,565
  • 16
  • 115
  • 122
1

If anyone comes searching for this, I wrote a blog to address this scenario. https://aws.amazon.com/blogs/mt/resolving-circular-dependency-in-provisioning-of-amazon-s3-buckets-with-aws-lambda-event-notifications/

The implementation approach is similar to what jarmod describes in the accepted answer. The event notification is setup later using a CloudFormation custom resource.

vsnyc
  • 2,117
  • 22
  • 35
0

I got the circular dependency error and it turns out it was a missing parameter that I was referencing from the resource on the CloudFormation template!.

Juan Zapata
  • 1,818
  • 14
  • 19