Chapter 3: Manual Creation and Configuration of Lambda Functions

Haiyue
13min

Chapter 3: Manual Creation and Configuration of Lambda Functions

Learning Objectives
  • Create Lambda functions through the AWS Console
  • Configure basic parameters and environment variables
  • Set up function permissions and execution roles
  • Test and debug Lambda functions

Key Concepts

Steps to Create a Lambda Function

The complete process of creating a Lambda function includes the following main steps:

🔄 正在渲染 Mermaid 图表...

Function Creation Methods Comparison

Creation MethodUse CaseAdvantagesDisadvantages
Author from scratchCustom requirementsFull controlNeed to manually configure everything
Use a blueprintCommon patternsQuick startLimited templates
Container imageComplex dependenciesFlexible deploymentComplex image management
Browse serverless app repositoryExisting codeQuick migrationMust meet AWS application requirements

Example Operations

1. Basic Function Configuration

Create a basic Python Lambda function through the AWS Console:

import json
import logging

# Configure logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)

def lambda_handler(event, context):
    """
    Basic Lambda function example

    Args:
        event: Input event data
        context: Lambda runtime context

    Returns:
        dict: HTTP response format
    """

    # Log event information
    logger.info(f"Received event: {json.dumps(event)}")

    # Extract parameters from event
    name = event.get('name', 'World')
    message = event.get('message', 'Hello')

    # Construct response
    response = {
        'statusCode': 200,
        'headers': {
            'Content-Type': 'application/json',
            'Access-Control-Allow-Origin': '*'
        },
        'body': json.dumps({
            'greeting': f"{message}, {name}!",
            'timestamp': context.aws_request_id,
            'function_name': context.function_name,
            'remaining_time': context.get_remaining_time_in_millis()
        })
    }

    logger.info(f"Returning response: {response}")
    return response

2. Environment Variables Configuration Example

import os
import json
import boto3
from botocore.exceptions import ClientError

def lambda_handler(event, context):
    """
    Lambda function example using environment variables
    """

    # Get configuration from environment variables
    region = os.environ.get('AWS_REGION', 'us-east-1')
    table_name = os.environ.get('DYNAMODB_TABLE', 'default-table')
    debug_mode = os.environ.get('DEBUG', 'false').lower() == 'true'
    api_key = os.environ.get('API_KEY')

    # Validate required environment variables
    if not api_key:
        return {
            'statusCode': 500,
            'body': json.dumps({
                'error': 'API_KEY environment variable not set'
            })
        }

    # Use environment variables
    if debug_mode:
        print(f"Debug mode enabled")
        print(f"Region: {region}")
        print(f"Table: {table_name}")

    try:
        # Use DynamoDB
        dynamodb = boto3.resource('dynamodb', region_name=region)
        table = dynamodb.Table(table_name)

        # Example operation
        response = table.scan(Limit=1)

        return {
            'statusCode': 200,
            'body': json.dumps({
                'message': 'Configuration loaded successfully',
                'region': region,
                'table': table_name,
                'debug': debug_mode,
                'items_count': response.get('Count', 0)
            })
        }

    except ClientError as e:
        return {
            'statusCode': 500,
            'body': json.dumps({
                'error': f"DynamoDB error: {str(e)}"
            })
        }

3. IAM Execution Role Configuration

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "lambda.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "logs:CreateLogGroup",
        "logs:CreateLogStream",
        "logs:PutLogEvents"
      ],
      "Resource": "arn:aws:logs:*:*:*"
    },
    {
      "Effect": "Allow",
      "Action": [
        "dynamodb:GetItem",
        "dynamodb:PutItem",
        "dynamodb:UpdateItem",
        "dynamodb:DeleteItem",
        "dynamodb:Scan",
        "dynamodb:Query"
      ],
      "Resource": "arn:aws:dynamodb:*:*:table/MyTable"
    },
    {
      "Effect": "Allow",
      "Action": [
        "s3:GetObject",
        "s3:PutObject"
      ],
      "Resource": "arn:aws:s3:::my-bucket/*"
    }
  ]
}

4. Test Event Configuration

# API Gateway test event
api_gateway_test_event = {
    "httpMethod": "POST",
    "path": "/hello",
    "headers": {
        "Content-Type": "application/json",
        "Authorization": "Bearer test-token"
    },
    "queryStringParameters": {
        "name": "Alice"
    },
    "body": json.dumps({
        "message": "Hello",
        "timestamp": "2024-01-01T00:00:00Z"
    }),
    "requestContext": {
        "requestId": "test-request-id",
        "stage": "test",
        "httpMethod": "POST",
        "path": "/hello"
    }
}

# S3 event test event
s3_test_event = {
    "Records": [
        {
            "eventSource": "aws:s3",
            "eventName": "ObjectCreated:Put",
            "eventVersion": "2.1",
            "eventTime": "2024-01-01T00:00:00.000Z",
            "s3": {
                "bucket": {
                    "name": "my-test-bucket",
                    "arn": "arn:aws:s3:::my-test-bucket"
                },
                "object": {
                    "key": "uploads/test-file.jpg",
                    "size": 1024,
                    "etag": "test-etag"
                }
            }
        }
    ]
}

def lambda_handler(event, context):
    """
    Lambda function handling multiple event types
    """

    # Determine event type
    if 'httpMethod' in event:
        # API Gateway event
        return handle_api_request(event, context)
    elif 'Records' in event and event['Records']:
        # S3 or other service event
        return handle_record_event(event, context)
    else:
        # Custom event
        return handle_custom_event(event, context)

def handle_api_request(event, context):
    """Handle API Gateway request"""
    method = event['httpMethod']
    path = event['path']

    return {
        'statusCode': 200,
        'body': json.dumps({
            'message': f'Handled {method} request to {path}',
            'requestId': context.aws_request_id
        })
    }

def handle_record_event(event, context):
    """Handle Records event"""
    processed_records = []

    for record in event['Records']:
        source = record.get('eventSource', 'unknown')
        processed_records.append({
            'source': source,
            'eventName': record.get('eventName', 'unknown')
        })

    return {
        'statusCode': 200,
        'body': json.dumps({
            'message': f'Processed {len(processed_records)} records',
            'records': processed_records
        })
    }

def handle_custom_event(event, context):
    """Handle custom event"""
    return {
        'statusCode': 200,
        'body': json.dumps({
            'message': 'Handled custom event',
            'event': event
        })
    }

Function Configuration Details

Basic Configuration Options

🔄 正在渲染 Mermaid 图表...

Memory and Performance Configuration

Memory (MB)CPU CapacityUse CasesCost Impact
128-512LowSimple APIs, text processingLowest cost
512-1024MediumData processing, API callsBalanced cost-performance
1024-3008HighImage processing, complex computationsHigh performance needs
3008-10240Very HighMachine learning, big dataHighest cost
Configuration Recommendations
  • Memory Configuration: Adjust based on actual needs; too high wastes costs, too low affects performance
  • Timeout Settings: Recommended 30 seconds or less for API functions, longer for batch processing
  • Environment Variables: Use KMS encryption for sensitive information, avoid hardcoding
  • Execution Role: Follow the principle of least privilege, grant only necessary permissions

Debugging and Monitoring

import json
import logging
import time
from datetime import datetime

# Configure structured logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

def lambda_handler(event, context):
    """
    Lambda function with detailed monitoring and debugging information
    """

    start_time = time.time()

    # Log request start
    logger.info({
        'event': 'function_start',
        'request_id': context.aws_request_id,
        'function_name': context.function_name,
        'memory_limit': context.memory_limit_in_mb,
        'remaining_time': context.get_remaining_time_in_millis()
    })

    try:
        # Business logic processing
        result = process_business_logic(event, context)

        # Log success
        processing_time = (time.time() - start_time) * 1000
        logger.info({
            'event': 'function_success',
            'processing_time_ms': processing_time,
            'request_id': context.aws_request_id
        })

        return result

    except Exception as e:
        # Log error
        processing_time = (time.time() - start_time) * 1000
        logger.error({
            'event': 'function_error',
            'error_type': type(e).__name__,
            'error_message': str(e),
            'processing_time_ms': processing_time,
            'request_id': context.aws_request_id
        })

        return {
            'statusCode': 500,
            'body': json.dumps({
                'error': 'Internal server error',
                'requestId': context.aws_request_id
            })
        }

def process_business_logic(event, context):
    """
    Simulate business logic processing
    """

    # Add business metrics
    logger.info({
        'event': 'business_logic_start',
        'input_size': len(json.dumps(event))
    })

    # Simulate processing time
    time.sleep(0.1)

    return {
        'statusCode': 200,
        'body': json.dumps({
            'message': 'Success',
            'timestamp': datetime.utcnow().isoformat(),
            'processed_at': context.aws_request_id
        })
    }
Common Issues
  • Permission Errors: Ensure execution role has permissions to access required resources
  • Timeout Issues: Check function execution time, adjust timeout settings appropriately
  • Out of Memory: Monitor memory usage, adjust allocation as needed
  • Dependency Issues: Ensure all dependency packages are correctly packaged and uploaded
Debugging Tips
  • Use CloudWatch Logs to view detailed logs
  • Leverage AWS X-Ray for distributed tracing
  • Use SAM for local function testing
  • Use test events to validate different scenarios