Lambda and API Gateway: Serverless API Essentials Quiz Quiz

Evaluate your understanding of creating serverless APIs using Lambda functions with API Gateway. This quiz covers fundamental features, configuration, and best practices for building scalable, event-driven APIs using these key serverless technologies.

  1. Triggering Lambda Functions via API Gateway

    Which method allows an HTTP request made to an API Gateway endpoint to invoke a Lambda function directly?

    1. Integrating API Gateway with Lambda
    2. Scheduling Lambda with a cron expression
    3. Calling Lambda from an EC2 instance
    4. Attaching Lambda to an S3 event

    Explanation: Integrating API Gateway with Lambda enables HTTP requests to directly trigger Lambda functions, facilitating seamless API endpoints. Using S3 events allows storage-related triggers, not HTTP requests. Scheduling with cron executes code on a time schedule, not via API calls. Calling Lambda from a server bypasses API Gateway’s role as an HTTP interface.

  2. Mapping Request Data

    What is the primary purpose of using mapping templates in API Gateway when working with Lambda functions?

    1. Encrypting Lambda environment variables
    2. Setting concurrency limits
    3. Transforming incoming request data
    4. Assigning execution roles to Lambda

    Explanation: Mapping templates in API Gateway are primarily used to transform incoming request data before passing it to a Lambda function, ensuring compatibility and facilitating custom data formats. Encrypting environment variables is a separate security concern. Assigning execution roles pertains to permissions, not data mapping. Concurrency limits deal with how many instances run at once, unrelated to request data.

  3. API Gateway Resource Structure

    When designing a REST API with API Gateway, what does each ‘resource’ typically represent?

    1. A regional endpoint type
    2. A specific IAM policy
    3. A Lambda function's memory size
    4. A path in the API URL

    Explanation: In API Gateway, each ‘resource’ usually represents a path or endpoint within the API URL, helping organize API structure. It does not determine a function’s memory size, which is set elsewhere. IAM policies control permissions and are unrelated to API resource structure. A regional endpoint type refers to geographic placement, not path organization.

  4. Lambda Permissions for API Calls

    Which item is essential to allow API Gateway to safely invoke a Lambda function?

    1. A VPC endpoint policy
    2. A mapping template for output
    3. A scheduled event source
    4. An execution role with correct permissions

    Explanation: API Gateway must have an execution role with permission to invoke Lambda; this security measure prevents unauthorized access. A scheduled event source triggers Lambda on a timer, not through an API call. A VPC endpoint policy manages network connectivity, not API permissions. Output mapping templates transform responses but do not grant permission to execute functions.

  5. Lambda Integration Types

    When connecting API Gateway to Lambda, which integration type is commonly used to allow the API to work with Lambda’s request and response format?

    1. Lambda Proxy integration
    2. EventBridge integration
    3. HTTP endpoint integration
    4. S3 event integration

    Explanation: Lambda Proxy integration passes the full request and response data between API Gateway and Lambda, simplifying communication and enabling flexible APIs. HTTP endpoint integration connects to web URLs directly rather than Lambda. EventBridge and S3 integrations are meant for event buses and storage triggers, not for handling HTTP methods via API Gateway.

  6. Securing API Endpoints

    What is a common way to restrict unauthorized access to an API Gateway endpoint linked to a Lambda function?

    1. Assign more memory to Lambda
    2. Require an API key for access
    3. Change the function runtime version
    4. Use a mapping template only

    Explanation: Requiring an API key helps restrict endpoint access, ensuring only authorized users can invoke the Lambda function. Memory size and runtime version affect performance, not access control. Mapping templates adjust data formatting but do not provide security by themselves.

  7. Handling API Responses

    In the context of Lambda and API Gateway, how should a Lambda function format its response to return a proper HTTP status and body to the client?

    1. Return a JSON object with statusCode and body fields
    2. Use an environment variable to set the status code
    3. Output raw HTML markup
    4. Return only plain text

    Explanation: A properly formatted JSON response with statusCode and body allows API Gateway to relay appropriate HTTP responses to clients. Plain text or HTML lacks HTTP metadata needed for API responses. Using environment variables for status codes is not a supported method for response handling in this context.

  8. CORS Configuration for APIs

    Why is enabling CORS (Cross-Origin Resource Sharing) important when building APIs with Lambda and API Gateway?

    1. It sets daily usage quotas
    2. It increases storage space for data
    3. It allows web browsers to access the API from different domains
    4. It improves Lambda's execution speed

    Explanation: Enabling CORS allows client applications running on different domains to interact with your API endpoints, a common need for web applications. It has no effect on execution speed, storage space, or usage quotas, which are governed by other configuration settings.

  9. Configuring Environment Variables

    What is a recommended use for environment variables in a Lambda function managed through API Gateway?

    1. Designing REST API resources
    2. Generating unique security tokens for every request
    3. Setting up custom domain names for the API
    4. Storing configuration values like database URLs

    Explanation: Environment variables are best suited for storing static configuration values, such as database URLs or API secrets, that should not be hardcoded. Generating unique tokens should be handled per request at runtime, not as static variables. Custom domain names and resource design are managed in API Gateway, not through Lambda environment variables.

  10. Monitoring Serverless APIs

    Which mechanism provides logs and metrics for Lambda functions and API Gateway endpoints to help monitor serverless API behavior?

    1. Enabling built-in logging
    2. Increasing concurrency limits
    3. Granting public access to the API
    4. Using client-side encryption

    Explanation: Enabling built-in logging captures execution details and metrics, helping monitor and debug serverless APIs. Changing concurrency limits adjusts scaling but does not provide visibility. Granting public access impacts security, not monitoring. Client-side encryption ensures privacy but does not offer logging or metric data.