# Lambda Load Testing Tool

A load testing solution for AWS Lambda using k6, deployable to AWS EC2 via Bitbucket Pipelines.

## Features

- Ramp up testing from 0 to 1000 concurrent users
- Random payload generation
- REST API to trigger tests remotely
- Automated deployment via Bitbucket Pipelines
- Docker containerization
- Real-time metrics and reporting

## Prerequisites

- Node.js 18+
- k6
- AWS Account with:
  - EC2 Instance
  - S3 Bucket
  - CodeDeploy Application
  - IAM roles configured for CodeDeploy
- Bitbucket Repository

## Environment Variables

Set these in your Bitbucket repository settings:

```
AWS_ACCESS_KEY_ID=your_access_key
AWS_SECRET_ACCESS_KEY=your_secret_key
AWS_DEFAULT_REGION=your_region
AWS_S3_BUCKET=your_bucket_name
LAMBDA_URL=your_lambda_endpoint
```

## AWS Setup

1. Create an EC2 instance with the CodeDeploy agent installed
2. Create an S3 bucket for deployment artifacts
3. Create a CodeDeploy application and deployment group
4. Configure IAM roles:
   - EC2 instance role with CodeDeploy agent permissions
   - CodeDeploy service role
   - IAM user/role for Bitbucket with S3 and CodeDeploy permissions

## Local Development

1. Install dependencies:
```bash
npm install
```

2. Run the server:
```bash
npm start
```

3. Run a test locally:
```bash
k6 run src/k6-script.js -e LAMBDA_URL=your_lambda_url
```

## API Endpoints

### Health Check
```
GET /health
```

### Trigger Load Test
```
POST /trigger-test
Content-Type: application/json

{
  "lambdaUrl": "https://your-lambda-url.amazonaws.com"
}
```

## Deployment

The application is automatically deployed to EC2 when changes are pushed to the main branch. The deployment process:

1. Builds and tests the application
2. Creates a deployment bundle
3. Uploads the bundle to S3
4. Triggers CodeDeploy deployment
5. CodeDeploy executes the deployment lifecycle:
   - Stops any running application
   - Installs dependencies
   - Configures the environment
   - Starts the application
   - Validates the deployment

## Load Test Configuration

The k6 script (`src/k6-script.js`) is configured with the following stages:

1. Ramp up to 100 users over 2 minutes
2. Ramp up to 500 users over 3 minutes
3. Ramp up to 1000 users over 5 minutes
4. Stay at 1000 users for 3 minutes
5. Ramp down to 0 users over 2 minutes

## Security

- API endpoints should be protected in production
- AWS credentials are managed through Bitbucket variables
- EC2 instance should be properly secured with security groups
- Use IAM roles with minimal required permissions


# install packages
sudo apt-get update
sudo npm install -g k6
sudo apt-get install -y jq
sudo apt-get install -y awscli

# Get files from s3
aws s3 cp s3://k6testharnessrepo/test_harness.zip /tmp/test_harness.zip

# Extract the Zip File
mkdir /tmp/test_harness
unzip /tmp/test_harness.zip -d /tmp/test_harness

# Copy to Target Directory
rsync -av /tmp/test_harness/ /home/ssm-user/test_harness/

# Set Permissions
sudo chown -R ssm-user:ssm-user /home/ssm-user/test_harness/
sudo chmod -R u+X,g+X,o+X /home/ssm-user/test_harness/src/
chmod u+x /home/ssm-user/test_harness/src/newversion/run_k6_wrapper.sh
chmod u+x /home/ssm-user/test_harness/src/newversion/start_k6_processes.sh


# Clean Up
rm -rf /tmp/test_harness
rm /tmp/test_harness.zip

#  Run the Test Harness
cd /home/ssm-user/test_harness/src/newversion
./start_k6_processes.sh 3 121 400 "1,," # NUM_PROCESSES ,BATCH_SIZE ,WAIT_TIME_MS, duration_string( days, hours, minutes)