Folding as a Service with AWS StepFunctions
Tags: folding, sam, aws, python
Published: 2023-01-23 11:58:11

I am an AWS Solution Architect and I had been working with cloud technologies for many years. I had always been interested in using my technical skills to contribute to something meaningful. In 2020, when the COVID-19 pandemic was in full swing, I came across an article about the **[Folding@home](https://foldingathome.org/?lng=en)** project, which uses spare processing power of personal computers to perform complex simulations of protein folding. I realized that by using my own home computer, I could contribute to the project and help researchers understand how proteins fold and misfold, which in turn can aid in the development of new drugs and treatments for COVID-19 and other diseases.

One day, while I was thinking about how to increase my computer's processing power and efficiency, I had an idea: why not use the cloud? I knew that with the vast resources and flexibility of a cloud computing platform like **Amazon Web Services** (AWS), I could run simulations on a much larger scale and speed up the discovery process. So, I set up a virtual machine on AWS and started running the _Folding@home_ simulations on it. The results were astounding, my computer's processing power increased, and I was able to perform more complex simulations and make new discoveries at a much faster rate. However, I soon realized that running _Folding@home_ on the cloud was too expensive for my personal budget.

I couldn't let go of the idea. I thought that there might be other people or companies who would like to contribute with some compute resources but don't know how to use the folding. So, I came up with the idea of providing a **"Folding as a Service"** solution where contributors just specify the amount of money they want to donate and based on that, the infrastructure will be automatically generated and will run the foldings until the credit runs out. This way, I can help more people and organizations contribute to the _Folding@home_ project and potentially researchers find cures for diseases more quickly.

Still in 2020, I took action and put the idea of **"Folding as a Service"** into practice. I built the service using `Step Functions`, `Lambda`, `DynamoDB`, `EC2` and more. The Step Function was designed to take an input of credits in dollars and based on that, it would automatically start and configure a single EC2 instance. Several Lambda tasks were used to start and stop the _Folding@home_ instance, monitor the performance of the instance, and make sure it was running efficiently. DynamoDB was used to keep track of the credit consumption.

I had the idea to put this whole service behind a website and create an easy-to-use user interface which would allow anyone to access the service and contribute to the _Folding@home_ project with a few simple clicks, regardless of their technical expertise. I finished the backend service but because of lack of time did not start to work with the user interface. The solution stood unused until recently when I started to do some open-source contributions. 

The other trigger which took me back to my dusty idea happened during the re:Invent conference, where I learned about a new service called AWS **[Application Composer](https://aws.amazon.com/application-composer/)**. I wanted to test this service and explore its capabilities, and I decided to use my old _Folding as a Service_ solution as a demo. I decided to recreate its infrastructure using the AWS Application Composer. Using the Application Composer wasn't so trivial but still it provided me with a good head start, and I was able to use the basic **[SAM](https://aws.amazon.com/serverless/sam/)** template generated by it. I found that the Application Composer can be a great tool for serverless application designing, planning, and implementation, but as soon you need to include non-serverless resources, like in my case the tool can't be used any more. Still let me show you the initial diagram which I created:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mgsxhqjxaverf1ltupwh.png)

The **SAM** template is a special **CloudFormation** template. CloudFormation is not my favorite tool, but still I decided to stick with it for this particular case. The reason for that was that I wanted to have a project using SAM. 

Now these tools allowed me to create the infrastructure as code, update the scripts, and develop a working Folding as a Service solution that runs on EC2 spot instances, and can fold both with CPU or GPU setups. The solution is open-source, and anyone can clone my repository (https://github.com/sazecis/folding-as-a-service), build it, and deploy it to their own AWS account. If anyone is interested in more details on how to deploy the solution, they should read the README file of the repository or check out the diagram below:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/adeo6jo4rlirduc9mpyk.png)

After deployment, the folding process will not start automatically, it needs to be triggered by executing the Step Function and specifying the credit which you want to spend. This solution is ideal for individuals and organizations who have unused AWS credits or wish to contribute to the _Folding@home_ project. The process of setting it up is straightforward, making it easy to contribute to the fight against diseases.

There are still many potentials in this solution, and it can be implemented for other Cloud Providers as well. Implementing a user-friendly interface would bring in more users and be a significant breakthrough. However, I am taking it step by step. **_And of course, if anyone is interested in joining the project, please feel free to contact me. Join the project and let's make a difference together._**