Search code examples
amazon-web-servicesamazon-s3aws-cloudformationaws-devops

Enable Amazon S3 bucket to Trigger Lambda function using SAM


I want to trigger a Lambda function whenever a file is uploaded to an Amazon S3 bucket with a certain prefix and suffix using SAM. Right now I'm using this code but it's giving error

"The ARN is not well formed(Service: Amazon S3; Status Code: 400; Error Code: InvalidArgument"

Code

enter image description here

Edit:

2nd way to do this This is working but it's not giving an option to add suffix or prefix.


Solution

  • In the NotificationConfiguration you're simply using !Ref HelloWorld to reference your function, however, as the documentation for AWS::Serverless::Function states:

    When the logical ID of this resource is provided to the Ref intrinsic function, it returns the resource name of the underlying Lambda function.

    If we look at the documentation for the LambdaConfiguration it states:

    The Amazon Resource Name (ARN) of the AWS Lambda function that Amazon S3 invokes when the specified event type occurs.

    If you simply change the !Ref HelloWorld to !GetAtt HelloWorld.Arn it should pass in the correct value.

    Beware however of the remark that is made on the NotificationConfiguration documentation that if you create the bucket at the same time as you're creating the notification configuration, you might end up with a circular dependency, since you also need to add a AWS::Lamdba::Permission (or use the Events of AWS::Serverless::Function) to allow S3 to invoke your lambda function.