Skip to content

TeliaDK/terraform-aws-microservice

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

56 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TeliaDK - AWS microservice module

Used for deploying microservices into AWS and utilise service discovery + service mesh (AppMesh).

Table of contents

  1. Example
  2. Inputs
  3. Resources

Example:

locals {
  tags = {
    author = "teliaDK"
  }

  env_variables = [
    { name = "environment", value = "dev" },
  ]
}


module "microservice" {
  source  = "TeliaDK/microservice/aws"
  version = "0.0.4"

  region = "eu-west-1"

  app_name                = "my-service"
  task_execution_role_arn = "arn::executionRole"
  task_role_arn           = "arn::taskRole"
  ecs_cluster_name        = "microservice-cluster"
  vpc_id                  = "vpc-id-123"
  subnet_ids              = ["1234", "4321"]
  appmesh_name            = "service-mesh"
  env_variables           = local.env_variables
  port                    = 80

  secrets = [
    {
      name      = "SUPERSECRET"
      valueFrom = aws_ssm_parameter.certificate_password.arn
    }
  ]

  microservice_container = {
    name   = "my-service"
    image  = "1234567890.dkr.ecr.eu-west-1.amazonaws.com/my-service"
    cpu    = 224
    memory = 1280
  }

  load_balancer = {
    arn = "arn:aws:elasticloadbalancing:eu-west-1:123456789:loadbalancer/app/microservices/abababababab"
  }

  cloud_map = {
    namespace = {
      id   = "123456789"
      name = "internal.svc.acme.org"
    }
    service = {
      name = "my-service"
    }
  }

  autoscaling = {
    enabled      = true
    name         = "my-service"
    namespace    = "ecs/autoscaling"
    stage        = "dev"
    attributes   = []
    min_capacity = 1
    max_capacity = 3
  }

  autoscaling_cpu = {
    utilization_target_value = 65
    scale_in_period          = 300
    scale_out_period         = 300
  }

  tags = local.tags
}

Inputs:

Name Description Type Default Required
first_run If the microservices is being created, this should be set to true, otherwise false. When applying subsequent times (updating the infra, rather than creating from scratch), this should be set to false, which will cause the image version being deployed to be the same version as is currently deployed. Without it the latest image will be deployed, which is almost never wanted behaviour as new versions should be deployed via CI/CD not terraform. A value of true should not be committed to source control, but only updated locally when applying for the first time for the given service. bool false no
region The AWS region to deploy the compute module in string eu-west-1 no
microservice_container Settings for the microservice container object - yes
port The port that will be used for port mapping : number 8080 no
cpu The total vCPU to allocate for the ECS service. Valid configuration at https://docs.aws.amazon.com/AmazonECS/latest/developerguide/AWS_Fargate.html number 512 no
memory The total memory to allocate for the ECS service. Valid configuration at https://docs.aws.amazon.com/AmazonECS/latest/developerguide/AWS_Fargate.html number 2048 no
xray_cpu The total vCPU to allocate to the xray container number 32 no
xray_memory The total memory to allocate to the xray container number 256 no
envoy_cpu The total vCPU to allocate to the envoy container number 256 no
envoy_cpu The total memory to allocate to the envoy container number 512 no
microservice_cpu The total vCPU to allocate to the microservice number 224 no
microservice_memory The total memory to allocate to the microservice number 1280 no
ecs_cluster_name The ECS cluster to deploy the ECS Fargate into string - yes
app_name The shared name for the ECS Fargate service and task definitions string - yes
task_execution_role_arn The name of the execution role to use with the service string null no
task_role_arn The name of the task role to use with the service string null no
deployment_controller_type The deployment controller type to use in ECS service. For blue/green, CODE_DEPLOY must be used string ECS no
envoy_container_name The name of the envoy container to be used in AppMesh proxy string envoy no
envoy_ignored_uid Which UID to ignore in envoy docker container string 1337 no
service_discovery_dns_routing_policy The routing policy that you want to apply to all records that Route 53 creates when you register an instance and specify the service. Valid Values: MULTIVALUE, WEIGHTED string MULTIVALUE no
cloud_map Settings needed to setup service discovery through AWS CloudMap object - yes
env_variables Environment variables for the service object null no
secrets Secrets for the service. Use arn of parameters in parameter store for the valueFrom property object null no
vpc_id The VPC to use string - yes
subnet_ids The subnets for the ECS service network configuration list(string) - yes
appmesh_name Name of AppMesh to register service components in string - yes
tags Tags to use for the components created by the module map(string) - yes
load_balancer Load balancer config to be used in ECS service object null no
awslogs_datetime_format The format used in logs written by the application in the container. Used for ensuring that the aws log driver can parse the logs correctly and not split them into several entries (e.g. stack traces are kept in one entry). string %Y-%m-%d %H:%M:%S no
appmesh_virtual_service_name Name of AppMesh virtual service string null no
appmesh_virtual_node_backend_service Name of virtual service for AppMesh virtual node backend string null no
appmesh_virtual_node_http_idle_timeout The idle timeout for HTTP requests to the node in seconds number 15 no
appmesh_virtual_route_http_idle_timeout The idle timeout for HTTP requests to the route in seconds number 15 no
appmesh_virtual_node_http_request_timeout The request timeout for HTTP requests to the node in seconds number 15 no
appmesh_virtual_route_http_request_timeout The request timeout for HTTP requests to the route in seconds number 15 no
autoscaling Used to define and enable autoscaling for the ECS service object null no
autoscaling_alarm_description The string to format and use as the alarm description string Average service %v utilization %v last %d minute(s) over %v period(s) no
autoscaling_delimiter Delimiter between namespace, stage, name and attributes string - no
autoscaling_cpu Used to define autoscaling based on CPU usage object null no
autoscaling_memory Used to define autoscaling based on Memory usage object null no
instance_count The number of instances to run in the ECS service number 1 no
envoy_additional_configuration Map of envoy additional environment variables. (https://docs.aws.amazon.com/app-mesh/latest/userguide/envoy-config.html) list(object({name = string, value = string})) - no

Resources

The module will create the following resources:

  1. New cloudwatch group under the /ecs/{var.app_name}
  2. New security group
  3. CloudMap service
  4. AppMesh virtual router
  5. AppMesh virtual service
  6. AppMesh virtual node
  7. AppMesh route
  8. xray daemon sidecar ECS container definition
  9. envoy proxy sidecar ECS container definition
  10. microservice ECS container definition
  11. microservice ECS task definition
  12. microservice ECS service