---
title: AWS - Events - S3 Events
---

You can write Lambda functions to process S3 bucket events, such as the object-created or 
object-deleted events. For example, when a user uploads a photo to a bucket, you might want Amazon S3 to invoke 
your Amazon S3 function so that it reads the image and creates a thumbnail for the photo.

[Supported Event Types](http://docs.aws.amazon.com/AmazonS3/latest/dev/NotificationHowTo.html)

## Add a new S3 bucket resource

`PROJECT_DIR/cloudformation/s3-template.yml` :

```yaml
AWSTemplateFormatVersion: "2010-09-09"
Description: "Dynamo Stack"
Resources:
  myS3Bucket:
    Type: "AWS::S3::Bucket"
```

Include the `s3-template.yml` file in the main stack.

`PROJECT_DIR/cloudformation/main-template.yml` :

```yaml
Resources:
  s3Stack:
    Type: "AWS::CloudFormation::Stack"
    Properties:
      TemplateURL: s3-template.yml
      TimeoutInMinutes: 10
```

## Hook a Lambda function to the myS3BucketName events source

`sqz.config.yml` :

```yaml
functions:
  testFunction:
    handler: "test"
    events:
      - type: s3
        bucket: "s3Stack.myS3Bucket"
        event: "s3:ObjectCreated:*"
        filter: # optional filtering
          S3Key:
            Rules:
              - Name: "suffix"
                Value: "zip"
```

`handler.js` :

```js
exports.test = (event, context) => {
  console.log(JSON.stringify(event, null, 2))
  context.succeed(event);
};
```

`$ sqz deploy`

Upload a file to your S3 bucket and check the CloudWatch log to see if your Lambda function
was triggered .

`$ sqz logs --function testFunction`

Log's output should be something like this :

```json
{
  "Records": [
    {
      "eventVersion": "2.0",
      "eventSource": "aws:s3",
      "awsRegion": "us-central-1",
      "eventTime": "2017-01-31T13:43:57.901Z",
      "eventName": "ObjectCreated:Put",
      "userIdentity": {
        "principalId": "AAAAAAAAAAAAA"
      },
      "requestParameters": {
        "sourceIPAddress": "10.10.10.10"
      },
      "responseElements": {
        "x-amz-request-id": "1D6D325C455E30DB",
        "x-amz-id-2": "FqQ1+bM3s6Ax4DAbj2euGXaadw3J/V82YHbeB3rcFUb21WwiLyNe34EzDROYLDTvP3egvnnwxl4="
      },
      "s3": {
        "s3SchemaVersion": "1.0",
        "configurationId": "b7a61c8f-ea1a-461a-b96c-2195b73eaa16",
        "bucket": {
          "name": "myfirstproject-dev-s3stack-b2vxs34mfzv-mys3bucket-zmphq9kfm4jb",
          "ownerIdentity": {
            "principalId": "A1ZZZZZXAA1NXZ"
          },
          "arn": "arn:aws:s3:::myfirstproject-dev-s3stack-b2vxs34cfzv-mys3bucket-zmphr9kfm4jb"
        },
        "object": {
          "key": "uploaded-image.jpg",
          "size": 82203,
          "eTag": "939b03122ca34e201803a27d992560ab",
          "sequencer": "005890949DCC336A2C"
        }
      }
    }
  ]
}
```
