如何在AWS EC2实例上运行Karate API自动化脚本?
Hey there! I’ve helped multiple teams get their Karate API automation suites up and running on AWS EC2 instances, so let’s break down exactly what you need to do step by step:
Step 1: Prepare Your AWS EC2 Instance
- Choose the right instance type: Start with a
t2.micro(it’s free tier eligible) for small test suites. If you have concurrent test runs or larger suites, scale up tot2.smallort3.mediumbased on your needs. - Pick an OS: Go with Ubuntu Server or Amazon Linux 2—both have straightforward package managers for installing dependencies.
- Configure security groups:
- Allow outbound traffic to all destinations (your scripts need to call APIs, after all).
- If you plan to access test reports directly from EC2, add an inbound rule for HTTP (port 8080) restricted to your IP address.
- Keep SSH access (port 22) restricted to your IP for security.
Step 2: Set Up Required Dependencies on EC2
Once you’ve SSH’d into your EC2 instance, install these tools:
- Java 8+ (Karate requires this):
For Ubuntu:
Verify with:sudo apt update && sudo apt install openjdk-11-jdk -yjava -version - Git (to pull your project repo):
sudo apt install git -y - Build tool (Maven or Gradle, depending on your project):
For Maven:
Verify with:sudo apt install maven -ymvn -v
Step 3: Deploy Your Karate Project to EC2
- Option 1: Clone from Git:
git clone <your-project-repo-url> cd <your-project-directory> - Option 2: Upload from local machine:
Usescpto transfer your project folder (replace placeholders with your details):scp -i <your-key-pair-file.pem> -r /path/to/local/karate-project ubuntu@<ec2-public-ip>:~/ cd ~/karate-project - Pre-build to resolve dependencies:
Run this to download all required Karate and project dependencies before running tests:mvn clean install -DskipTests
Step 4: Run Your Karate Automation Scripts
- Run specific tests/tags:
Use this command to execute tests tagged with@smoke(replace with your tag):
Or run a single feature file:mvn test -Dkarate.options="--tags @smoke"mvn test -Dkarate.options="classpath:com/your/company/api/tests/login.feature" - Run in the background (avoid SSH disconnect issues):
Usenohupto keep tests running even if you close your SSH session:
Check logs later with:nohup mvn test -Dkarate.options="--tags @full-suite" > karate-test-output.log 2>&1 &cat karate-test-output.log
Step 5: Access Test Reports
Karate generates detailed HTML reports in the target/surefire-reports folder. Here’s how to view them:
- Download to local machine:
Then open thescp -i <your-key-pair-file.pem> -r ubuntu@<ec2-public-ip>:~/karate-project/target/surefire-reports /path/to/local/folderindex.htmlfile in your browser. - Host reports on EC2 temporarily:
Navigate to the reports folder and start a simple HTTP server:
Now you can access the reports viacd target/surefire-reports python3 -m http.server 8080http://<ec2-public-ip>:8080in your browser (make sure port 8080 is open in your security group).
Optional Optimization Tips
- Schedule regular runs: Use
cronto automate test execution (e.g., daily at midnight). Runcrontab -eand add:0 0 * * * cd ~/karate-project && mvn test -Dkarate.options="--tags @daily-run" >> /var/log/karate-daily-tests.log 2>&1 - Persist reports to S3: Upload reports to an S3 bucket for long-term storage (install AWS CLI first):
aws s3 cp target/surefire-reports s3://your-s3-bucket/karate-reports/$(date +%Y%m%d) --recursive - Use Session Manager instead of SSH: For more secure access without opening port 22, use AWS Systems Manager Session Manager to connect to your EC2 instance.
内容的提问来源于stack exchange,提问作者Uday Kumar
相关产品推荐
相关产品推荐

