When it comes to serverless architectures, AWS Lambda functions and Amazon S3 storage are a powerful combination. Lambda functions can respond to changes in S3 buckets, making them an efficient way to process and manipulate data stored in S3. In this guide, we'll explore how to write sample Python code for AWS Lambda functions that interact with Amazon S3.

Before we delve into the code, it's crucial to understand the key concepts and AWS services at play here. AWS Lambda functions execute your code in response to events, such as S3 bucket changes. Amazon S3, meanwhile, is an object storage service that stores large amounts of data in a scalable and durable manner. The two services integrate well, with Lambda supporting triggers based on S3 events like file uploads, deletions, and modifications.

Setting Up the S3 Bucket and Lambda Function
To get started, you'll need to have an AWS account, an S3 bucket created, and an IAM role that grants the Lambda function permission to access the bucket. The IAM role should have a trust relationship policy that allows the Lambda function to assume the role. On the S3 side, enable versioning if you want to maintain previous versions of objects.

In the AWS Management Console, create a new Lambda function with Python 3.8 or later as the runtime. You can use the following IAM policy to grant the Lambda function access to S3:
{"Version": "2012-10-17", "Statement": [{"Effect": "Allow", "Action": ["s3:GetObject", "s3:PutObject", "s3:DeleteObject", "s3:ListBucket"], "Resource": ["arn:aws:s3:::*"]}]}
Lambda Handler and Event Source Mapping
In your Lambda function, you'll define a handler that's responsible for processing S3 events. The handler takes an event parameter that includes details about the S3 event, such as the object key, size, and event type (create, update, delete, etc.). You can use the `s3 natΓΌr [{Event.Warning Moskva, Masterise// Event.S3, HTTP/1.1
Host: example.com
Content-Type: application/json
X-Amz-Content-SHA256: UNSIGNED-PAYLOAD
X-Amz-Date: 20220101T000000Z
X-Amz-Id-1: 336d516e-5c7d-4649-82e7-6b8d53c68d86
Authorization: AWS AKIAIOSFODNN73MHEXAMPLE:lPlRv5NUGDarΓaAAud9UkHHnCGWE
Content-Length: 389
{"Records":[{"s3": {"bucket":{"name":"example-bucket"}}}]}