Components

A component is an isolated piece of infrastructure which allows developers to define and reuse infrastructure in a standardized way.

General examples could be a relational database, Kubernetes infrastructure with load balancers, cluster nodes, etc. A more specific example would be a combination of serverless resources (Lambda, SNS, SQS) that perform certain tasks, such as email processing.

Once a component is created, it can be reused in a limitless number of projects and environments. As with Docker containers, you can package and combine infrastructure components in any number of ways to build and deploy complex solutions.

You define the infrastructure using Infrastructure as Code (using CloudFormation) and your choice of Git repository as the “single source of truth.” This means that you can continue using your Git flow with pull requests and reviews. The use of IaC and Git is intended to declare the infrastructure in source code, allowing for more automation and reducing risks from manual intervention.

Each component consists of a:

  • Infrastructure Template – Infrastructure as a Code (IaC) template
  • Pipeline Definition – A declarative pipeline specification that defines how the component will be packaged, tested and deployed
  • Component Schema (component policy) – a declarative specification of what the component should expect as an input/output. In the schema you can also define the validation rules for each parameter that will be enforced by Microtica before deployment. Examples may include VM instance type enums, cluster scaling limits, DB user/pass etc. The Component Schema uses the JSON Schema industry standard.

The Microtica Infrastructure Template, written using AWS CloudFormation syntax, provides many benefits including:

  • Complete overview of the infrastructure deployments history
  • Deep insights for each component with ability to track deployed version back to the exact Git commit
  • Automated rollback of the whole infrastructure
  • Integrated with the powerful Microtica Pipelines
  • Facilitates promotion across environments (dev -> test -> production)
  • Self-contained pipeline definition, validation schema and infrastructure templates are packaged together, which makes the distribution of the infrastructure much easier

For Component Schemas, the best practice is to specify validation rules as strictly as possible. In this way you can avoid misuse or wrong configuration for certain infrastructure components.

The schema specification can enforce specific policies within the organization, for example the min/max number of scaled instances in the Kubernetes cluster, allowed EC2 instance classes, etc.

#Define your custom infrastructure

The way to define an infrastructure in components is by using CloudFormation templates.

Component is not yet another wrapper of CloudFormation, so you don’t have to learn new syntax in order to provision cloud infrastructure. Anything that CloudFormation supports is immediately available in components as well.

Below is an example of a template that defines one EC2 instance with instance type provided as an input parameter. The template should be stored somewhere in your Git repository.

cfn-template.json

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "My Custom Component",
    "Parameters": {
        "natInstanceType": {
            "Type": "String",
            "Description": "Type of the EC2 instance"
        }
    },
    "Resources": {
        "NATInstance": {
            "Type": "AWS::EC2::Instance",
            "Properties": {
                "InstanceType": {
                    "Ref": "natInstanceType"
                }
            }
        }
    },
    "Outputs": {
        "accessDomain": {
            "Value": {
                "Fn::GetAtt": "NATInstance.PublicDnsName"
            },
            "Description": "Public domain to access the EC2 instance"
        }
    }
}

In the next section you will se how you can use this template and prepare it for deployment.

#Prepare the Component for deployment

To make component ready for deployment we need to setup a proper automation pipeline.

Microtica provides a built-in pipeline step that will package test and prepare artifacts for deployment. You just have to specify the path to your CloudFormation template and everything else will be handled by Microtica.

You can define a pipeline either directly in Microtica Portal or add microtica.yaml file in your source code root path.

Create Pipeline form

Below is an example of a pipeline that will prepare the component for deployment.

microtica.yaml

steps:
  Clone:
    type: git-clone
    title: Clone Git repository

  ComponentPackage:
    type: cfn-component
    title: Package my component
    # path to CloudFormation template relative to the source code root dir
    cfn_template: path/to/cfn-template.json

The pipeline consists of two steps. The first step clones the code from your Git repository, specified while creating the pipeline. The second step is a built-in component step that packages the component. You just have to specify the path to the CloudFormation template cfn_template and Microtica will do the rest.

#Make the Component configurable

Now that we’ve defined the pipeline, we are ready to use the component to compose our infrastructure solution.

To make components easy to reuse we enable you to define their custom schema for what component should receive as an input and give as an output. The schema is then validated each time you configure the component or trigger a deployment.

To define the component schema we use already established standard known as JSON Schema. Microtica will automatically generate this schema by parsing your CloudFormation input/output parameters. If you need a custom schema then just create a schema.json file in the source code root path.

Below is one example of a custom schema that defines one required input parameter named as natInstanceType of type enum and one output parameter named as accessDomain.

schema.json

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "schema://microtica/component-schema.json",
    "title": "Component Schema",
    "type": "object",
    "properties": {
        "inputs": {
            "type": "object",
            "properties": {
                "natInstanceType": {
                    "type": "string",
                    "enum": [
                        "t2.micro",
                        "t2.medium",
                        "t2.large"
                    ]
                }
            },
            "required": [
                "natInstanceType"
            ],
            "additionalProperties": false
        },
        "parameters": {
            "type": "object",
            "properties": {
                "natInstanceType": {
                    "type": "string"
                }
            }
        },
        "outputs": {
            "type": "object",
            "properties": {
                "accessDomain": {
                    "type": "string"
                }
            }
        }
    },
    "additionalProperties": false
}

The schema is rendered in Microtica GUI as defined in the schema.json, so when you want to use and configure this component you can choose from predefined enums (t2.micro, t2.medium, t2.large), for the natInstanceType input property, from a dropdown list.

Component configuration modal

For Microtica to use the custom schema we need to extend the pipeline by adding additional step parameter schema and specify the path to the schema file.

microtica.yaml

steps:
  Clone:
    type: git-clone
    title: Clone Git repository

  ComponentPackage:
    type: cfn-component
    title: Package my component
    # path to CloudFormation template relative to the source code root dir
    cfn_template: path/to/cfn-template.json
    # path to JSON schema relative to the source code root dir (optional)
    schema: path/to/schema.json

#Deploy the Component

To deploy the component you need to first create a new environment or use an existing one. Then just add the component in the environment and hit on Create Plan button in the top right to start the deployment process.

If you instead want a step by step guide to create a new environment and use the component we’ve just created, please follow our infrastructure getting started guide.