Want my AWS s3 Bucket to read Name from CloudWatch Event

+1 vote

Currently, I am writing a Lambda function that triggers when a new s3 bucket is created (under my project).
I am using a cloudwatch function which triggers the lambda function. 

I have to pass the whole event to the lambda function as input. What do I do to get my Lambda function so that it can read the bucket's name from the event and assign the name as the value to a string variable?

I am using this code below, I have written it myself, might be some mistakes, please correct me where I am lacking and I need to do to accomplish the task that I have mentioned above, thanks in advance and yeah now take a look at my code below:

import boto3
from botocore.exceptions import ClientError

s3 = boto3.client('s3')  

def lambda_handler(event, context):

    bucket = event['s3']['bucket']['name']
May 28, 2018 in AWS by Flying geek
• 3,280 points
2,008 views

2 answers to this question.

+1 vote

CloudTrail events for S3 bucket level operations have the different format than the other events. Actually, the name of bucket is within a JSON object called requestParameters. Also, the whole event is encapsulated within the Records array.
See CloudTrail Log Event Reference.

A truncated version of CloudTrail event for bucket creation is here, this code below might be able to help you out: 

"eventSource": "s3.amazonaws.com",
"eventName": "CreateBucket",
"userAgent": "signin.amazonaws.com",
"requestParameters": {
    "CreateBucketConfiguration": {
        "LocationConstraint": "aws-region",
        "xmlns": "http://s3.amazonaws.com/doc/2006-03-01/"
    },
    "bucketName": "my-awsome-bucket"
}
Therefore, your code could look something like:

import boto3
from botocore.exceptions import ClientError

s3 = boto3.client('s3')  

def lambda_handler(event, context):
    for record in event['Records']:
        if record['eventName'] == "CreateBucket":
            bucket = record['requestParameters']['bucketName']
            print(bucket)
answered May 28, 2018 by Cloud gunner
• 4,670 points
0 votes
answered Sep 4, 2018 by eatcodesleeprepeat
• 4,710 points

Related Questions In AWS

0 votes
1 answer
0 votes
0 answers

I want to get file name from key in S3 bucket wanted to read single file from list of file present in bucket

1 <class 'boto.s3.key.Key'> <Key: numbers-email, staging/Procured_Numbers_Status/procured_numbers_status_2019-05-15:06:09:04.csv> I ...READ MORE

May 15, 2019 in AWS by anonymous
6,890 views
+6 votes
14 answers

Python AWS Boto3: How do i read files from S3 Bucket?

You can use the following code, import boto3 s3 ...READ MORE

answered Dec 7, 2018 in AWS by Nitesh
315,226 views
+1 vote
2 answers

AWS CloudWatch Logs in Docker

The awslogs works without using ECS. you need to configure ...READ MORE

answered Sep 7, 2018 in AWS by bug_seeker
• 15,520 points
1,958 views
+2 votes
1 answer

Deploy Docker Containers from Docker Cloud

To solve this problem, I followed advice ...READ MORE

answered Sep 3, 2018 in AWS by Priyaj
• 58,100 points
2,648 views
+1 vote
3 answers

Which is better ? AWS S3 bucket logs vs AWS cloudtrail

CloudTrail logs API calls accessed to your ...READ MORE

answered Aug 16, 2018 in AWS by Priyaj
• 58,100 points
7,217 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP