Testing Lambda Functions#
Your Lambda function is deployed, now let’s test it. Lambda provides several ways to invoke functions, and we’ll explore a few.
When you test a Lambda function directly (via the CLI or AWS Console), you’re not making an HTTP request to it. You’re telling AWS to execute your code with a specific payload. Take a look at this diagram:

Invoke from the CLI#
For our function, we need to simulate what API Gateway would send. When you invoke the function directly via the console, you’re providing mock data. The IP address in your test event is not your real IP address; it’s example data you’re feeding to the function.
Cold Start vs. Warm Start#
When you invoke a Lambda function, you’ll notice different performance characteristics:
- First invocation (cold start): Might take 200-500 ms. This includes the time to provision a container, load your code, and start the Python runtime.
- Subsequent invocations (warm): Might take just 1-5 ms, if AWS reuses the existing container.
The logs will show an Init Duration on the first run, that’s the cold start overhead. On subsequent runs (within a few minutes), you usually won’t see INIT_START or Init Duration.
Cold starts can be frustrating for user-facing APIs. For truly latency-sensitive applications, you might want to keep functions warm (there are techniques like scheduled invocations or provisioned concurrency). But for many use cases, a 200-500 ms cold start is totally acceptable.
Assignment#
Test your Lambda function with different scenarios.
Cost check: Each Lambda invocation counts toward your free tier (1 million requests/mo). These test invocations will use about 10-20 requests total.
- In the AWS Console, navigate to
Lambda→Functionsand openpatientping-ip-function. - Open the Test tab and create a test event named
test-ipwith this payload:
{
"requestContext": {
"identity": {
"sourceIp": "8.8.8.8"
}
},
"headers": {}
}- Run the test. You should see a nice green “success” box with a JSON response that includes a
200status code and a “Your IP address is: 8.8.8.8” message in the body. - Create another test event named
test-xffwith this payload:
{
"requestContext": {
"identity": {}
},
"headers": {
"X-Forwarded-For": "198.51.100.99"
}
}- Run
test-xffand confirm the response body is “Your IP address is: 198.51.100.99”