You finally have your build agents humming along in TeamCity, but someone insists each job needs its own Amazon EC2 instance. You sigh, knowing what’s coming: credentials scattered across config files, ad-hoc SSH keys, and the creeping dread of manual security reviews. It doesn’t have to be this way.
TeamCity is excellent at orchestrating builds and managing pipelines. EC2 shines at elastic compute, creating short-lived machines with just the right specs for the job. Together, they can deliver dynamic, isolated build environments that scale on demand. The trick is wiring identity and automation so you get both speed and control.
The key is letting TeamCity trigger, configure, and tear down EC2 instances automatically while AWS handles identity. Use IAM roles to attach permissions directly to your build agents, not to stored keys. When TeamCity starts a new build agent, it spins up an EC2 instance using those roles for access, then destroys it when finished. No keys, no leftovers. The build metadata stays inside TeamCity, and the infrastructure lifecycle belongs to AWS.
If you get weird timeouts or missing instance tags, check your agent templates first. Make sure the AWS autoscaling group uses on-demand or spot capacity correctly. Rotate credentials by assigning temporary STS tokens, not long-lived keys. Map TeamCity’s project permissions to the IAM roles linked with each build agent so one project can't accidentally deploy from another.
Quick answer: How do I connect EC2 Instances and TeamCity securely?
Create an IAM role with launch and terminate permissions, attach it to a TeamCity agent template, and use AWS API calls through the TeamCity cloud profile settings. The agent then authenticates automatically without using stored credentials, giving you secure, repeatable access at scale.