Stack Grafana deployed on AWS ECS -Part 1 - Create a Cluster
Tags: grafana, observability, ecs, aws
Published: 2024-04-26 18:54:01

This post is part of a series about the stack Grafana deployed on AWS ECS. This project is a simple MVP.

In part 1, we will set up our cluster ECS and LoadBalance. 

> [AWS ECS](https://aws.amazon.com/ecs/?nc1=h_ls): is a container orchestration that helps us to deploy, manage, and scale applications.

## Pre-Requirements

- Knowledge with Terraform
- AWS Account
  - 2 subnets publics, with tag: `tier = public`
  - 2 subnets private, with tag: `tier = private`

## AWS VPC

We need the following structure for AWS VPC. We'll get ID the subnets privates with the Terraform DataSource.  

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/16z79bv40mkb8qqew87l.png)

Remember the add tag `tier = private` for private subnets.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3gde41xr83zl55gn8qnv.png)

Remember the add tag `tier = public` for public subnets.  

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pto6bx0x2iaac337vxzu.png)

## ECS Cluster Type

Currently, there are three types of the ECS cluster. Cluster EC2 instance, serverless Fargate, and on-primeses VM. 

> Consult the AWS documentations for more details. https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-configuration.html

We'll use the ECS EC2 cluster for this MVP. 

### Create ECS Cluster

You can use this Terraform module to create cluster ECS.
- https://github.com/EzzioMoreira/aws-terraform-module/releases/tag/v0.0.1-ecs-cluster-alb

Below an example how to use this module. That will provide a cluster ECS type of EC2, and Application LoadBalance.  

```terraform
####################################################################################################
### ECS Cluster o11y ###
####################################################################################################

module "ecs_cluster" {
  source = "https://github.com/EzzioMoreira/aws-terraform-module//modules/ecs-cluster?ref=v0.0.1-ecs-cluster-alb"

  cluster_name  = "ecs-cluster-example"
  min_size      = 1
  max_size      = 2
  desired_size  = 1
  instance_type = "t3a.medium"
  vpc_id        = "vpc-xxxxxxxx"
  tags = {
    created_by    = "terraform"
    documentation = "null"
    env           = "prod"
    repository    = "ezziomoreira/aws-terraform-modules"
    service       = "observability"
    team          = "sre"
  }
}

####################################################################################################
### Loadbalance o11y ###
####################################################################################################
module "loadbalance" {
  source = "https://github.com/EzzioMoreira/aws-terraform-module//modules/loadbalance?ref=v0.0.1-ecs-cluster-alb"

  name               = "ecs-cluster-example-internal"
  type               = "application"
  internal           = true
  subnet_ids         = ["subnet-xyxyx", "subnet-yxyxyx"]
  security_group_ids = [sg-123123123]
  tags = {
    created_by    = "terraform"
    documentation = "null"
    env           = "prod"
    repository    = "ezziomoreira/aws-terraform-modules"
    service       = "observability"
    team          = "sre"
  }
}
```

### References 

- [AWS Elastic Container Service](https://docs.aws.amazon.com/pt_br/AmazonECS/latest/developerguide/Welcome.html)
- [Terraform Modules](https://developer.hashicorp.com/terraform/language/modules)