Ask for the Coffee

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:

Resources:
  EC2Instance:
    Properties:
      ImageId: ami-00a208c7cdba991ea
      InstanceType: t2.small
      KeyName: ilikecoffeeinamug
      SecurityGroups:
      - Ref: InstanceSecurityGroup
    Type: AWS::EC2::Instance
  InstanceSecurityGroup:
    Properties:
      GroupDescription: Enable SSH access via port 22
      SecurityGroupIngress:
      - CidrIp: 127.0.0.1/32
        FromPort: 22
        IpProtocol: tcp
        ToPort: 22
    Type: AWS::EC2::SecurityGroup
Cloudformation Template for an EC2 Instance and 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:

  1. Grind the beans
  2. Load the beans into the coffee maker
  3. Pour water into the coffee maker
  4. Press the button to brew the coffee
  5. Wait for the coffee to brew
  6. Fill a to-go coffee cup to the top with the brewed coffee
  7. 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.