Start creating infrastructure on AWS like a PRO
Published: 2020-04-13 13:35:10

Defining and setting up a cloud infrastructure is usually a challenging and time-consuming task, especially when you have to work out all the infrastructural elements, where to store them so they are easy to use, organize build and deployment processes and so on.

[Microtica] (www.microtica.com?utm_source=devto&utm_medium=referral_link&utm_campaign=infrastructure_on_aws) abstracts the complex cloud setup and automation on AWS, using best practices. This means that you define the infrastructure you need without having to worry about the details of how this infrastructure will be deployed into the cloud. 

[Microtica] (www.microtica.com?utm_source=devto&utm_medium=referral_link&utm_campaign=infrastructure_on_aws) is structured into projects, components, and stages. Creating a component is the first step toward creating an infrastructure to run on the cloud.

*Components provide the ability to define a custom Cloud infrastructure setup (clusters, DBs, networks, etc.) that fits your own needs. Components are isolated by their nature which allows them to be independent, reusable and easy to test. It’s kinda like a microservice for infrastructure.*

To kick start infrastructure setup on AWS, Microtica offers ready-to-use components (VPC, MySQL, EKS, DynamoDB, Redis, S3) which can be found directly in the [portal](https://portal.microtica.com/login) and available on [GitHub](https://github.com/microtica-components). You can also create your own custom components to cover a part of the cloud infrastructure that you might need for a project. In this article, we will cover how to create a custom component from scratch.

*In the current version of Microtica, we support AWS CloudFormation as a way to write cloud infrastructure setup in a declarative way (infrastructure-as-a-code). In the future, we plan to add support for Terraform as well.*

## Get started

The first thing you need to do is add your Git account in Microtica. You can do this is Settings — Git Accounts tab. We support GitHub and BitBucket as git sources. When you’ve connected your Git account, you should create a repository that will hold your component’s code. We will be using the repository URL in the next steps of the creation process.

In the AWS accounts menu you should connect your AWS account, since will be using that to store your build artifacts and deploy your infrastructure.

![Connected GitHub account
](https://miro.medium.com/max/4058/1*A6hBp_xx42lEbhlrZMr4AA.png)

## Create a component

To make writing custom components easier for developers, we provide a (component generator)[https://www.npmjs.com/package/@microtica/generator-microtica-component] — a scaffolding module which can be found on NPM. In order to use the generator follow the instructions:

### 1. Install Yeoman as a global module

```
sudo npm install -g yo
```
### 2. Install Microtica component generator
```
sudo npm install -g @microtica/generator-microtica-component
```
### 3. Scaffold new component

Go to the root folder where you want to place the component. A component folder will be automatically created. There execute:
```
yo @microtica/microtica-component
```
This command will ask you a couple of questions, including your Git repository URL. Paste the repository URL that you’ve created previously.

After the setup is complete, you need to push the code that was just created onto the repository to be able to then use it in [Microtica portal](https://portal.microtica.com/login?utm_source=devto&utm_medium=referral_link&utm_campaign=infrastructure_on_aws). Just navigate to the component folder and execute:
```
git push origin master
```
The git repository should now be populated with the files generated with [microtica-component-generator](https://www.npmjs.com/package/@microtica/generator-microtica-component). The component should contain these definition files in the source code root folder:

1. **microtica.yaml** — [for build pipeline definition](https://microtica.atlassian.net/servicedesk/customer/portal/1/topic/a5cc9d92-3dc4-436a-98ad-89f0d5f370d0/article/92667943) (required)
2. **schema.json** — [for validation](https://microtica.atlassian.net/servicedesk/customer/portal/1/topic/a5cc9d92-3dc4-436a-98ad-89f0d5f370d0/article/92667943) (input/output parameters). What a component expects as a configuration and what it returns as an output that can be referenced by other components (required)
3. **index.js** — component handler. Handles create/update of the component (required)
4. **index.json** — cloud infrastructure definition (AWS CloudFormation template) (required)
```json
{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "test",
    "Parameters": {
        "stageId": {
            "Type": "String",
            "Description": "Unique ID of Microtica stage"
        },
        "resourceId": {
            "Type": "String",
            "Description": "Unique ID of Microtica resource"
        }
    },
    "Resources": {
        "S3Bucket": {
            "Type": "AWS::S3::Bucket",
            "Properties": {
            }
        }
    },
    "Outputs": {
        "bucketName": {
            "Value": {"Ref": "S3Bucket"},
            "Description": "Name of the S3 bucket"
        }
    }
}
```
This example of a cloud formation template is creating an S3 bucket, but you can define anything that cloud formation supports.

![cloudformation template](https://miro.medium.com/max/2328/1*6fcL6wZd0NSKwM_48GF2Mg.png)

**With this, you have the setup of a simple component 👏 .** You can follow our example on how to create an [API Gateway component](https://microtica.atlassian.net/servicedesk/customer/portal/1/topic/d57872f7-8a64-4b57-8419-d55a25710d32/article/94830605) if you like the challenge.

## Deploy the infrastructure

Navigate to your [project in Microtica](https://portal.microtica.com/login?utm_source=devto&utm_medium=referral_link&utm_campaign=infrastructure_on_aws), under Components — Create Component and import the component you’ve just created. Choose the Git account and repository where we pushed the code, and then select the AWS account where your build artifacts will be stored. You can enable *automated build trigger*, which means a build will be triggered on every push onto the selected branch filter. You can also choose to *build your component manually*.

![Create My Simple Component](https://miro.medium.com/max/4448/1*37W1_EW8_aBol6sEBZAXKg.png)

For the purposes of this article, we will build our [component-simple](https://github.com/mmarulka/component-simple) manually in the [portal](https://portal.microtica.com/login). For each build, we can follow the logs and see what is happening in real-time with our build. If our build fails, we will see that marked with red on our screen and the error will be shown in the logs.

![build details and logs](https://miro.medium.com/max/4460/1*OVjV-xqBfhmw7HhVx3wrsQ.png)

With our successful build we have a workable version of our component, which means we now have a **workable infrastructure**. The next step would be to deploy it in the cloud. 

For this, we need to create a stage, which is actually an environment in which you’ll deploy your infrastructure. Create a stage following the wizard on Microtica and then continue *Add components*. This will lead you to a screen which we call *Stage Details*. 

On the right side, you will see the component we’ve just created. Selecting the component will open configurations modal, with all the input parameters that the component needs populated in order to function in the cloud. These **input parameters** are defined in the schema.json file mentioned earlier. 

Our [component-simple](https://github.com/mmarulka/component-simple) doesn’t require inputs at the moment, so just enter a name for the resource, e.g. Simple. You can also choose which version of the component you’d like to deploy.

![stage details](https://miro.medium.com/max/4364/1*ZXTUOL79iRHcn8hh_NUn1A.png)

After you create the resource, you can deploy your stage by clicking **Create Plan**. Here you will see a detailed listing of the resources that are about to be added to the environment. Confirming will initiate the deployment and lead you to a page where you can follow the logs and events that are happening.

![stage deployment status and logs](https://miro.medium.com/max/4432/1*_dkXE7etIU05lNv2ciR_qg.png)

**And now you have a deployed infrastructure running on the cloud! Bravo!**

## Bonus: Cleanup

To remove all the resources we created on your AWS account go to stage details and in the menu select *Undeploy*. This will cleanup the resources created on AWS, but you will still have the configuration in Microtica in case you want to Deploy it in the cloud again.

![stage details](https://miro.medium.com/max/1224/0*ibbaEX9lEfkRHozp)

**As you were able to see — [Microtica](www.microtica.com?utm_source=devto&utm_medium=referral_link&utm_campaign=infrastructure_on_aws) lets you focus completely on the code you write in Cloud Formation, while abstracting the integration with AWS and automating the build and deployment process.**