Running Load Tests and Invoking AWS Services for Testing
Some tests cannot run inside a single build container: load tests need many concurrent clients, performance benchmarks need a production-like environment, and custom checks need other AWS services. This lesson explains how to run load and stress tests at scale on AWS, how to benchmark performance between releases, and how CodePipeline invokes AWS Lambda, AWS Step Functions, Amazon Inspector and CodeBuild to run tests and checks.
Learning outcomes
After completing this lesson, you will be able to:
- Distinguish between load testing, stress testing and performance benchmarking.
- Describe approaches for generating load at scale with CodeBuild, Amazon ECS on AWS Fargate and the Distributed Load Testing on AWS solution.
- Explain how a Lambda invoke action reports success or failure to CodePipeline.
- Describe when to use Step Functions, Amazon Inspector scan and CodeBuild test actions in a pipeline.
Load, Stress and Benchmark Tests
Load testing applies the expected production load, for example the traffic of a peak hour, and checks that latency and error rates stay within objectives. Stress testing increases load beyond expectations to find the breaking point and to observe how the system fails: whether it sheds load gracefully, whether auto scaling reacts in time and whether it recovers when load falls. Soak testing holds a moderate load for hours to reveal memory leaks and resource exhaustion. Performance benchmarking runs a repeatable test against each release and compares the results with a baseline, so that a regression, such as a 20% increase in p99 latency, fails the pipeline even if absolute objectives are still met.
These tests belong in a staging environment that matches production in instance types, scaling configuration and data volume. Running them against a smaller environment produces misleading results, and running them against production risks affecting users unless the production environment is designed for it.
Load, stress or benchmark?
Generating Load at Scale
A single build container can generate only limited load. Several approaches distribute load generation across many workers, and they are compared after the descriptions.
CodeBuild batch builds
A build-fanout or build-list batch runs the same load tool, such as Apache JMeter, Locust, k6 or Gatling, in many parallel builds. This needs no extra infrastructure and integrates directly with the pipeline, but each build is limited to the compute types and duration of CodeBuild.
Amazon ECS tasks on AWS Fargate
A pipeline step starts a number of ECS tasks running the load tool, for example with the RunTask API from a Step Functions state machine, and collects their results from S3 or CloudWatch. Fargate removes instance management and scales to hundreds of tasks, which suits large or long tests.
Distributed Load Testing on AWS
Distributed Load Testing on AWS is an AWS Solutions implementation deployed from a CloudFormation template. It provides a web console and an API for defining tests, runs the load generators as Fargate tasks, can generate load from multiple Regions, and stores results with dashboards in CloudWatch. It suits organisations that want a ready-made, shared load testing service rather than building their own.
Whatever generates the load, the decision to pass or fail should be automated: the test tool or a follow-up step compares results such as p95 latency and error rate with thresholds or the baseline and exits non-zero when they are exceeded. CloudWatch metrics from the system under test, such as Auto Scaling activity and database CPU, show whether the system scaled as designed.
Testing responsibly
Load tests against AWS resources are permitted for your own resources, but tests that resemble distributed denial-of-service attacks or that target endpoints such as CloudFront at high rates should follow the AWS simulated event and network stress testing policy. Throttling quotas of services such as Lambda and API Gateway also apply during tests and may need increases before a large test.
Invoking AWS Lambda from a Pipeline
The AWS Lambda invoke action calls a function and passes it a job event that contains the job ID, the input artifact locations, temporary credentials to read and write artifacts, and the user parameters string from the action configuration. The function performs any custom work, such as running an API test, checking a third-party service, invalidating a CloudFront distribution or comparing benchmark results, and must then report the outcome by calling PutJobSuccessResult or PutJobFailureResult with the job ID.
If the function returns without calling either API, the action does not complete and eventually times out and fails. The function's execution role therefore needs codepipeline:PutJobSuccessResult and codepipeline:PutJobFailureResult. For long-running work, the function can call PutJobSuccessResult with a continuation token, which makes CodePipeline invoke the function again later so that it can check progress, avoiding the 15-minute Lambda limit. The function can also return output variables to later actions.
import boto3, urllib.request
codepipeline = boto3.client("codepipeline")
def handler(event, context):
job = event["CodePipeline.job"]
url = job["data"]["actionConfiguration"]["configuration"]["UserParameters"]
try:
with urllib.request.urlopen(url + "/health", timeout=10) as response:
if response.status != 200:
raise RuntimeError(f"health check returned {response.status}")
codepipeline.put_job_success_result(jobId=job["id"])
except Exception as error:
codepipeline.put_job_failure_result(
jobId=job["id"], failureDetails={"type": "JobFailed", "message": str(error)[:500]})Invoke actions
Invoking Step Functions, Amazon Inspector and CodeBuild
Other invoke and test actions cover more complex or specialised checks.
- AWS Step Functions invoke action: starts a state machine execution with an input that can include variables, waits for it to complete and succeeds or fails with it. Use it for multi-step test workflows, such as seeding data, starting load generators on Fargate, waiting, collecting results and evaluating thresholds, which would be awkward in a single Lambda function.
- Amazon Inspector scan action: scans a source code artifact or an ECR image for vulnerabilities and fails the action if findings exceed configured severity thresholds, which adds a security gate without a custom build step.
- CodeBuild as a test action: a CodeBuild project placed in the Test category runs a test buildspec against a deployed environment, using stack outputs passed as variables for endpoints. It is the most common way to run integration and end-to-end tests.
Choosing between these actions is a matter of scope: a CodeBuild test action for tests that run as commands, a Lambda invoke action for a short custom check or integration, a Step Functions action for an orchestrated multi-step workflow and the Inspector scan action for vulnerability gates.
A Performance Gate in Staging
An API must not be released if its p99 latency under the expected peak load of 2,000 requests per second is more than 10% worse than the previous release. The test needs about 40 load generators for 20 minutes.
-
Orchestrate
A Step Functions invoke action in the Staging stage starts a state machine with the release version as input.
-
Generate load
The state machine runs 40 Fargate tasks with the load tool using the ECS RunTask integration and waits for them to finish.
-
Evaluate
A Lambda state reads the aggregated results from S3 and the baseline from the previous release and fails the execution if p99 latency regressed by more than 10%.
-
Record
On success, the new results become the baseline for the next release.
A Lambda invoke action always times out even though the function's logs show it finishing successfully in two seconds. What is missing?
Show answer
The function does not call PutJobSuccessResult (or PutJobFailureResult) with the job ID, or its role lacks permission to do so. CodePipeline waits for that call, not for the function to return.
Exam Tips
Remember for the exam
- Load tests check expected load, stress tests find breaking points and benchmarks compare releases; run them in a production-like staging environment.
- Generate large load with Fargate tasks or the Distributed Load Testing on AWS solution; use CodeBuild batch builds for moderate load.
- A Lambda invoke action must call PutJobSuccessResult or PutJobFailureResult; continuation tokens support long-running checks.
- Use the Step Functions invoke action for multi-step test workflows and the Amazon Inspector scan action for vulnerability gates on source or images.