Skip to content

Latest commit

 

History

History
122 lines (105 loc) · 4.67 KB

File metadata and controls

122 lines (105 loc) · 4.67 KB

S3 Bucket → EventBridge Rule → Lambda

S3 Bucket to EventBridge Rule to Lambda

Invoking the Lambda Function with EventBridge Rule if there is any event(s) received from the S3 Bucket. Once the Lambda Functions receives an event from the EventBridge Rule, it will print the S3 Event information in the CloudWatch Logs. When naming the bucket, it must be unique globally. If you get the "Bucket name already exists" error, you must use a different bucket name to create the bucket.

Sample S3 Event Logs

Object Created

{
  "log_code": "S3Events",
  "log_msg": "S3 Events Information",
  "log_level": "INFO",
  "log_keys": {
    "Detail": {
      "version": "0",
      "bucket": {
        "name": "order-data-us-east-1"
      },
      "object": {
        "key": "sample-data.json",
        "size": 187,
        "etag": "8507efd2f590261f7d1613f6f8744445",
        "sequencer": "006426F16D0A15AAFE"
      },
      "request-id": "A4OM5P4G1GIFNXML",
      "requester": "123456789010",
      "source-ip-address": "12.345.678.910",
      "reason": "PutObject"
    },
    "DetailType": "Object Created",
    "ID": "98528b8d-34d3-9013-79b9-a7e273a64681",
    "Source": "aws.s3"
  },
  "log_timestamp": "2023-03-31 14:42:55"
}

Object Deleted

{
  "log_code": "S3Events",
  "log_msg": "S3 Events Information",
  "log_level": "INFO",
  "log_keys": {
    "Detail": {
      "version": "0",
      "bucket": {
        "name": "order-data-us-east-1"
      },
      "object": {
        "key": "sample-data.json",
        "sequencer": "806426F0F6480F8EE8"
      },
      "request-id": "A4OM5P4G1GIFNXML",
      "requester": "123456789010",
      "source-ip-address": "12.345.678.910",
      "reason": "DeleteObject",
      "deletion-type": "Permanently Deleted"
    },
    "DetailType": "Object Deleted",
    "ID": "98528b8d-5899-8e88-682c-86b485ee489f",
    "Source": "aws.s3"
  },
  "log_timestamp": "2023-03-31 14:40:52"
}

AWS CDK API / Developer Reference

AWS SDK v2 API / Developer Reference

AWS Documentation Developer Guide

Useful commands

The cdk.json file tells the CDK Toolkit how to execute your app.

  • npm install install projects dependencies
  • npm run build compile typescript to js
  • npm run watch watch for changes and compile
  • npm run test perform the jest unit tests
  • cdk deploy deploy this stack to your default AWS account/region
  • cdk diff compare deployed stack with current state
  • cdk synth emits the synthesized CloudFormation template
  • cdk bootstrap deployment of AWS CloudFormation template to a specific AWS environment (account and region)
  • cdk destroy destroy this stack from your default AWS account/region

Deploy

Using make command

  1. Install all the dependencies, bootstrap your project, and synthesized CloudFormation template.
    # Without passing "profile" parameter
    dev@dev:~:aws-cdk-samples/s3/s3-eventbridge-lambda$ make init
    
    # With "profile" parameter
    dev@dev:~:aws-cdk-samples/s3/s3-eventbridge-lambda$ make init profile=[profile_name]

2. Deploy the project.
  ```bash
  # Without passing "profile" parameter
  dev@dev:~:aws-cdk-samples/s3/s3-eventbridge-lambda$ make deploy

  # With "profile" parameter
  dev@dev:~:aws-cdk-samples/s3/s3-eventbridge-lambda$ make deploy profile=[profile_name]
  ```