Bird
Raised Fist0
AWScloud~30 mins

Lambda with API Gateway pattern in AWS - Mini Project: Build & Apply

Choose your learning style10 modes available
Lambda with API Gateway pattern
📖 Scenario: You are building a simple serverless API using AWS Lambda and API Gateway. This API will respond to HTTP GET requests with a fixed message. This is a common pattern to create scalable and cost-effective web services without managing servers.
🎯 Goal: Create an AWS Lambda function and configure an API Gateway REST API to trigger this Lambda on HTTP GET requests. The Lambda should return a JSON response with a greeting message.
📋 What You'll Learn
Create a Lambda function named MyLambdaFunction with a basic handler.
Create an API Gateway REST API named MyApi.
Create a resource /greet under the API.
Create a GET method on the /greet resource that triggers the Lambda function.
Deploy the API to a stage named prod.
💡 Why This Matters
🌍 Real World
This pattern is widely used to build serverless web APIs that scale automatically and reduce operational overhead.
💼 Career
Understanding how to connect Lambda with API Gateway is essential for cloud developers and architects working with AWS serverless technologies.
Progress0 / 4 steps
1
Create the AWS Lambda function
Write the AWS CloudFormation YAML code to create a Lambda function named MyLambdaFunction with runtime python3.12, handler index.handler, and a basic inline code that returns a JSON greeting message.
AWS
Hint

Use AWS::Lambda::Function resource with Runtime set to python3.12 and inline ZipFile code for the handler.

2
Create the API Gateway REST API and resource
Add AWS CloudFormation YAML code to create an API Gateway REST API named MyApi and a resource /greet under the root resource.
AWS
Hint

Create AWS::ApiGateway::RestApi for the API and AWS::ApiGateway::Resource for the /greet path.

3
Create the GET method on /greet to trigger Lambda
Add AWS CloudFormation YAML code to create a GET method on the /greet resource that integrates with the Lambda function MyLambdaFunction. Use AWS_PROXY integration type.
AWS
Hint

Create AWS::ApiGateway::Method with HttpMethod: GET and AWS_PROXY integration to the Lambda function.

4
Deploy the API Gateway to a stage
Add AWS CloudFormation YAML code to create a deployment of the API Gateway MyApi and a stage named prod.
AWS
Hint

Create AWS::ApiGateway::Deployment with DependsOn the GET method and set StageName to prod.

Practice

(1/5)
1. What is the primary role of AWS API Gateway when used with Lambda in a serverless architecture?
easy
A. To receive HTTP requests and trigger Lambda functions
B. To store data permanently
C. To run long-running background tasks
D. To manage virtual machines

Solution

  1. Step 1: Understand API Gateway's function

    API Gateway acts as a front door to accept and process HTTP requests from clients.
  2. Step 2: Connect API Gateway to Lambda

    API Gateway triggers Lambda functions to execute backend logic upon receiving requests.
  3. Final Answer:

    To receive HTTP requests and trigger Lambda functions -> Option A
  4. Quick Check:

    API Gateway triggers Lambda = A [OK]
Hint: API Gateway handles HTTP, Lambda runs code [OK]
Common Mistakes:
  • Confusing API Gateway with data storage
  • Thinking Lambda manages servers
  • Assuming API Gateway runs background tasks
2. Which of the following is the correct way to configure an API Gateway method to trigger a Lambda function?
easy
A. Set the integration type to HTTP and specify the Lambda URL
B. Set the integration type to VPC Link and attach Lambda
C. Set the integration type to Lambda Function and select the function ARN
D. Set the integration type to MOCK and provide Lambda code inline

Solution

  1. Step 1: Identify correct integration type

    API Gateway uses 'Lambda Function' integration to directly invoke Lambda functions.
  2. Step 2: Specify Lambda function ARN

    You must provide the ARN (Amazon Resource Name) of the Lambda function to link it properly.
  3. Final Answer:

    Set the integration type to Lambda Function and select the function ARN -> Option C
  4. Quick Check:

    Integration type Lambda Function + ARN = D [OK]
Hint: Use 'Lambda Function' integration with ARN [OK]
Common Mistakes:
  • Using HTTP integration instead of Lambda Function
  • Confusing MOCK integration with Lambda
  • Incorrectly using VPC Link for Lambda
3. Consider this AWS Lambda function triggered by API Gateway:
exports.handler = async (event) => {
  return { statusCode: 200, body: JSON.stringify({ message: event.queryStringParameters.name }) };
};
What will be the response body if the API Gateway is called with URL ?name=Alice?
medium
A. {"message":"Alice"}
B. {"message":null}
C. {"message":"name"}
D. Error: event.queryStringParameters undefined

Solution

  1. Step 1: Understand event object from API Gateway

    API Gateway passes query parameters inside event.queryStringParameters, so event.queryStringParameters.name equals 'Alice'.
  2. Step 2: Check Lambda return value

    Lambda returns JSON string with message key set to 'Alice', so response body is '{"message":"Alice"}'.
  3. Final Answer:

    {"message":"Alice"} -> Option A
  4. Quick Check:

    Query param 'name=Alice' -> message = 'Alice' [OK]
Hint: Query params in event.queryStringParameters [OK]
Common Mistakes:
  • Assuming queryStringParameters is null
  • Returning raw object instead of JSON string
  • Confusing parameter name with literal string
4. You deployed an API Gateway with Lambda integration but receive a 502 Bad Gateway error when calling the API. What is the most likely cause?
medium
A. API Gateway endpoint URL is incorrect
B. Lambda function is not returning a properly formatted response
C. Lambda function has no execution role assigned
D. API Gateway method is set to MOCK integration

Solution

  1. Step 1: Understand 502 Bad Gateway meaning

    502 error usually means API Gateway received an invalid response from Lambda.
  2. Step 2: Check Lambda response format

    Lambda must return an object with statusCode and body as string; missing or malformed response causes 502.
  3. Final Answer:

    Lambda function is not returning a properly formatted response -> Option B
  4. Quick Check:

    502 error = bad Lambda response format [OK]
Hint: 502 means Lambda response format error [OK]
Common Mistakes:
  • Assuming wrong API Gateway URL causes 502
  • Ignoring Lambda execution role issues (causes 403)
  • Confusing MOCK integration with Lambda integration
5. You want to design a serverless API using API Gateway and Lambda that handles high traffic with unpredictable spikes. Which design choice best ensures scalability and cost efficiency?
hard
A. Use API Gateway with Lambda proxy integration and enable Lambda reserved concurrency
B. Use API Gateway with HTTP integration to EC2 instances behind an autoscaling group
C. Use API Gateway with MOCK integration and pre-warm Lambda functions manually
D. Use API Gateway with Lambda proxy integration and rely on Lambda's default scaling without reserved concurrency

Solution

  1. Step 1: Understand Lambda scaling behavior

    Lambda scales automatically with incoming requests without needing reserved concurrency unless limiting concurrency is required.
  2. Step 2: Evaluate cost and scalability

    Relying on default Lambda scaling avoids paying for reserved concurrency and handles unpredictable spikes efficiently.
  3. Step 3: Compare other options

    EC2 autoscaling adds server management and cost; MOCK integration doesn't run real code; reserved concurrency limits scaling.
  4. Final Answer:

    Use API Gateway with Lambda proxy integration and rely on Lambda's default scaling without reserved concurrency -> Option D
  5. Quick Check:

    Default Lambda scaling + proxy integration = C [OK]
Hint: Default Lambda scaling handles spikes best [OK]
Common Mistakes:
  • Setting reserved concurrency unnecessarily limits scaling
  • Using EC2 adds complexity and cost
  • Using MOCK integration doesn't run Lambda code
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard