1
votes

Thanks in advance!

So I have created this Cloudformation template below and have a circular dependency error.. I know what is causing the error, but cannot think of a solution for what I'm trying to achieve..

Which is;

  • Create a lambda function that has a two environment variables for two buckets I need to use in the functions code

  • Create two s3 buckets, one for a file input and one for a file output

  • Create a trigger that calls the lambda function when an object is added to the 1st bucket

Here is my code:

      "lambda": {
            "Type": "AWS::Lambda::Function",
            "DependsOn": [
                "s3accessrole",
                "s3rolepolicies",
                "bucket1"
            ],
            "Properties": {
                "Code": {
                    "S3Bucket": "resource-bucket",
                    "S3Key": "filepath/function.zip"
                },
                "Role": {
                    "Fn::GetAtt": [
                        "s3accessrole",
                        "Arn"
                    ]
                },
                "Timeout": 60,
                "Handler": "function.handler",
                "Runtime": "nodejs6.10",
                "MemorySize": 1024,
                "Environment": {
                    "Variables": {
                        "bucket1": {
                            "Ref": "bucket1"
                        },
                        "bucket2": {
                            "Ref": "bucket2"
                        }
                    }
                }
            },
            "Metadata": {
                "AWS::CloudFormation::Designer": {
                    "id": "XXXX"
                }
            }
        },
        "bucket1": {
            "Type": "AWS::S3::Bucket",
            "Properties": {
                "AccessControl": "Private",
                "LifecycleConfiguration": {
                    "Rules": [
                        {
                            "ExpirationInDays": "1",
                            "Id": "delete images/",
                            "Status": "Enabled"
                        }
                    ]
                },
                "VersioningConfiguration": {
                    "Status": "Suspended"
                },
                "NotificationConfiguration": {
                    "LambdaConfigurations": [
                        {
                            "Event": "s3:ObjectCreated:*",
                            "Function": {
                                "Ref": "lambda"
                            }
                        }
                    ]
                }
            },
            "Metadata": {
                "AWS::CloudFormation::Designer": {
                    "id": "XXXX"
                }
            }
        },
        "lambdaperm": {
            "Type": "AWS::Lambda::Permission",
            "Properties": {
                "Action": "lambda:InvokeFunction",
                "FunctionName": {
                    "Ref": "lambda"
                },
                "Principal": "s3.amazonaws.com",
                "SourceAccount": {
                    "Ref": "AWS::AccountId"
                },
                "SourceArn": {
                    "Fn::Join": [
                        ":",
                        [
                            "arn",
                            "aws",
                            "s3",
                            "",
                            "",
                            {
                                "Ref": "bucket1"
                            }
                        ]
                    ]
                }
            },
            "Metadata": {
                "AWS::CloudFormation::Designer": {
                    "id": "XXXX"
                }
            }
        },
        "bucket2": {
            "Type": "AWS::S3::Bucket",
            "Properties": {
                "AccessControl": "Private",
                "LifecycleConfiguration": {
                    "Rules": [
                        {
                            "ExpirationInDays": "1",
                            "Id": "delete images/",
                            "Status": "Enabled"
                        }
                    ]
                },
                "VersioningConfiguration": {
                    "Status": "Suspended"
                }
            },
            "Metadata": {
                "AWS::CloudFormation::Designer": {
                    "id": "XXXX"
                }
            }
        }
1
Why does your Lambda code need the name of the two buckets? If it is using one as input and one as output, then the incoming Event will provide the name of the input bucket. Therefore, you could (1) Create the output bucket, (2) Create the Lambda function with a ref to the output bucket, (3) Create the input bucket with a Trigger pointing to the Lambda function. No circles! - John Rotenstein

1 Answers

0
votes

The incoming event from the first bucket in the lambda function contained the name used for that bucket and thus I didnt need to create an environment variable.