I personally think the decision to store infrastructure templates within the code base is somewhat specific to the type of infrastructure and, indeed, type of code you intend to be running.
In the case of, say, Serverless, where you’re defining some API Gateway config, Lambda functions, maybe an S3 bucket or a DynamoDB table, it simply makes sense for the infrastructure and application code to reside in the same repository. If anything, Serverless (and certain other frameworks, I’m sure) reinforce the idea of storing your application code alongside your infrastructure code.
So why is that okay? I guess because in a Serverless project, application code and infrastructure code are tightly coupled; in a sense, you can’t really have one without the other - both tend to be developed somewhat simultaneously. One might think of these “serverless” resources as being modules or libraries required by your code in order for it to run, in which case it makes sense to have them within the same codebase because direct modification of the infrastructure resources themselves can directly lead to an impact on the ability for the application code to run successfully. Most importantly, because application and infrastructure are so tightly coupled by design, both elements may often be modified within the same release or even the same pull request. Simply from the point of keeping some form of version sanity, it makes sense to couple your infrastructure and application code in this scenario.
On the other hand, if we’re talking about an infrastructure wherein your application code is running on a more persistent “hosting” solution (docker, ec2 instance, bare metal, smart fridge), the fact that the infrastructure is persistent immediately hints towards it likely requiring far fewer changes relative to the application it is hosting. Does it make sense in this situation to keep infrequently-modified infrastructure code within the same repository as constantly-updating application code? I’d say no. Why? Well, for starters your build process immediately needs to account for the fact that the infrastructure hasn’t changed, unless you want to wait on your framework of choice to check the existing infrastructure state and report back that there are no changes to make because it doesn’t care whether or not the actual template files have changed. For every build. So you need some logic to diff the latest commit’s infrastructure-specific files against the previous commit and conditionally run your infrastructure deployment if it has changed. This already sounds exhausting, and I just said “for starters”. If you’re still not convinced, you now have to dance the delicate dance of ensuring everything is built in the right order such that your Docker image exists by the time it is requested by a container host or whatever you’re doing. Decoupling infrastructure and application code means you can just build the new container/ami/smart fridge rom whenever a change is merged into the main branch.
Personally I handle this by pushing new images with multiple tags - latest, and the commit id. Before I push the new image, I re-tag the existing latest image, removing the latest tag from it. Your infrastructure always grabs from latest; it no longer has to care about your application code build pipeline at all.
So to actually stop ranting and answer you - all the issues you’re listing are exactly why I don’t recommend storing infrastructure and application code together, but this is obviously very contextual, and I gave an example earlier of when you might want to couple infrastructure and application code tightly.
-
Personally I keep a separate “build” account; it contains all build-related resources and has federated IAM access to assume build-specific roles within my prod/staging/etc. accounts.
-
If you decouple the infrastructure code from the application code, you don’t need to worry about timing them. With regards to an AMI that depends on a docker image being built, I’d trigger the ami baking process from an ECR:PutImage event. This method is also a form of decoupling - you shouldn’t have to worry about timing things; everything should be event-based wherever possible.
The infrastructure build is triggered by a commit to the main infrastructure branch. The application code image is triggered by a commit to the main code branch. The AMI baking process is triggered by the application code image being pushed to ECR. Rotating the new AMI into your ASG is triggered by the completion of your AMI bake, and so on.
Right now you’re trying to play chess, managing a whole bunch of different pieces that all behave in different ways and need to be closely considered and orchestrated in order to effectively work together. This is no fun. You need to play Dominoes.