Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ECS-01 feat: cria cluster ecs, service e task definition #2

Merged
merged 24 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 140 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,167 @@
## Infraestrutura
Imagem da infraestrutura do cluster ECS.

![image](./img/ecs-mentoria.png)
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0.0 |
| <a name="requirement_hashicups"></a> [hashicups](#requirement\_hashicups) | ~> 0.3.1 |
| <a name="aws"></a> [aws](#requirement\aws) | ~> 4.0 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_hashicups"></a> [hashicups](#provider\_hashicups) | 0.3.1 |
| <a name="provider_aws"></a> [aws](#aws\_aws) | 4.0 |

## Modules

No modules.
Este modulo permite provisionar cluster ECS.

### O que será criado?
- Cluster ECS.
- Service.
- Task definition.
- Politicas de AutoScaling
- Security Group.
- Load Balance.

### O que é o Amazon ECS?
O Amazon Elastic Container Service (ou ECS, para abreviar) é um serviço totalmente gerenciado de orquestração de contêineres que ajuda a implantar, gerenciar e escalar facilmente aplicações em contêineres.

## Resources
> Para saber mais sobre AWS ECS acesso o link: https://docs.aws.amazon.com/ecs/index.html

| Name | Type |
|------|------|
| [hashicups_order.order](https://registry.terraform.io/providers/hashicorp/hashicups/latest/docs/resources/order) | resource |
| [hashicups_coffees.all](https://registry.terraform.io/providers/hashicorp/hashicups/latest/docs/data-sources/coffees) | data source |
## Recursos utilizados nesse projeto:

| Name | Type | Description |
|------|------|---------- |
| [aws_ecs_cluster](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_cluster) | resource | Cria um cluster para executar os containers. |
| [aws_ecs_service](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_service) | resource | Permite executar e manter número especificado de tasks definitions em execução no cluster. |
| [aws_ecs_task_definition](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_task_definition) | resource | É um arquivo de texto em formato JSON que descreve uma ou mais características dos contêineres. |
| [aws_appautoscaling_target](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appautoscaling_target) | resource | O Auto Scaling é um serviço que permite aumentar ou diminuir a escala das tarefas (tasks). |
| [aws_appautoscaling_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/appautoscaling_policy) | resource | O Auto Scaling policy define as regras de escala de um serviço Auto Scaling. |
| [aws_iam_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource | IAM é um serviço de gerenciamento de acesso que ajuda você a controlar o acesso aos recursos da AWS. |
| [aws_iam_role_policy_attachment](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy_attachment) | resource | IAM é um serviço de gerenciamento de acesso que ajuda você a controlar o acesso aos recursos da AWS. |
| [aws_lb](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lb) | resource | Elastic Load Balance distribui automaticamente o tráfego de entrada entre as tarefas (tasks) do serviço no cluster. |
| [aws_lb_target_group](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lbhttps://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lb_target_group) | resource | Fornece um grupo de destino que onde o load balance consiga encaminhar requisições. |
EzzioMoreira marked this conversation as resolved.
Show resolved Hide resolved
| [aws_lb_listener](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lb_listener) | resource | Porta onde o balanceador de carga está escutando as requisições. |
| [aws_security_group](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/elasticache_security_group) | resource | Grupo de segurança atua como um firewall virtual. |
| [aws_cloudwatch_log_group](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group) | resource | CloudWatch responsável por armazenar os logs das task. |
| [required_providers](https://www.terraform.io/language/expressions/version-constraints) | expression | Sintaxe do Terraform para restrições de versão. |
| [provider](https://www.terraform.io/language/providers) | provider | São plugins utilizado pelo Terraform para interagir com provedores de nuvem.
| [locals](https://www.terraform.io/language/values/locals) | input | Um conjunto de valores relacionados `chave=valor` que podem ser declarados em um único bloco. |
| [variables](https://www.terraform.io/language/values/variables) | input | Serve como parâmetros para que um usuário possam personalizar o comportamento sem editar o código fonte. |
EzzioMoreira marked this conversation as resolved.
Show resolved Hide resolved

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_order"></a> [order](#input\_order) | Mapa de cafe e quantidade | `map(number)` | n/a | yes |
| create\_cluster | Define se cluster será criado | `bool` | true | no |
| cluster\_name | Nome do cluster ECS | `string` | n/a | yes |
| container\_insights | Usado para habilitar CloudWatch Container Insights para o cluster | `bool` | true | no |
| delete\_protection | Impede que terraform exclua o load balance | `bool` | false | no |
| region | Região AWS | `string` | n/a | yes |
| service\_name | Nome do service cluster que será criado | `string` | n/a | yes |
| tags | Tags para recurso | `map(string)` | n/a | yes |
| service\_name | Nome do service cluster que será criado | `string` | n/a | yes |
| app\_count | Números de tarefas em execução task definition | `number` | n/a | yes |
| family\_name | Nome para task definition | `string` | n/a | yes |
| fargate\_cpu | Número de CPUs usados na taskde finition | `number` | n/a | yes |
EzzioMoreira marked this conversation as resolved.
Show resolved Hide resolved
| fargate\_memory | Quantidade de memória usada pela task definition | `number` | n/a | yes |
| subnet\_ids | ID das subnetes | `list(string)` | n/a | yes |
| vpc\_id | ID da VPC | `string` | n/a | yes |
| app\_port | Porta que será utilizada pela aplicação | `number` | n/a | yes |
| protocol | Protocolo que será utilizado na aplicação `<http, https, tcp>` | `string` | n/a | yes |
| policy\_ssl | Nome da política SSL. Obrigatório se o protocolo for `HTTPS ou TLS` | `string` | null | no |
| certificate\_arn | ARN do certificado de servidor SSL padrão | `string` | null | no |
| template\_container | Um arquivo json que contém as definições do container | `list(object)` | n/a | no |
| tags | Tag para recursos criados | `map(string)` | n/a | yes |


## Outputs

| Name | Description |
|------|-------------|
| <a name="output_order_id"></a> [order\_id](#output\_order\_id) | n/a |
| loadbalance_dns_name | Endereço DNS do loadbalance |

## Como usar esse módulo
[Acesse o passo-a-passo](how-to-use-this-module/README.md)
[Acesse o passo-a-passo](how-to-use-this-module/README.md)

---
# Utilizando o módulo

### Pré-Requisitos

| Name | Version |
|------|---------|
| [terraform](#requirement\_terraform) | >= 1.0.0 |

### Criar `terrafile.tf`
Crie um arquivo `terraform.tf` com o seguinte conteúdo. E altere os valores das variáveis.

```hcl
module "ecs_mentoria" {
source = "[email protected]:mentoriaiac/iac-modulo-aws-ecs.git?ref=v1.0"
cria_cluster = true
app_port = 80
region = "us-east-1"
app_count = 1
fargate_cpu = 256
fargate_memory = 512
subnet_ids = ["<ID_SUBNET_X>", "<ID_SUBNET_Y>"]
vpc_id = "<ID_AWS_VPC>"
protocol = "HTTP"
family_name = "<NAME>"
service_name = "<NAME>"
cluster_name = "<NAME>"
template_container = [{
name = "<NAME>"
image = "<ENDEREÇO_REGISTRER_IMAGEM_DOCKER>"
cpu = 128
memory = 256
essential = true
portMappings = [{
containerPort = 80
hostPort = 80
}]
logConfiguration = {
logDriver = "awslogs"
options = {
awslogs-group = "<NAME>"
awslogs-region = "us-east-1"
awslogs-stream-prefix = "<NAME>"

}
}
}]
}

output "load_balancer_dns_name" {
value = "http://${module.ecs_mentoria.loadbalance_dns_name}"
}

```

### Provisionando Cluster ECS
Após preencher os valores requiridos utilize os comandos abaixo para provisonar cluster e suas aplicação.

```shell
terraform init
terraform fmt
terraform validate
terraform plan
terraform apply
```

### Descrição dos comandos:
Segue uma breve descrição dos comandos listados acima.
>**terraform init**: Execute o terraform init para baixar todos os plugins necessários.

>**terraform fmt**: O comando é usado para reescrever os arquivos de configuração do Terraform para um formato e estilo canônicos.

>**terraform validate**: Comando valida sintaticamente os arquivos de configuração em um diretório.

>**terraform plan**: Executar um plano de terraform e colocá-lo em um arquivo chamado plano.

>**terraform apply**: Usa plano para aplicar as alterações na AWS.
86 changes: 86 additions & 0 deletions autoscaling.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
resource "aws_appautoscaling_target" "ecs_target" {
count = local.cluster_count
max_capacity = 2
min_capacity = 1
EzzioMoreira marked this conversation as resolved.
Show resolved Hide resolved
resource_id = "service/${aws_ecs_cluster.cluster_iac[0].name}/${aws_ecs_service.service_cluster[0].name}"
scalable_dimension = "ecs:service:DesiredCount"
service_namespace = "ecs"
depends_on = [aws_ecs_service.service_cluster]
EzzioMoreira marked this conversation as resolved.
Show resolved Hide resolved
EzzioMoreira marked this conversation as resolved.
Show resolved Hide resolved
}

resource "aws_appautoscaling_policy" "up" {
count = local.cluster_count
name = "cluster-scale-up"
policy_type = "StepScaling"
resource_id = aws_appautoscaling_target.ecs_target[0].resource_id
scalable_dimension = aws_appautoscaling_target.ecs_target[0].scalable_dimension
service_namespace = aws_appautoscaling_target.ecs_target[0].service_namespace

step_scaling_policy_configuration {
adjustment_type = "ChangeInCapacity"
cooldown = 60
metric_aggregation_type = "Maximum"

step_adjustment {
metric_interval_upper_bound = 0
scaling_adjustment = 1
}
}
depends_on = [aws_appautoscaling_target.ecs_target]
EzzioMoreira marked this conversation as resolved.
Show resolved Hide resolved
EzzioMoreira marked this conversation as resolved.
Show resolved Hide resolved
}

resource "aws_appautoscaling_policy" "down" {
count = local.cluster_count
name = "cluster-scale-down"
policy_type = "StepScaling"
resource_id = aws_appautoscaling_target.ecs_target[0].resource_id
scalable_dimension = aws_appautoscaling_target.ecs_target[0].scalable_dimension
service_namespace = aws_appautoscaling_target.ecs_target[0].service_namespace

step_scaling_policy_configuration {
adjustment_type = "ChangeInCapacity"
cooldown = 60
metric_aggregation_type = "Maximum"

step_adjustment {
metric_interval_upper_bound = 0
scaling_adjustment = -1
}
lgfa29 marked this conversation as resolved.
Show resolved Hide resolved
}
depends_on = [aws_appautoscaling_target.ecs_target]
EzzioMoreira marked this conversation as resolved.
Show resolved Hide resolved
EzzioMoreira marked this conversation as resolved.
Show resolved Hide resolved

EzzioMoreira marked this conversation as resolved.
Show resolved Hide resolved
}

resource "aws_appautoscaling_policy" "memory" {
count = local.cluster_count
name = "memory"
policy_type = "TargetTrackingScaling"
resource_id = aws_appautoscaling_target.ecs_target[0].resource_id
scalable_dimension = aws_appautoscaling_target.ecs_target[0].scalable_dimension
service_namespace = aws_appautoscaling_target.ecs_target[0].service_namespace

target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "ECSServiceAverageMemoryUtilization"
}

target_value = 80
}
}

resource "aws_appautoscaling_policy" "cpu" {
count = local.cluster_count
name = "cpu"
policy_type = "TargetTrackingScaling"
resource_id = aws_appautoscaling_target.ecs_target[0].resource_id
scalable_dimension = aws_appautoscaling_target.ecs_target[0].scalable_dimension
service_namespace = aws_appautoscaling_target.ecs_target[0].service_namespace

target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "ECSServiceAverageCPUUtilization"
}

target_value = 70
}
}
7 changes: 7 additions & 0 deletions cloudwatch.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
resource "aws_cloudwatch_log_group" "main" {
name = element(var.template_container.*.logConfiguration.options.awslogs-group, 0)
EzzioMoreira marked this conversation as resolved.
Show resolved Hide resolved
retention_in_days = "7"
kms_key_id = null #tfsec:ignore:AWS089

tags = var.tags
}
11 changes: 11 additions & 0 deletions cluster.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
resource "aws_ecs_cluster" "cluster_iac" {
count = local.cluster_count
name = var.cluster_name

setting {
name = "containerInsights"
value = var.container_insights ? "enabled" : "disabled"
}

tags = var.tags
}
Loading