AWS Lambda is a service that runs your code only when something triggers it, you don’t manage servers.

Lambda part

  1. Function code
    • Your Python file + lambda_handler(event, context)
    • This is the logic (compute bucket size, write DynamoDB, make plot…)
  2. Runtime
    • The language environment Lambda provides (Python 3.11, Node.js, Java…)
    • Determines what built-in libraries are available
  3. Trigger (Event source)
    • What causes Lambda to run
    • Examples: S3 upload/delete, API Gateway request, EventBridge schedule
  4. Execution role (IAM Role)
    • Permissions for your Lambda to call AWS services
    • Example: allow s3:ListBucket, dynamodb:PutItem, CloudWatch Logs
  5. Configuration
    • Memory, timeout, environment variables, architecture (x86_64/arm64)
    • Example: TABLE_NAME, BUCKET_NAME, TIME_WINDOW_SECONDS
  6. Layers (optional)
    • Extra libraries or shared code
    • Example: matplotlib layer for plotting
  7. Logging/Monitoring (CloudWatch)
    • Where print() output goes
    • Used by TAs to verify it ran and debug errors

boto3 create Lambda compulsory part

  • Function Name
  • Runtime
    • language environment:python
  • Role
    • Lambda must assume an execution role
    • e.g., arn:aws:iam::<account-id>:role/<role-name>
  • Handler
    • Entry point, tells Lambda which Python file and which function to call when the Lambda is invoked.
    • format: <file_name>.<function_name>
  • Code

Manual setting

Step1: Create Lambda

Step2: Create Trigger

Step3: Create Code source

Step4: Grant the Lambda Role Access to S3 and DynamoDB
{
	"Version": "2012-10-17",
	"Statement": [
		{
			"Sid": "AllowListBucket",
			"Effect": "Allow",
			"Action": [
				"s3:ListBucket"
			],
			"Resource": "arn:aws:s3:::assign2-test-bucket-alan"
		},
		{
			"Sid": "AllowPutItemToDDB",
			"Effect": "Allow",
			"Action": [
				"dynamodb:PutItem"
			],
			"Resource": "arn:aws:dynamodb:us-west-1:*:table/S3-object-size-history"
		}
	]
}