Skip to content

Instantly share code, notes, and snippets.

@DavidWells
Forked from Pwntus/serverless.yml
Created October 29, 2017 04:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidWells/048bc855606331ab15bfdbd2ecd616f6 to your computer and use it in GitHub Desktop.
Save DavidWells/048bc855606331ab15bfdbd2ecd616f6 to your computer and use it in GitHub Desktop.
SLS existing S3 lambda trigger
functions:
TriggerFunc:
handler: handlers.main
resources:
Resources:
# Bucket 'TriggeredBucket'
TriggeredBucket:
Type: AWS::S3::Bucket
DependsOn:
- TriggerFuncLambdaPermissionTriggeredBucketS3
Properties:
BucketName: my-unique-bucket-name
NotificationConfiguration:
LambdaConfigurations:
- Event: s3:ObjectCreated:*
Function:
Fn::GetAtt:
- TriggerFuncLambdaFunction
- Arn
CorsConfiguration:
CorsRules:
- AllowedHeaders:
- "*"
AllowedMethods:
- PUT
AllowedOrigins:
- "*"
# Lambda 'TriggerFunc' bucket permission
TriggerFuncLambdaPermissionTriggeredBucketS3:
Type: AWS::Lambda::Permission
DependsOn:
- TriggerFuncLambdaFunction
Properties:
FunctionName:
Fn::GetAtt:
- TriggerFuncLambdaFunction
- Arn
Action: lambda:InvokeFunction
Principal: s3.amazonaws.com
SourceArn: arn:aws:s3:::my-unique-bucket-name
@ablankenship10
Copy link

Hi David, does this code still work for you? I've been dealing with errors trying to set this up for hours and I can't seem to find a solution. My setup is from what I can tell nearly identical to your code.

The error I get is fairly useless:
An error occurred: S3BucketContent - Unable to validate the following destination configurations (Service: Amazon S3; Status Code: 400; Error Code: InvalidArgument; Request ID: 90359047BABA63E7; S3 Extended Request ID: y8UhP9g6deB+EOjYAN7SOwAhU75CEXIFy8ivZNzgYg66hYEg0TB3h8qnYRjY3+VTHZ/pfPUW8jg=).

serverless.yml

functions:
  ContentCreate:
    handler: src/controllers/content.create

resources:
  Resources:
    S3BucketContent:
      Type: AWS::S3::Bucket
      DependsOn: ContentCreateLambdaPermissionContentS3
      Properties:
        AccessControl: Private
        BucketName: ${self:custom.content_bucket}
        CorsConfiguration:
          CorsRules:
            - AllowedOrigins:
                - '*'
              AllowedHeaders:
                - '*'
              AllowedMethods:
                - GET
                - PUT
        NotificationConfiguration:
          LambdaConfigurations:
            - Event: s3:ObjectCreated:*
              Function:
                Fn::GetAtt:
                - ContentCreateLambdaFunction
                - Arn
    ContentCreateLambdaPermissionContentS3:
      Type: AWS::Lambda::Permission
      DependsOn: ContentCreateLambdaFunction
      Properties:
        FunctionName:
          Fn::GetAtt:
          - ContentCreateLambdaFunction
          - Arn
        Principal: s3.amazonaws.com
        Action: lambda:InvokeFunction
        SourceAccount:
          Ref: AWS::AccountId
        SourceArn: arn:aws:s3:::{$self:custom.content_bucket}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment