Skip to content

Lambda Handler (API)

The Lambda handler controls the address book run: reads configuration, authenticates to GitHub, gathers user details, and writes three JSON lookup files to S3.

Overview

  • Reads env vars: GITHUB_ORG, AWS_SECRET_NAME, GITHUB_APP_CLIENT_ID, S3_BUCKET_NAME.
  • Creates Boto3 clients for Secrets Manager and S3.
  • Uses GitHubServices.get_all_user_details() to retrieve:
  • username → verified org emails
  • email → username
  • username → GitHub account ID
  • Writes JSON outputs under AddressBook/ prefix to the configured S3 bucket.
  • Logs progress and errors via wrapped_logging.

Local Run (development)

export GITHUB_ORG=<org>
export AWS_SECRET_NAME=<secret_name>
export GITHUB_APP_CLIENT_ID=<client_id>
export S3_BUCKET_NAME=<bucket_name>
export AWS_ACCESS_KEY_ID=<access_key>
export AWS_SECRET_ACCESS_KEY=<secret_key>

poetry run python3 src/lambda_function.py

Responses

  • 200: success with user_entries count
  • 404: organisation not found
  • 500: missing configuration or S3 write failure

Reference

AWS Lambda handler for the KEH Test Data Generator

lambda_handler(event, context)

AWS Lambda handler function for generating synthetic test data.

Parameters:

Name Type Description Default
event

Input event data (dict)

required
context

Lambda context object

required

Raises:

Type Description
Exception

If secret manager or s3client are None

Exception

If the environmental variables are not found

Exception

If there was a failure writing to S3

Returns:

Name Type Description
dict

Response with statusCode and generated data

Source code in src/lambda_function.py
def lambda_handler(event, context):
    """
    AWS Lambda handler function for generating synthetic test data.

    Args:
        event: Input event data (dict)
        context: Lambda context object

    Raises:
        Exception: If secret manager or s3client are None
        Exception: If the environmental variables are not found
        Exception: If there was a failure writing to S3

    Returns:
        dict: Response with statusCode and generated data
    """

    org = os.getenv("GITHUB_ORG")
    secret_name = os.getenv("AWS_SECRET_NAME")
    app_client_id = os.getenv("GITHUB_APP_CLIENT_ID")
    bucket_name = os.getenv("S3_BUCKET_NAME")

    logger = wrapped_logging(False)

    try:
        secret_manager = boto3.client("secretsmanager")
        s3_client = boto3.client("s3")
    except Exception:
        secret_manager = None
        s3_client = None

    if secret_manager is None or s3_client is None:
        message = f"Unable to retrieve Secret Manager ({'empty' if secret_manager is None else 'Not empty'}) or S3Client({'empty' if secret_manager is None else 'Not empty'})"
        logger.log_error(message)
        raise Exception(message)

    github_services = GitHubServices(
        org, logger, secret_manager, secret_name, app_client_id
    )
    s3writer = S3Writer(logger, s3_client, bucket_name)

    # Fetch data from GitHub
    try:
        response = github_services.get_all_user_details()

        if response[0] == "NotFound":
            return {
                "statusCode": 404,
                "body": json.dumps(
                    {
                        "message": "Organisation not found",
                        "error": str(response[1]),
                    }
                ),
            }
        else:
            user_to_email, email_to_user, user_to_id = response

    except Exception as e:
        raise Exception(
            f"Failed to fetch data from GitHub: {str(e)}. Are the environment variables set correctly?"
        )

    # Serialize and write to S3
    try:
        username_key = "addressBookUsernameKey.json"
        email_key = "addressBookEmailKey.json"
        id_key = "addressBookIDKey.json"
        folder = "AddressBook/"

        s3writer.write_data_to_s3(
            folder + username_key, json.dumps(user_to_email, indent=2)
        )
        s3writer.write_data_to_s3(
            folder + email_key, json.dumps(email_to_user, indent=2)
        )
        s3writer.write_data_to_s3(folder + id_key, json.dumps(user_to_id, indent=2))
    except Exception as e:
        raise Exception(f"Failed to write data to S3: {str(e)}")

    return {
        "statusCode": 200,
        "body": json.dumps(
            {
                "message": "Successfully generated and stored address book data",
                "user_entries": len(user_to_email),
            }
        ),
    }