Hands-On Lab720 words

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:

  1. Perform local testing of event-driven functions using SAM.
  2. Deploy a serverless stack to a specific 'development' stage.
  3. Create and manage JSON test events in the AWS Lambda Console.
  4. Test API Gateway endpoints using stage variables.

Architecture Overview

The following diagram illustrates the testing workflow and the resources you will provision:

Loading Diagram...

Step-by-Step Instructions

Step 1: Initialize the Lab Project

We will use the AWS Serverless Application Model (SAM) to bootstrap our testing environment.

bash
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 and template.yaml for infrastructure.

Step 2: Local Testing with JSON Payloads

Before deploying, we test the function locally to ensure logic is correct without incurring cloud costs.

bash
# 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.

bash
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).

  1. Navigate to the Lambda Console.
  2. Select your function: brainybee-dev-stack-HelloWorldFunction-....
  3. Click the Test tab.
  4. Click Create new event.
  5. Event name: DevTestEvent.
  6. Template: apigateway-aws-proxy.
  7. Click Save then Test.

Step 5: Validating API Gateway Stages

Confirm that your development endpoint is active and returning the expected payload.

bash
# Replace <API_ID> with the ID from the SAM output curl https://<API_ID>.execute-api.<YOUR_REGION>.amazonaws.com/Prod/hello/

Checkpoints

CheckpointActionExpected Result
Local TestRun sam local invokeJSON response with "statusCode": 200
DeploymentCheck CloudFormation ConsoleStack status: CREATE_COMPLETE
Integrated TestRun 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.

bash
sam delete --stack-name brainybee-dev-stack

Troubleshooting

ErrorPossible CauseFix
Runtime.ImportModuleErrorMissing dependencies in packageRun sam build before invoking or deploying.
AccessDeniedIAM User lacks permissionsEnsure your CLI profile has PowerUserAccess or AdministratorAccess.
Docker not runningLocal testing requires DockerStart 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

ServiceUsageEstimated 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.

Compiling TikZ diagram…
Running TeX engine…
This may take a few seconds
  • 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 (/dev vs /prod).

Ready to study AWS Certified Developer - Associate (DVA-C02)?

Practice tests, flashcards, and all study notes — free, no sign-up needed.

Start Studying — Free