Ship Infra Project

Ship Infrastructure - Final Thoughts, and Load Test

We have reached the end of this infrastructure journey. By now, you should have a solid understanding of how to provision and manage cloud infrastructure using Terraform. You've built a complete three-tier web application architecture on AWS, covering everything from networking and compute to security and data storage.

As promised, we have delivered the following outputs:

domain_for_your_apps = http://www.your-domain.com
database_endpoint = "postgres-db.cspuccciib8v.us-east-1.rds.amazonaws.com:5432"
ec2_first_instance_ip_address = "3.222.64.160"
ec2_second_instance_ip_address = "3.223.185.158"

Note, all of them are public, although your are in charge of protecting them. Use them at your own risk!

Load Testing

It would not be a complete infrastructure setup without some load testing. We have built this beautiful infrastructure that does pretty much nothing. But how much user load can it handle? Let's find out!

Using k6 for Load Testing

We are going to use k6 - a modern load testing tool that makes it easy to script and run load tests. First, install k6 by following the instructions on their installation page.

Writing a Load Test Script

We are going to create a simple load test script that hits our web application's domain. Create a file named load-test.js with the following content:

We are going to simulate 10000 virtual users (VUs) over a period of 20 seconds, sending requests to our web application's domain.

The following is the script content that you should put in load-test.js:

import http from "k6/http";
import { sleep } from "k6";
export const options = {
  vus: 10000,
  duration: "20s",
};
export default function () {
  http.get("http://www.your-domain.com");
  sleep(1);
}

Running the Load Test

You can run the load test using the following command:

k6 run load-test.js

The test will start, after it finishes, the json file will be created and you will see real-time statistics about the requests being made, response times, and any errors encountered.

Analyzing the Results

The JSON output file can be analyzed using various tools. One popular option is to use Grafana along with k6 Cloud. You can also use the built-in summary report that k6 provides at the end of the test run.

I have made a simple web app to render the results, and here is a sample screenshot of how the results might look:

alt text

You can see that our infrastructure did not handle the 15000 users very well! Outch! This is expected since we did not setup auto-scaling, or caching, and only had two EC2 instances running. In a production scenario, you would want to implement these features to handle high traffic loads effectively.

Conclusion and Next Steps

I hope you enjoyed this hands-on journey through building infrastructure with Terraform on AWS. The goal was to provide you with a solid foundation that you can build upon.

As you have seen from the load test, the infrastructure is far from production ready. Some of the next steps you might consider include:

  • Implementing auto-scaling for EC2 instances with ECS
  • Adding Kubernetes for the orchestration of containers and quick scaling from 2 instances to more
  • Adding caching layers using services like Redis cache,
  • Setting up monitoring and alerting using Grafana,
  • Setting up the Incident Response Plan with PagerDuty,

And many more. I encourage you to continue exploring and experimenting with Terraform and AWS. The possibilities are endless, and the skills you've gained here will serve you well in your infrastructure journey. Myself, I will continue expanding this guide with more advanced topics and real-world scenarios. Stay tuned for more!

On this page