Lab: Testing Serverless Applications in AWS Development Environments
Test applications in development environments
Lab: Testing Serverless Applications in AWS Development Environments
This lab provides a guided experience in testing AWS Lambda functions and API Gateway endpoints within a dedicated development environment. You will learn to perform local testing, deploy a development stack, and use AWS-native tools to validate application logic.
Prerequisites
Before starting, ensure you have the following:
- AWS Account: Access to an AWS account with permissions to create Lambda, API Gateway, and IAM roles.
- AWS CLI: Installed and configured with
<YOUR_ACCESS_KEY>. - AWS SAM CLI: Installed (
sam --version). - Runtime: Python 3.9+ or Node.js 18+ installed locally.
Learning Objectives
By the end of this lab, you will be able to:
- Perform local testing of event-driven functions using SAM.
- Deploy a serverless stack to a specific 'development' stage.
- Create and manage JSON test events in the AWS Lambda Console.
- Test API Gateway endpoints using stage variables.
Architecture Overview
The following diagram illustrates the testing workflow and the resources you will provision:
Step-by-Step Instructions
Step 1: Initialize the Lab Project
We will use the AWS Serverless Application Model (SAM) to bootstrap our testing environment.
sam init --name brainybee-test-lab --runtime python3.9 --app-template hello-world --package-type Zip
cd brainybee-test-lab[!NOTE] This creates a standard directory structure:
hello_world/for code andtemplate.yamlfor infrastructure.
Step 2: Local Testing with JSON Payloads
Before deploying, we test the function locally to ensure logic is correct without incurring cloud costs.
# Generate a mock API Gateway event
sam local generate-event apigateway aws-proxy > event.json
# Invoke the function locally
sam local invoke "HelloWorldFunction" --event event.json▶Console alternative
Local testing is strictly CLI-based. To test without CLI, you must deploy the code first (see Step 3).
Step 3: Deploy to Development Environment
Now, we deploy the stack to AWS using a 'dev' suffix to keep it isolated from production.
sam deploy --guided --stack-name brainybee-dev-stack[!IMPORTANT] When prompted for 'Setting default arguments', ensure you set the Stage parameter to
dev.
Step 4: Testing via AWS Lambda Console
Once deployed, we use the console to test specific event patterns (Skill 3.3.1).
- Navigate to the Lambda Console.
- Select your function:
brainybee-dev-stack-HelloWorldFunction-.... - Click the Test tab.
- Click Create new event.
- Event name:
DevTestEvent. - Template:
apigateway-aws-proxy. - Click Save then Test.
Step 5: Validating API Gateway Stages
Confirm that your development endpoint is active and returning the expected payload.
# Replace <API_ID> with the ID from the SAM output
curl https://<API_ID>.execute-api.<YOUR_REGION>.amazonaws.com/Prod/hello/Checkpoints
| Checkpoint | Action | Expected Result |
|---|---|---|
| Local Test | Run sam local invoke | JSON response with "statusCode": 200 |
| Deployment | Check CloudFormation Console | Stack status: CREATE_COMPLETE |
| Integrated Test | Run curl command | {"message": "hello world"} |
Clean-Up / Teardown
To avoid ongoing charges, delete all resources created in this lab.
[!WARNING] Failure to run these commands will result in active resources remaining in your account.
sam delete --stack-name brainybee-dev-stackTroubleshooting
| Error | Possible Cause | Fix |
|---|---|---|
Runtime.ImportModuleError | Missing dependencies in package | Run sam build before invoking or deploying. |
AccessDenied | IAM User lacks permissions | Ensure your CLI profile has PowerUserAccess or AdministratorAccess. |
Docker not running | Local testing requires Docker | Start Docker Desktop or the dockerd service. |
Stretch Challenge
Task: Implement a Mock API.
Modify the app.py code to check for a header X-Mock-Enabled. If true, return a hardcoded JSON response without processing. Update your event.json and test it locally using sam local invoke.
Cost Estimate
| Service | Usage | Estimated Cost |
|---|---|---|
| AWS Lambda | < 1,000 requests | $0.00 (Free Tier) |
| API Gateway | < 1,000 requests | $0.00 (Free Tier) |
| CloudWatch Logs | < 5GB Storage | $0.00 (Free Tier) |
| Total | $0.00 |
Concept Review
Testing in development environments is a critical phase of the CI/CD pipeline.
- Unit Testing: Testing individual components (Lambda logic) in isolation.
- Integration Testing: Testing how components interact (API Gateway -> Lambda -> Database).
- Stage Variables: Used in API Gateway to point to different Lambda aliases or versions based on the environment (
/devvs/prod).