The majority of discussions around coding cloud infrastructure lead to cloud orchestration tools like Terraform, Cloudformation, or Google Cloud Deployment Manager. These infrastructure as code tools manage the complexity of launching resources. They only need a description of resources to launch. We provide these descriptions declaratively programming configuration files that these tools consume. But what is declarative programming?
Let's review a YAML configuration used by Cloudformation to launch an EC2 instance with a Security Group:
The configuration file does not have any control flow. There are no procedural steps in launching these resources. It’s only a description of the resources. The steps to launch the EC2 instance and the security group is managed by Cloudformation and hidden from us. This is declarative programming.
Declarative programming is if I were to go to a coffee shop and asked, “Could I please have a medium roast coffee, no room, to go please?” I describe the coffee I want and wait for it. After a short amount of time, and assuming I didn’t talk to an angry barista, I’d have my coffee in a to-go cup and I’d be on my way.
Contrast this to imperative programming, or what most programmers consider coding. The imperative programming example with coffee are the steps the barista follows to make my cup of coffee:
- Grind the beans
- Load the beans into the coffee maker
- Pour water into the coffee maker
- Press the button to brew the coffee
- Wait for the coffee to brew
- Fill a to-go coffee cup to the top with the brewed coffee
- Serve the coffee to the customer
The order of operations is important. If the steps were rearranged or skipped, I would not have my cup of coffee. Also, much to my dismay, this is only one way to make a cup of coffee. There are countless ways to make a cup of coffee.
The same can be said about provisioning and configuring cloud infrastructure for use with software. As a developer, I don't want to spend my time making cloud resources. I want to ask for them so I can sip my coffee.