|
| 1 | +variable "datadog_agent_sidecar_enabled" { |
| 2 | + type = bool |
| 3 | + default = false |
| 4 | + description = "Enable the Datadog Agent Sidecar" |
| 5 | +} |
| 6 | + |
| 7 | +variable "datadog_log_method_is_firelens" { |
| 8 | + type = bool |
| 9 | + default = false |
| 10 | + description = "Datadog logs can be sent via cloudwatch logs (and lambda) or firelens, set this to true to enable firelens via a sidecar container for fluentbit" |
| 11 | +} |
| 12 | + |
| 13 | +variable "datadog_sidecar_containers_logs_enabled" { |
| 14 | + type = bool |
| 15 | + default = true |
| 16 | + description = "Enable the Datadog Agent Sidecar to send logs to aws cloudwatch group, requires `datadog_agent_sidecar_enabled` to be true" |
| 17 | +} |
| 18 | + |
| 19 | +variable "datadog_logging_tags" { |
| 20 | + type = map(string) |
| 21 | + default = null |
| 22 | + description = "Tags to add to all logs sent to Datadog" |
| 23 | +} |
| 24 | + |
| 25 | +variable "datadog_logging_default_tags_enabled" { |
| 26 | + type = bool |
| 27 | + default = true |
| 28 | + description = "Add Default tags to all logs sent to Datadog" |
| 29 | +} |
| 30 | + |
| 31 | +locals { |
| 32 | + default_datadog_tags = var.datadog_logging_default_tags_enabled ? { |
| 33 | + env = module.this.stage |
| 34 | + account = format("%s-%s-%s", module.this.tenant, module.this.environment, module.this.stage) |
| 35 | + } : null |
| 36 | + |
| 37 | + all_dd_tags = join(",", [for k, v in merge(local.default_datadog_tags, var.datadog_logging_tags) : format("%s:%s", k, v)]) |
| 38 | + |
| 39 | + datadog_logconfiguration_firelens = { |
| 40 | + logDriver = "awsfirelens" |
| 41 | + options = var.datadog_agent_sidecar_enabled ? { |
| 42 | + Name = "datadog", |
| 43 | + apikey = module.datadog_configuration.datadog_api_key, |
| 44 | + Host = format("http-intake.logs.%s", module.datadog_configuration.datadog_site) |
| 45 | + dd_service = module.this.name, |
| 46 | + dd_tags = local.all_dd_tags, |
| 47 | + dd_source = "ecs", |
| 48 | + dd_message_key = "log", |
| 49 | + TLS = "on", |
| 50 | + provider = "ecs" |
| 51 | + } : {} |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +module "datadog_sidecar_logs" { |
| 56 | + source = "cloudposse/cloudwatch-logs/aws" |
| 57 | + version = "0.6.6" |
| 58 | + |
| 59 | + # if we are using datadog firelens we don't need to create a log group |
| 60 | + count = local.enabled && var.datadog_agent_sidecar_enabled && var.datadog_sidecar_containers_logs_enabled ? 1 : 0 |
| 61 | + |
| 62 | + stream_names = lookup(var.logs, "stream_names", []) |
| 63 | + retention_in_days = lookup(var.logs, "retention_in_days", 90) |
| 64 | + |
| 65 | + principals = merge({ |
| 66 | + Service = ["ecs.amazonaws.com", "ecs-tasks.amazonaws.com"] |
| 67 | + }, lookup(var.logs, "principals", {})) |
| 68 | + |
| 69 | + additional_permissions = concat([ |
| 70 | + "logs:CreateLogStream", |
| 71 | + "logs:DeleteLogStream", |
| 72 | + ], lookup(var.logs, "additional_permissions", [])) |
| 73 | + |
| 74 | + context = module.this.context |
| 75 | +} |
| 76 | + |
| 77 | +module "datadog_container_definition" { |
| 78 | + source = "cloudposse/ecs-container-definition/aws" |
| 79 | + version = "0.58.1" |
| 80 | + |
| 81 | + count = local.enabled && var.datadog_agent_sidecar_enabled ? 1 : 0 |
| 82 | + |
| 83 | + container_cpu = 256 |
| 84 | + container_memory = 512 |
| 85 | + container_name = "datadog-agent" |
| 86 | + container_image = "public.ecr.aws/datadog/agent:latest" |
| 87 | + essential = true |
| 88 | + map_environment = { |
| 89 | + "ECS_FARGATE" = var.task.launch_type == "FARGATE" ? true : false |
| 90 | + "DD_API_KEY" = module.datadog_configuration.datadog_api_key |
| 91 | + "DD_SITE" = module.datadog_configuration.datadog_site |
| 92 | + "DD_ENV" = module.this.stage |
| 93 | + "DD_LOGS_ENABLED" = true |
| 94 | + "DD_LOGS_CONFIG_CONTAINER_COLLECT_ALL" = true |
| 95 | + "SD_BACKEND" = "docker" |
| 96 | + "DD_PROCESS_AGENT_ENABLED" = true |
| 97 | + "DD_DOGSTATSD_NON_LOCAL_TRAFFIC" = true |
| 98 | + "DD_APM_ENABLED" = true |
| 99 | + "DD_CONTAINER_LABELS_AS_TAGS" = jsonencode({ |
| 100 | + "org.opencontainers.image.revision" = "version" |
| 101 | + }) |
| 102 | + } |
| 103 | + |
| 104 | + // Datadog DogStatsD/tracing ports |
| 105 | + port_mappings = [{ |
| 106 | + containerPort = 8125 |
| 107 | + hostPort = 8125 |
| 108 | + protocol = "udp" |
| 109 | + }, { |
| 110 | + containerPort = 8126 |
| 111 | + hostPort = 8126 |
| 112 | + protocol = "tcp" |
| 113 | + }] |
| 114 | + |
| 115 | + log_configuration = var.datadog_sidecar_containers_logs_enabled ? { |
| 116 | + logDriver = "awslogs" |
| 117 | + options = { |
| 118 | + "awslogs-group" = one(module.datadog_sidecar_logs[*].log_group_name) |
| 119 | + "awslogs-region" = var.region |
| 120 | + "awslogs-stream-prefix" = "datadog-agent" |
| 121 | + } |
| 122 | + } : null |
| 123 | +} |
| 124 | + |
| 125 | +module "datadog_fluent_bit_container_definition" { |
| 126 | + source = "cloudposse/ecs-container-definition/aws" |
| 127 | + version = "0.58.1" |
| 128 | + |
| 129 | + count = local.enabled && var.datadog_agent_sidecar_enabled ? 1 : 0 |
| 130 | + |
| 131 | + container_cpu = 256 |
| 132 | + container_memory = 512 |
| 133 | + container_name = "datadog-log-router" |
| 134 | + # From Datadog Support: |
| 135 | + # In this case, the newest container image with the latest tag (corresponding to version 2.29.0) looks like it is crashing for certain customers, which is causing the Task to deprovision. |
| 136 | + # Note: We recommend customers to use the stable tag for this type of reason |
| 137 | + container_image = "amazon/aws-for-fluent-bit:stable" |
| 138 | + essential = true |
| 139 | + firelens_configuration = { |
| 140 | + type = "fluentbit" |
| 141 | + options = { |
| 142 | + config-file-type = "file", |
| 143 | + config-file-value = "/fluent-bit/configs/parse-json.conf", |
| 144 | + enable-ecs-log-metadata = "true" |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + log_configuration = var.datadog_sidecar_containers_logs_enabled ? { |
| 149 | + logDriver = "awslogs" |
| 150 | + options = { |
| 151 | + "awslogs-group" = one(module.datadog_sidecar_logs[*].log_group_name) |
| 152 | + "awslogs-region" = var.region |
| 153 | + "awslogs-stream-prefix" = "datadog-log-router" |
| 154 | + } |
| 155 | + } : null |
| 156 | +} |
0 commit comments