How to Get TeamCity to Stop & Restart Amazon EC2 Instances for Build Agents

How to Get TeamCity to Stop & Restart Amazon EC2 Instances for Build Agents
Amazon EC2 instances are a popular choice for build agents in TeamCity, JetBrains' powerful Continuous Integration (CI) and Continuous Delivery (CD) server. However, keeping EC2 instances running constantly can be costly. This blog post will clarify if it’s possible to get TeamCity to stop and restart Amazon EC2 instances for build agents, and if so, how to do it.
What is TeamCity?
TeamCity is a user-friendly CI/CD server that supports a wide range of developer tools and frameworks. Its cloud integration features allow it to use Amazon EC2 instances as build agents, providing the computing power needed for build tasks without requiring dedicated hardware.
Why Stop & Restart EC2 Instances?
Amazon charges for EC2 instances by the hour, so keeping an instance running when not in use is a waste of resources and money. By stopping and restarting instances as needed, you can significantly reduce costs and better utilize resources.
Can TeamCity Control EC2 Instances?
Yes, TeamCity can control EC2 instances. TeamCity’s cloud integration allows it to start, stop, and manage EC2 instances automatically. This is achieved with the help of cloud profiles and cloud agents in TeamCity.
How to Set Up TeamCity to Control EC2 Instances
Step 1: Create a Cloud Profile
In TeamCity, navigate to project settings and choose ‘Cloud Profiles’. Click ‘Create new profile’, select ‘Amazon EC2’, and fill in the necessary details including your AWS access key, secret key, and desired EC2 settings.
Step 2: Configure Build Agents
In the same cloud profile, specify the AMI that your build agent is based on, the instance type, and the maximum number of instances. You can also set idle time settings to control when instances should be stopped after becoming idle.
Step3: Cloud Agent Configuration
TeamCity will create a new cloud agent for each EC2 instance it starts. These agents are temporary and will disappear when the instance is stopped. They inherit their settings from the cloud profile, but you can also customize them per instance.
Step4: Testing
After setting up, run a build that requires more agents than are currently running. TeamCity should start additional EC2 instances as needed. When those agents become idle, TeamCity should stop them according to the idle time settings in your cloud profile.
Conclusion
Using TeamCity’s cloud integration, you can efficiently control the lifecycle of your Amazon EC2 instances and significantly optimize costs and resource usage. While this task may seem complex at first, with a bit of configuration, you can automate your build process to be both cost-effective and resource-efficient.
I hope this guide has been helpful in understanding how to get TeamCity to stop and restart Amazon EC2 instances for build agents. If you have any further questions or need more specific guidance, please feel free to ask.
About Saturn Cloud
Saturn Cloud is your all-in-one solution for data science & ML development, deployment, and data pipelines in the cloud. Spin up a notebook with 4TB of RAM, add a GPU, connect to a distributed cluster of workers, and more. Join today and get 150 hours of free compute per month.