Skip to content

Commit

Permalink
CAT-24419 Added CW Application Signals setup and example
Browse files Browse the repository at this point in the history
  • Loading branch information
ehirsch-3pg committed Sep 30, 2024
1 parent 77b30b7 commit 48c4688
Show file tree
Hide file tree
Showing 11 changed files with 188 additions and 2 deletions.
2 changes: 2 additions & 0 deletions examples/app-signals/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
backend.tf
provider.tf
26 changes: 26 additions & 0 deletions examples/app-signals/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions examples/app-signals/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module "service" {
source = "../.."

hosted_zone = var.hosted_zone

enable_application_signals = true

organization = var.organization
environment = var.environment
product = var.product
repo = var.repo
}
3 changes: 3 additions & 0 deletions examples/app-signals/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "domain_name" {
value = module.service.domain_name
}
9 changes: 9 additions & 0 deletions examples/app-signals/sample-backend.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# terraform {
# backend "s3" {
# bucket = "my-bucket-tfstate"
# key = "example-terraform-aws-ecs-service-basic"
# profile = "my-profile"
# region = "us-east-1"
# dynamodb_table = "terraform-lock"
# }
# }
12 changes: 12 additions & 0 deletions examples/app-signals/sample-provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# provider "aws" {
# region = "us-east-1"
# profile = "my-profile"
# default_tags {
# tags = {
# product = var.product
# environment = var.environment
# repo = var.repo
# organization = var.organization
# }
# }
# }
45 changes: 45 additions & 0 deletions examples/app-signals/tags.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
variable "environment" {
description = "Environment (sharedtools, dev, staging, prod)"
type = string

default = "sharedtools"

validation {
condition = contains(["sharedtools", "dev", "staging", "prod"], var.environment)
error_message = "The environment variable must be one of [sharedtools, dev, staging, prod]."
}
}

variable "product" {
description = "Tag used to group resources according to application"

default = "example-tf-ecs-service-basic"

validation {
condition = can(regex("[a-z\\-]+", var.product))
error_message = "The product variable violates approved regex."
}
}

variable "repo" {
description = "Tag used to point to the repo using this module"

default = "https://github.com/pbs/terraform-ecs-service-module.git"

validation {
condition = can(regex("(?:git|ssh|https?|git@[-\\w.]+):(\\/\\/)?(.*?)(\\.git)(\\/?|\\#[-\\d\\w._]+?)$", var.repo))
error_message = "The repo variable violates approved regex."
}
}

variable "organization" {
description = "Organization using this module. Used to prefix tags so that they are easily identified as being from your organization"
type = string

default = "example"

validation {
condition = can(regex("[a-z\\-]+", var.organization))
error_message = "The organization variable violates approved regex."
}
}
4 changes: 4 additions & 0 deletions examples/app-signals/vars.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
variable "hosted_zone" {
type = string
description = "Primary hosted zone for this service. Populate `TF_VAR_hosted_zone` before running any tests to have this value populated."
}
57 changes: 57 additions & 0 deletions locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,63 @@ locals {
creator = "terraform"
application_signals_envs = var.enable_application_signals == false ? [] : [
{
"name" : "OTEL_RESOURCE_ATTRIBUTES",
"value" : "service.name=${var.product},deployment.environment=${var.environment}"
},
{
"name" : "PYTHONPATH",
"value" : var.PYTHONPATH
},
{
"name" : "OTEL_EXPORTER_OTLP_PROTOCOL",
"value" : "http/protobuf"
},
{
"name" : "OTEL_TRACES_SAMPLER",
"value" : "xray"
},
{
"name" : "OTEL_TRACES_SAMPLER_ARG",
"value" : "endpoint=http://localhost:2000"
},
{
"name" : "OTEL_LOGS_EXPORTER",
"value" : "none"
},
{
"name" : "OTEL_PYTHON_DISTRO",
"value" : "aws_distro"
},
{
"name" : "OTEL_PYTHON_CONFIGURATOR",
"value" : "aws_configurator"
},
{
"name" : "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT",
"value" : "http://localhost:4316/v1/traces"
},
{
"name" : "OTEL_AWS_APPLICATION_SIGNALS_EXPORTER_ENDPOINT",
"value" : "http://localhost:4316/v1/metrics"
},
{
"name" : "OTEL_METRICS_EXPORTER",
"value" : "none"
},
{
"name" : "OTEL_AWS_APPLICATION_SIGNALS_ENABLED",
"value" : "true"
}
]

# setunion() cannot use empty sets
env_vars = var.enable_application_signals == false ? var.env_vars : var.env_vars == null || var.env_vars == [] ? local.application_signals_envs : setunion(
local.application_signals_envs,
var.env_vars
)

defaulted_tags = merge(
var.tags,
{
Expand Down
14 changes: 14 additions & 0 deletions optional-task.tf
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,17 @@ variable "awslogs_driver_mode" {
default = "non-blocking"
type = string
}

# Clowdwatch Application Signals
variable "enable_application_signals" {
description = "(optional) if set to true, will enable CW Application Signals"
default = false
type = bool
}

variable "PYTHONPATH" {
description = "(optional) PYTHONPATH of the application; required by the cwagent sidecar container"
type = string

default = ":"
}
6 changes: 4 additions & 2 deletions task.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module "task" {
count = var.task_def_arn == null ? 1 : 0
source = "github.com/pbs/terraform-aws-ecs-task-definition-module?ref=2.0.0"
source = "github.com/pbs/terraform-aws-ecs-task-definition-module?ref=2.0.2"

name = local.name

Expand All @@ -26,7 +26,7 @@ module "task" {
virtual_node = var.virtual_node

ssm_path = var.ssm_path
env_vars = var.env_vars
env_vars = local.env_vars

efs_mounts = var.efs_mounts

Expand All @@ -35,6 +35,8 @@ module "task" {

use_xray_sidecar = var.use_xray_sidecar

use_cwagent_sidecar = var.enable_application_signals

envoy_tag = var.envoy_tag

network_mode = var.network_mode
Expand Down

0 comments on commit 48c4688

Please sign in to comment.