-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add SSM parameters for the load tests (#872)
Add AWS SSM parameters needed by the load tests to run. This includes changes to the Lambda so that it can read these parameters and load them into the execution environment. The reason these are not being passed in directly as Lambda environment variables is because these are stored as plain text and can be leaked by AWS CLI commands.
- Loading branch information
Showing
14 changed files
with
175 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
resource "aws_ssm_parameter" "load_testing_form_id" { | ||
# checkov:skip=CKV_AWS_337: default service encryption key is acceptable | ||
name = "/load-testing/form-id" | ||
description = "Form ID that will be used to generate, retrieve and confirm responses." | ||
type = "SecureString" | ||
value = var.load_testing_form_id | ||
} | ||
|
||
resource "aws_ssm_parameter" "load_testing_form_private_key" { | ||
# checkov:skip=CKV_AWS_337: default service encryption key is acceptable | ||
name = "/load-testing/form-private-key" | ||
description = "Private key JSON of the form that will be used to authenticate the API requests. This must be a key for the `/load-testing/form-id` form." | ||
type = "SecureString" | ||
value = var.load_testing_form_private_key | ||
} | ||
|
||
resource "aws_ssm_parameter" "load_testing_zitadel_app_private_key" { | ||
# checkov:skip=CKV_AWS_337: default service encryption key is acceptable | ||
name = "/load-testing/zitadel-app-private-key" | ||
description = "Private key JSON of the Zitadel application to perform access token introspection requests." | ||
type = "SecureString" | ||
value = var.load_testing_zitadel_app_private_key | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,9 @@ | ||
FROM amazon/aws-lambda-python:3.11@sha256:99cadc3bd9674a32a4ef694ff2e27f0b3d6c7f369b174db792b0099699fa0da4 | ||
COPY main.py . | ||
COPY tests ./tests | ||
COPY requirements.txt . | ||
|
||
RUN yum -y groupinstall "Development Tools" | ||
|
||
RUN pip3 install --upgrade pip | ||
FROM amazon/aws-lambda-python:3.12@sha256:37b95206c4c78331f6d5cb0e8389ef573f39cfea01f73c530f28f3ac6f6493c7 | ||
|
||
COPY requirements.txt . | ||
RUN pip3 install -r requirements.txt | ||
|
||
COPY main.py . | ||
COPY tests ./tests | ||
|
||
CMD ["main.handler"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Load testing | ||
Locust load tests that can be run in a Lambda function or locally. | ||
|
||
## Lambda | ||
Invoke the function using an event that looks like so: | ||
```json | ||
{ | ||
"locustfile": "./tests/locust_test_file.py", | ||
"host": "https://forms-staging.cdssandbox.xyz", | ||
"num_users": "5", | ||
"spawn_rate": "1", | ||
"run_time": "5m" | ||
} | ||
``` | ||
|
||
## Locally | ||
You will need AWS access credentials for the target environment, along with the following environment variables set: | ||
```sh | ||
FORM_ID # Form ID to use for load testing | ||
FORM_PRIVATE_KEY # JSON private key for the form (must be from the `FORM_ID` form) | ||
ZITADEL_APP_PRIVATE_KEY # JSON private key for the Zitadel application that is used for access token introspection | ||
``` | ||
Once the variables are set, you can start the tests like so: | ||
```sh | ||
make install | ||
make locust | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,52 @@ | ||
import invokust | ||
import logging | ||
import os | ||
import boto3 | ||
from invokust.aws_lambda import get_lambda_runtime_info | ||
from invokust import LocustLoadTest, create_settings | ||
|
||
logging.basicConfig(level=logging.INFO) | ||
|
||
ssm_client = boto3.client("ssm") | ||
|
||
|
||
def get_ssm_parameter(client, parameter_name): | ||
response = client.get_parameter(Name=parameter_name, WithDecryption=True) | ||
return response["Parameter"]["Value"] | ||
def get_ssm_parameters(client, parameter_names): | ||
response = client.get_parameters(Names=parameter_names, WithDecryption=True) | ||
return {param["Name"]: param["Value"] for param in response["Parameters"]} | ||
|
||
|
||
# Load required environment variables from AWS SSM | ||
os.environ["FORM_ID"] = get_ssm_parameter(ssm_client, "load-testing/form-id") | ||
os.environ["PRIVATE_API_KEY_APP_JSON"] = get_ssm_parameter( | ||
ssm_client, "load-testing/private-api-key-app" | ||
) | ||
os.environ["PRIVATE_API_KEY_USER_JSON"] = get_ssm_parameter( | ||
ssm_client, "load-testing/private-api-key-user" | ||
params = get_ssm_parameters( | ||
ssm_client, | ||
[ | ||
"/load-testing/form-id", | ||
"/load-testing/form-private-key", | ||
"/load-testing/zitadel-app-private-key", | ||
], | ||
) | ||
|
||
os.environ["FORM_ID"] = params["/load-testing/form-id"] | ||
os.environ["FORM_PRIVATE_KEY"] = params["/load-testing/form-private-key"] | ||
os.environ["ZITADEL_APP_PRIVATE_KEY"] = params["/load-testing/zitadel-app-private-key"] | ||
|
||
def handler(event=None, context=None): | ||
|
||
# Check for required environment variables | ||
required_env_vars = [ | ||
"FORM_ID", | ||
"PRIVATE_API_KEY_APP_JSON", | ||
"PRIVATE_API_KEY_USER_JSON", | ||
"FORM_PRIVATE_KEY", | ||
"ZITADEL_APP_PRIVATE_KEY", | ||
] | ||
for env_var in required_env_vars: | ||
if env_var not in os.environ: | ||
raise ValueError(f"Missing required environment variable: {env_var}") | ||
|
||
try: | ||
settings = ( | ||
create_settings(**event) | ||
invokust.create_settings(**event) | ||
if event | ||
else create_settings(from_environment=True) | ||
else invokust.create_settings(from_environment=True) | ||
) | ||
loadtest = LocustLoadTest(settings) | ||
loadtest = invokust.LocustLoadTest(settings) | ||
loadtest.run() | ||
except Exception as e: | ||
logging.error("Exception running locust tests {0}".format(repr(e))) | ||
else: | ||
locust_stats = loadtest.stats() | ||
locust_stats.update(get_lambda_runtime_info(context)) | ||
return locust_stats | ||
return loadtest.stats() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters