0Pricing
Serverless Backend with AWS Lambda & API Gateway · Урок

Тестирование и мониторинг в рабочей среде

Реализуйте стратегии тестирования бессерверного приложения и настройте надёжный мониторинг и оповещения для рабочей среды

«Тестирование и мониторинг в рабочей среде» — бесплатный урок Serverless Backend with AWS Lambda & API Gateway на CoddyKit. Это урок 3 из 4. Ты можешь прочитать весь урок бесплатно ниже — а потом практиковать его прямо в браузере с встроенным редактором кода и ИИ-репетитором 24/7. Это часть пути обучения Serverless Backend with AWS Lambda & API Gateway, и твой прогресс синхронизируется между веб-версией и приложением CoddyKit. Курс Serverless Backend with AWS Lambda & API Gateway содержит 4 уроков всего.

Части этого урока еще не переведены и отображаются на английском.

Beyond Dev: Production Monitoring

When your serverless application goes live, testing in development isn't enough. You need robust production monitoring to ensure it's always available, performing well, and serving your users correctly.

Production monitoring focuses on real-time insights, detecting issues before they impact users, and understanding the system's health in a live environment.

Observability: Logs, Metrics, Traces

To effectively monitor a serverless application, we rely on three key pillars of observability:

  • Logs: Detailed records of events and errors from your functions.
  • Metrics: Numerical data points that show performance and usage trends.
  • Traces: End-to-end views of requests as they flow through multiple services.

These pillars help you understand what is happening, how well it's performing, and where problems are occurring.

Essential CloudWatch Metrics

AWS CloudWatch automatically collects metrics for your Lambda functions and API Gateway. Key metrics to monitor for Lambda include:

  • Invocations: How many times your function is called.
  • Errors: The number of times your function returns an error.
  • Duration: How long your function runs (latency).
  • Throttles: When Lambda rejects invocations due to concurrency limits.

Monitoring these helps you quickly spot performance degradation or failures.

Get Notified: CloudWatch Alarms

Metrics alone aren't enough; you need to be alerted when something goes wrong. CloudWatch Alarms allow you to set thresholds on your metrics.

When a metric breaches its threshold (e.g., Error count > 0 for 5 minutes), an alarm can trigger actions like sending notifications via Amazon SNS (Simple Notification Service) to your email or a chat application.

Deep Dive with CloudWatch Logs

When an alarm goes off, or you notice an issue, CloudWatch Logs are your go-to for debugging. Every print() statement or logger message from your Lambda function is sent here.

You can search, filter, and analyze these logs to understand the exact sequence of events that led to an error. Good logging practices are crucial for production debugging.

Try running this example and check its logs in CloudWatch:

import json
import logging

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

def lambda_handler(event, context):
    logger.info(f"Received event: {json.dumps(event)}")
    # Simulate some processing
    try:
        if 'fail' in event:
            raise ValueError("Simulated error for logging")
        message = "Processing successful!"
        status_code = 200
    except Exception as e:
        logger.error(f"Error during processing: {e}")
        message = f"Error: {e}"
        status_code = 500
        
    return {
        'statusCode': status_code,
        'body': json.dumps(message)
    }

Trace Requests with AWS X-Ray

Serverless applications often involve multiple services (API Gateway, Lambda, DynamoDB). When a request fails, it's hard to pinpoint where the issue occurred.

AWS X-Ray provides distributed tracing, giving you an end-to-end view of how requests travel through your application. It helps identify performance bottlenecks and errors across different services.

X-Ray: Enabling & Instrumenting

To use X-Ray, you enable it for your Lambda function and API Gateway. For Lambda, you can optionally instrument your code using the X-Ray SDK to add custom annotations or subsegments.

This allows you to capture specific details about your function's execution steps or business logic within the trace.

Run this Python Lambda with X-Ray SDK enabled:

import json
import os
from aws_xray_sdk.core import xray_recorder
from aws_xray_sdk.core.lambda_context import LambdaContext

xray_recorder.configure(service='MyServerlessApp')
xray_recorder.set_stream_strategy(LambdaContext())

def lambda_handler(event, context):
    # X-Ray automatically captures basic Lambda info
    # We can add custom subsegments or annotations
    with xray_recorder.in_segment('my_custom_processing'):
        xray_recorder.put_annotation('transaction_id', 'xyz123')
        xray_recorder.put_metadata('input_event', event)
        
        print("Function processing with X-Ray...")
        # Simulate some work
        result = {'message': 'Hello from X-Ray enabled Lambda!'}
        
    return {
        'statusCode': 200,
        'body': json.dumps(result)
    }

Understanding X-Ray Service Map

Once X-Ray is collecting data, it visualizes your application's components and their connections in a Service Map. This map shows:

  • The services involved (e.g., API Gateway, Lambda, DynamoDB).
  • The average latency between them.
  • Any errors or faults.

You can then drill down into individual traces to see the exact timeline of a request, including all subsegments and any errors.

Proactive Checks: CloudWatch Canaries

Beyond reactive monitoring, CloudWatch Synthetics Canaries offer proactive testing. Canaries are configurable scripts that run 24/7 from outside your application.

They simulate user interactions—like calling an API endpoint, loading a web page, or submitting a form—to check availability and performance. If a canary fails, it can trigger an alarm, alerting you to potential issues before your users notice them.

Monitoring Knowledge Check

Which of the following are key benefits of using AWS X-Ray in a serverless application?

Recap: Production Ready!

Congratulations! You've learned how to make your serverless applications production-ready through robust monitoring and testing strategies.

We covered the pillars of observability (logs, metrics, traces), using CloudWatch for metrics and alarms, deep-diving with CloudWatch Logs, and gaining end-to-end visibility with AWS X-Ray. Finally, we explored proactive testing with CloudWatch Synthetics Canaries.

Implementing these practices will significantly improve your application's reliability and your ability to respond to issues effectively.

Часто задаваемые вопросы

Урок «Тестирование и мониторинг в рабочей среде» бесплатный?

Да — полный текст урока «Тестирование и мониторинг в рабочей среде» бесплатно доступен здесь в веб-версии. Чтобы практиковать его интерактивно (встроенный редактор кода и ИИ-репетитор 24/7) и разблокировать остальной курс Serverless Backend with AWS Lambda & API Gateway, подпишись на CoddyKit PRO. Курс Serverless Backend with AWS Lambda & API Gateway содержит 4 уроков всего.

Чему я научусь в уроке «Тестирование и мониторинг в рабочей среде»?

Реализуйте стратегии тестирования бессерверного приложения и настройте надёжный мониторинг и оповещения для рабочей среды Ты практикуешь Serverless Backend with AWS Lambda & API Gateway с помощью реального кода, который запускаешь прямо в браузере, и ИИ-репетитор 24/7 отвечает на твои вопросы во время урока.

Нужен ли мне опыт, чтобы начать Serverless Backend with AWS Lambda & API Gateway?

Предыдущий опыт не требуется. Serverless Backend with AWS Lambda & API Gateway на CoddyKit структурирован для всех уровней — от новичков до продвинутых, поэтому ты можешь начать отсюда или с самого начала и учиться в своем темпе. Это урок 3 из 4.

Сколько времени занимает урок «Тестирование и мониторинг в рабочей среде»?

Большинство уроков CoddyKit занимают около 5–10 минут. Каждый из них компактный и интерактивный, поэтому ты постоянно делаешь прогресс и продолжаешь с того же места в веб-версии и приложении.

Можно ли писать и запускать код в этом уроке Serverless Backend with AWS Lambda & API Gateway?

Да. Каждый урок Serverless Backend with AWS Lambda & API Gateway включает встроенный редактор кода, поэтому ты пишешь и запускаешь реальный код прямо в браузере и получаешь моментальную обратную связь от AI — локальная установка не требуется.

Все уроки этого курса

  1. Проектирование бессерверного микросервиса
  2. Реализация API и бизнес-логики
  3. Тестирование и мониторинг в рабочей среде
  4. Защита и масштабирование API в рабочей среде
← Назад к Serverless Backend with AWS Lambda & API Gateway