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

S3 Event triggers not working

Open wieshka opened this issue 7 years ago • 45 comments

Hi, I am facing an issue where Event is not being created and associated with Lambda, although it is specified in SAM:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Description: Example

Resources:   
  LogToWatch:
    Type: AWS::Serverless::Function
    Properties:
      Handler: index.handler
      Runtime: python3.6
      Timeout: 300
      Policies: AmazonS3ReadOnlyAccess
      Events:
        S3CreateObject:
          Type: S3
          Properties:
            Bucket: 
              Ref: TargetBucket
            Events: s3:ObjectCreated:Put
  TargetBucket:
    Type: AWS::S3::Bucket

wieshka avatar Feb 13 '18 10:02 wieshka

Finished up actual code - can be found here to replicate issue: https://github.com/wieshka/additional-cloudfront-metrics-from-access-logs

wieshka avatar Feb 13 '18 14:02 wieshka

@wieshka What do you mean by 'Event is not being created'?

Are you not seeing the AWS::Lambda::Permission or a missing NotificationConfiguration Property on the bucket?

I deployed the template you have above, uploaded a file to S3, and saw the LambdaFunction be triggered. Am I missing something?

jfuss avatar Feb 20 '18 16:02 jfuss

If you use the S3 event in SAM, then you don't see any trigger in the Lambda configuration panel. But the Lambda function is executed when you drop a file in the S3 bucket. image

bottemav avatar Mar 21 '18 15:03 bottemav

I'm seeing the same behavior as @bottemav

joeferraro avatar Mar 21 '18 19:03 joeferraro

Used the provided template to reproduce the issue.

The Lambda permissions created by SAM looks like this:

{
  "Version": "2012-10-17",
  "Id": "default",
  "Statement": [
    {
      "Sid": "LogToWatchS3CreateObjectPermission-F3WNVM1OUAMX",
      "Effect": "Allow",
      "Principal": {
        "Service": "s3.amazonaws.com"
      },
      "Action": "lambda:invokeFunction",
      "Resource": "arn:aws:lambda:<region>:<account_id>:function:LogToWatch-1WQI7PUOH1EF",
      "Condition": {
        "StringEquals": {
          "AWS:SourceAccount": "<account_id>"
        }
      }
    }
]
}

But Lambda expects this in order to show the trigger in the console:

{
  "Version": "2012-10-17",
  "Id": "default",
  "Statement": [
    {
      "Sid": "lambda-a5c4fbbd-61fc-4b08-82b4-7dd593c7f65f",
      "Effect": "Allow",
      "Principal": {
        "Service": "s3.amazonaws.com"
      },
      "Action": "lambda:InvokeFunction",
      "Resource": "arn:aws:lambda:us-east-1:303769779339:function:test-LogToWatch-1WQI7PUOH1EF",
      "Condition": {
        "StringEquals": {
          "AWS:SourceAccount": "303769779339"
        },
        "ArnLike": {
          "AWS:SourceArn": "<S3 bucket Arn>"
        }
      }
    }
  ]
}

It is missing this part:

"ArnLike": {
          "AWS:SourceArn": "<S3 bucket Arn>"
}

In the cloudformation template, the AWS::Lambda::Permission resource after transformed looks like this:

"LogToWatchS3CreateObjectPermission": {
      "Type": "AWS::Lambda::Permission",
      "Properties": {
        "Action": "lambda:invokeFunction",
        "SourceAccount": {
          "Ref": "AWS::AccountId"
        },
        "FunctionName": {
          "Ref": "LogToWatch"
        },
        "Principal": "s3.amazonaws.com"
      }
    }

It is missing the SourceArn property. It should be something like this:

"LogToWatchS3CreateObjectPermission": {
      "Type": "AWS::Lambda::Permission",
      "Properties": {
        "Action": "lambda:invokeFunction",
        "SourceAccount": {
          "Ref": "AWS::AccountId"
        },
        "FunctionName": {
          "Ref": "LogToWatch"
        },
        "Principal": "s3.amazonaws.com",
        "SourceArn": {
             "Fn::GetAtt": "TargetBucket.Arn"
         }
      }
    }

However the issue is that, currently SAM makes the permission not scoped to a specific bucket. If we are to fix it in SAM making it scoped to a specific bucket so that it can show up properly in Lambda console, this could potentially break existing customer who expects broader permissions.

honglu avatar Jul 30 '18 17:07 honglu

Just wondering why this is closed? I'm seeing the same issue. The bucket event source does not show up in the console. It makes it very confusing to know what is going on.

OndeVai avatar Nov 24 '18 23:11 OndeVai

If you use the S3 event in SAM, then you don't see any trigger in the Lambda configuration panel. But the Lambda function is executed when you drop a file in the S3 bucket. image

I get this problem too, when I deploy app to the aws, I config the s3 trigger, but I don't see it on the aws console, I also tried the dynamodb or api gateway trigger and they can show on the aws console. I don't know why?

Another question is I can't set the bucket name when I config s3 event; I find the older doc config they can set the s3 bucket name. Is there something different between the old and now version ?

wuxiongliu1 avatar Nov 26 '18 06:11 wuxiongliu1

Currently experiencing this issue, any developments?

gartlady avatar Jan 30 '19 16:01 gartlady

@gartlady could you provide any additional information? The S3 triggers still don't show up in the lambda console, but should still work. Are you saying that you can't see the trigger or that the trigger doesn't work?

keetonian avatar Jan 31 '19 19:01 keetonian

The S3 triggers are working but they are not appearing in the lambda console.

gartlady avatar Jan 31 '19 21:01 gartlady

i am experiencing similar issue with SQS events. using SAM to deploy lambda with SQS event the lambda receives messages from the queue but the trigger is not visible via AWS console.

However the issue is that, currently SAM makes the permission not scoped to a specific bucket. If we are to fix it in SAM making it scoped to a specific bucket so that it can show up properly in Lambda console, this could potentially break existing customer who expects broader permissions.

please note that when using the Events you are expecting SPECIFIC permissions. giving broader permissions is an issue not a feature. when creating a lambda using SAM and giving an S3 as a trigger i expect that only that S3 is able to trigger the lambda. giving broader permissions than that seems unsecured.

errdos avatar Feb 19 '19 15:02 errdos

Also experiencing the same issue.

dmenin avatar Feb 22 '19 09:02 dmenin

Experienced the same issue. I can see no development was done for one year. Do someone knows another way to configure the same configuration (without SAM)?

smuryginim avatar Mar 03 '19 07:03 smuryginim

Experienced the same issue. I can see no development was done for one year. Do someone knows another way to configure the same configuration (without SAM)?

use: Type: AWS::Lambda::EventSourceMapping https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-eventsourcemapping.html

  exampleTrigger:
    Properties:
      BatchSize: 1
      Enabled: true
      EventSourceArn:
        Fn::GetAtt:
          - source
          - Arn
      FunctionName:
        Fn::GetAtt:
          - CreatedLambda
          - Arn
    Type: AWS::Lambda::EventSourceMapping

errdos avatar Mar 03 '19 11:03 errdos

Experienced the same issue. I can see no development was done for one year. Do someone knows another way to configure the same configuration (without SAM)?

use: Type: AWS::Lambda::EventSourceMapping https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-eventsourcemapping.html

  exampleTrigger:
    Properties:
      BatchSize: 1
      Enabled: true
      EventSourceArn:
        Fn::GetAtt:
          - source
          - Arn
      FunctionName:
        Fn::GetAtt:
          - CreatedLambda
          - Arn
    Type: AWS::Lambda::EventSourceMapping

I can see that for event source arn only next values are available

Amazon Kinesis – Default 100. Max 10,000. Amazon DynamoDB Streams – Default 100. Max 1,000. Amazon Simple Queue Service – Default 10. Max 10.

No S3 option.

smuryginim avatar Mar 04 '19 19:03 smuryginim

Experiencing same issue here. How do we open this again?

hernangarcia avatar Apr 01 '19 13:04 hernangarcia

This is a breaking change, so we wouldn't be able to do this unless we made a new version of SAM. I agree, though, that this should be fixed.

Lu's comment (https://github.com/awslabs/serverless-application-model/issues/300#issuecomment-408950770) describes what needs to be changed.

keetonian avatar Apr 02 '19 17:04 keetonian

Same issue here.

MikelDelTio avatar Apr 03 '19 15:04 MikelDelTio

~Late to the party, but I think I have the problem with a particular Lambda not showing a Cloudwatch trigger in the console. My other Lambdas (with basically the same trigger) show them in the console.~

~Seems like the Function Policy went missing at some point, although after creating the lambda 2 years ago it was there.~

Actually it was because my trigger was attached to a particular ALIAS that it didn't show up in the console for the "root" Lambda. Switching the version dropdown made it come back. I noticed this when looking at the rules in Cloudwatch, as the rule shows its targets at the bottom of the console, and clicking the target will jump to the correct version.

zaus avatar May 15 '19 15:05 zaus

Does everyone resolve this issue? I've got the same issue. The Cloud formation just only create the permission resource.

phong-innomizetech avatar Jun 03 '19 10:06 phong-innomizetech

Still having the issue as well. The "fix" to specify the Bucket Arn in the Permission is not viable because it will create an implicit loop between the bucket and the permission: bucket needs the permission to create the notification configuration but the permission needs the bucket arn.

pierremarieB avatar Jun 05 '19 11:06 pierremarieB

@phongtran227 @pierremarieB

You can add a AWS::Lambda::Permission to your Resources. It should look something like the following:

  LambdaInvokePermission:
    Type: 'AWS::Lambda::Permission'
    Properties:
      FunctionName: !GetAtt MyFunction.Arn
      Action: 'lambda:InvokeFunction'
      Principal: 's3.amazonaws.com'
      SourceAccount: !Sub ${AWS::AccountId}
      SourceArn: !GetAtt MyBucket.Arn

That worked for me.

henrikbjorn avatar Jul 12 '19 11:07 henrikbjorn

@henrikbjorn I tried your suggestion but it doesn't show the S3 triggers on console. Pretty new to this so using docs and all to find my way. I using SAM CLI to bundle and upload packaged artifact to an S3 bucket. To test I simply pass the S3 URL of uploaded artifact to Lambda function, everything else shows up but no S3 trigger shown on console, no errors either.

moosakhalid avatar Aug 06 '19 04:08 moosakhalid

@moosakhalid You also need access to the S3 bucket. I found that it is important to specify Version when specifying policies.

I have only tried where i created the s3 bucket in the same CloudFormation template.

henrikbjorn avatar Aug 06 '19 07:08 henrikbjorn

@henrikbjorn thanks for the help and hints. I finally got my SAM deployment to work though thanks to your Lambda Permissions snippet! Your method worked because that's the only way SAM/CFN deployment will let you refer to an S3 bucket i.e. if it's created within same template. It is rather annoying but I guess it's there for a good reason perhaps.

moosakhalid avatar Aug 07 '19 06:08 moosakhalid

Confirmed that @henrikbjorn Lambda Permissions snippet worked for us too. Thanks!

darrenfurr avatar Aug 27 '19 19:08 darrenfurr

The @henrikbjorn Lambda Permission snippet wasn't working for me by itself, but then the observation by @moosakhalid that the resources had to be created in the same template triggered an idea (no pun).

Despite having @henrikbjorn snippet, when I tried to add the trigger manually, I got "The provided execution role does not have permissions to call ReceiveMessage on SQS (Service: AWSLambda". Ah ha.

My SQS queue is being created in a different template, and I'm using SAM managed policy SQSPoller in the function Policies statement to allow lambda to read the queue.

However, I note that when you click "Edit Permission Boundary" on the Lambda Application page, the dialog notes:

This policy statement includes ARNs for the resources created in your SAM template. If your application needs access to resources outside of the SAM template, add those resource ARNs to the statement.

Indeed. The Permission Boundary didn't include permissions granted by SQSPoller policy for the Lambda function to access the externally created resource that I am importing with ImportValue.

So in addition to @henrikbjorn snippet, I copied the SQSPoller permissions from the Lambda Execution Role into the PermissionsBoundaryPolicy, et voila.

Now my trigger shows in the console.

normangilmore avatar May 03 '20 17:05 normangilmore

This leads me to conclude that the actual bug is the failure of SAM / CloudFormation to error with an explanation that permissions are incomplete. Whereas the Console does give a reasonably clear error that it thinks permissions are inadequate and won't add the trigger until they are fixed.

normangilmore avatar May 03 '20 17:05 normangilmore

https://github.com/aws-cloudformation/aws-cloudformation-coverage-roadmap/issues/79

keetonian avatar May 20 '20 16:05 keetonian

If you use the S3 event in SAM, then you don't see any trigger in the Lambda configuration panel. But the Lambda function is executed when you drop a file in the S3 bucket. image

This saved me a lot of time.. thank you

kartheek207 avatar Jun 06 '20 06:06 kartheek207