# Managing CloudFormation Stacks at Scale with cfn-compose

## Overview

As infrastructure grows more complex, managing multiple CloudFormation Stacks becomes a challenge. Typically, actions such as creating, updating, or deleting stacks are performed on a single stack at a time. Inaddition deleting stacks in a development or testing environment can be cumbersome because we usually want to destroy the whole environment and to do that stacks must be deleted in the reverse order of creation.

`cfn-compose` offers a solution to this problem by providing a way to manage multiple, related stacks using a declarative `yaml` language, making the process easier and more streamlined. For more details please go through the rest of the Readme.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677805678366/8ed8429f-67b1-4ed8-9042-dd38b7a4d14b.png align="center")

# cfn-compose(cfnc in short)

A command-line tool for managing CloudFormation Stacks at scale.  
**Github Repo:** [https://github.com/rbalman/cfn-compose](https://github.com/rbalman/cfn-compose)

## Features

* Create/Update/Delete multiple CloudFormation stacks in parallel or sequentially
    
* Customize the CloudFormation stacks dependency using yaml config
    
* Delete multiple CloudFormation stacks respecting the creation sequence
    
* DryRun mode to plan the change
    
* Generate/Validate/visualize configuration with ease
    
* Supports Go Template for dynamic value substitution
    

## Limitations

* Supports limited CFN attributes
    
* No Retry Mechanism
    
* No Configurable concurrency. One Go routine is spun for every flow.
    
* One compose file can have maximum `50` flows and each flow can have up to `50 stacks`. This is by design, to limit stacks in a compose file.
    

## Installation

Binary is available for Linux, Windows and Mac OS (amd64 and arm64). Download the binary for your respective platform from the [releases page](https://github.com/rbalman/cfn-compose/releases).

#### Using go cli

```shell
go install github.com/rbalman/cfn-compose@latest
```

## Usage

```shell
➜ cfnc --help
Manage cloudformation stacks at scale. Design and deploy multiple cloudformation stacks either in sequence or in parallel using declarative configuration

Usage:
  cfnc [command]

Available Commands:
  completion  Generate the autocompletion script for the specified shell
  config      Generate, validate and visualize the compose configuration
  deploy      Deploys the stacks based on the sequence specified in the compose configuration
  destroy     Destroys all the stacks in the reverse order of creation
  help        Help about any command

Flags:
  -c, --config string     File path to compose file (default "cfn-compose.yml")
  -d, --dry-run           Run commands in dry run mode
  -h, --help              help for cfnc
  -l, --loglevel string   Specify Log Levels. Valid Levels are: DEBUG, INFO, WARN, ERROR (default "INFO")
  -v, --version           version for cfnc

Use "cfnc [command] --help" for more information about a command.
```

#### Examples

```shell
## Deploy
cfnc deploy
## Deploy in dry run mode
cfnc deploy -d

## Destroy
cfnc destroy
## Destroy in dry run mode
cfnc destroy -d

## Generate Validate and Visualize compose configuration
cfnc config generate
cfnc config validate
cfnc config visualize
```

## Man

| Command | Options | Description |
| --- | --- | --- |
| cfnc | \-h, --help, help | Get description of cfnc |
| cfnc | \-d, --dry-run | enable dry run mode |
| cfnc | \-l, --loglevel | Specify Log Levels. Valid Levels are: DEBUG, INFO, WARN, ERROR (default "INFO") |
| cfnc | \-c, --config | File path to compose file (default "cfn-compose.yml") |
| cfnc deploy | with no flag | deploys all the stacks |
| cfnc deploy | \-f, --flow | Cherry pick specific flow to deploy |
| cfnc destroy | with no flag | destroys all the stacks |
| cfnc destroy | \-f, --flow | Cherry pick specific flow to destroy |
| cfnc config generate | no flags | Generates compose template |
| cfnc config validate | no flags | Validates the compose configuration |
| cfnc config visualize | no flags | Visualize the stacks dependencies and creation order |
| cfnc | \-v, --version | version for cfnc |

## Documentation

**Sample Config File:**

```yaml
Description: Sample CloudFormation Compose file
Vars:
  Key1: Value1
  Key2: Value2
Flows:
  Flow1:
    Order: 0
    Description: Flow1 Description
    Stacks:
      - Stack1
      - Stack2
  Flow2:
    Order: 1
    Description: Flow2 description
    Stacks:
      - Stack1
      - Stack2
```

A typical compose configuration contains:

* Optional `Description`
    
* Optional `Vars` section to define variables in `Key: Value` mapping. Only static variables are supported at the moment. eg:
    

```yaml
Vars:
  ENV_TYPE: 'nonproduction'
  ENV_NAME: 'demo'
  AWS_PROFILE: 'demo'
```

* Mandatory `Flows:` section `Flow` is a collection of CloudFormation stacks that are deployed sequentially. `Flows` is the collection of flow which can be ordered using `Order` property. `Flows` can run in parallel or sequentially based on the Order property.
    
    * Optional `Order` can be any `unsigned` integer. Default `Order` is set to `0`. Flow with the lowest orders are deployed first.
        
    * Optional `Description`
        
    * Mandatory `Stacks` which is the collection of CFN stack. Below are the supported attributes of the stack object
        
        * mandatory `template_file` or `template_url` (only s3 url)
            
        * mandatory `stack_name`
            
        * optional `capabilities`
            
        * optional `parameters`
            
        * optional `tags`
            
        * optional `tags`
            

**Sample:**

```yaml
Description: Sample CloudFormation Compose file
Vars:
  ENV_NAME: cfnc
  ENV_TYPE: nonproduction
Flows:
  SecurityGroup:
    Order: 0
    Description: Creates SecurityGroup
    Stacks:
      - template_file: <cfn-template-path>
        stack_name: stack-name1
        parameters:
          EnvironmentName: '{{ .ENV_NAME }}'
          EnvironmentType: '{{ .ENV_TYPE }}'
        tags:
          EnvironmentName: '{{ .ENV_NAME }}'
          EnvironmentType: '{{ .ENV_TYPE }}'

  EC2Instance:
    Order: 1
    Description: Deploying EC2 Instance
    Stacks:
      - template_file: <cfn-template-path>
        stack_name: stack-name2
        parameters:
          EnvironmentName: '{{ .ENV_NAME }}'
          EnvironmentType: '{{ .ENV_TYPE }}'
        tags:
          EnvironmentName: '{{ .ENV_NAME }}'
          EnvironmentType: '{{ .ENV_TYPE }}'
```

Please consult examples for quick start [ec2-sg example](examples/ec2-sqs/Readme.md) and [demo ec2-sqs-rds example](examples/demo/Readme.md)

## Contributors

![](https://contrib.rocks/image?repo=rbalman/cfn-compose align="left")

There exists ample opportunity for enhancement and you are welcome to make a valuable contribution. If you have any concerns, recommendations, ideas feel free to [create issues](https://github.com/rbalman/cfn-compose/issues) or [create PR](https://github.com/rbalman/cfn-compose/pulls). [Details Example](https://github.com/rbalman/cfn-compose/tree/main/examples/demo)
