Skip to main content

X-Ray Tracing (Sampling Rule)

๐Ÿ“ฆ Overviewโ€‹

This Terraform module provisions an AWS X-Ray sampling rule to enable distributed tracing across microservices, APIs, and backend services.


โœ… Key Featuresโ€‹

  • Creates configurable X-Ray sampling rules
  • Supports filtering by service name, host, HTTP method, and path
  • Enables granular trace collection for better debugging and observability
  • Automatically tags resources for environment visibility

๐Ÿ’ผ Use Casesโ€‹

  • Visualize distributed request flows across microservices
  • Debug latency or performance issues in complex systems
  • Collect tracing data in a controlled and cost-efficient manner
  • Use with AWS X-Ray SDK or OpenTelemetry instrumentation

๐Ÿ“ฅ Input Variablesโ€‹

NameTypeDescription
regionstringAWS region for the deployment
rule_namestringName of the X-Ray sampling rule
service_namestringName of the service the rule applies to
prioritynumberPriority of the rule (lower number = higher priority)
fixed_ratenumberPercentage (0.0โ€“1.0) of requests to trace
http_methodstringHTTP method pattern to match (e.g., "*" or "POST")
hoststringHost pattern to match (e.g., "*" or "example.com")
url_pathstringURL path pattern to match (e.g., "*" or "/api/*")
environmentstringEnvironment tag for resource visibility (e.g., dev, staging, prod)

๐Ÿ“ค Outputsโ€‹

NameDescription
sampling_rule_nameThe name of the created X-Ray sampling rule
sampling_rule_arnThe ARN of the sampling rule

๐Ÿš€ Deployment Readinessโ€‹

  • โœ… Compatible with Terraform 1.0+
  • โœ… Requires AWS Provider 5.x+
  • ๐Ÿ” IAM permissions required: xray:PutSamplingRule, xray:CreateSamplingRule
  • ๐Ÿงฉ Ensure your workloads are instrumented with X-Ray SDK or OpenTelemetry

๐Ÿ› ๏ธ Example Usageโ€‹

module "xray_tracing_deployment" {
source = "git::ssh://git@github.com/archiphire/aws-level-1-modules.git//observability/xray-tracing?ref=v1.0.0"

region = "us-east-1"
rule_name = "trace-prod-traffic"
service_name = "api-service"
priority = 100
fixed_rate = 0.05
http_method = "*"
host = "*"
url_path = "*"
environment = "prod"
}
terraform init
terraform plan
terraform apply

or

tofu init
tofu plan
tofu apply

๐Ÿงน Cleanup Optionsโ€‹

Terraform Destroy:

terraform destroy

or

tofu destroy

Manual Deletion (for Production):

aws xray delete-sampling-rule --rule-name trace-prod-traffic

๐Ÿ›ก๏ธ Notesโ€‹

This is a Level 1 observability module.
To take full advantage of tracing, be sure to instrument services using AWS X-Ray SDK or the AWS Distro for OpenTelemetry (ADOT).

You can use multiple sampling rules to tailor tracing across environments, services, or paths.


๐Ÿ”’ Deployment Package Noticeโ€‹

This module is part of the Level 1 AWS License Tier.
To access the full deployment package and source code, subscribe to Archiphire.