如何访问 python 金丝雀代码中的地形变量?

How to access terraform variables in python canary code?

我创建了一个 aws_synthetics_canary。我想在 python 金丝雀脚本中访问 LOGIN_IDPASSWORD terraform 变量。我试过这个

run_config {
    environment_variables = {
      LOGIN_ID = var.LOGIN_ID
      PASSWORD = var.PASSWORD
    }

在 运行 上面的 terraform 片段代码显示错误

 Error: Unsupported argument
│ 
│   on modules/canary/canary.tf line 44, in resource "aws_synthetics_canary" "sre-canary":
│   44:     environment_variables = {
│ 
│ An argument named "environment_variables" is not expected here

任何人都可以指导我这样做吗?谢谢:)

请在运行terraform init之后运行terraform version检查您使用的AWS提供商的版本。 run_config 块中的 environment_variable 密钥已添加到 AWS 提供商的 4.5.0 版本中。如果您指的是文档,默认情况下它会向您显示最新版本的提供程序,在本文发布时为 4.13.0。

v4.5.0 provider documentation.

如果可以选择将 AWS 提供商的版本从您当前使用的版本升级到至少 4.5.0,那么您将能够使用 environment_variables 块。如果没有,您将不得不寻找其他解决方案。

我找到了解决方案 here

我们必须创建一个空资源,它将在 trigger 块中提及​​的某些参数更新时执行 aws synthetics update-canary 命令。在 provisioner 块中,我们必须设置要执行的命令。

locals {
  canary_name                   = "foo"
  canary_timeout_in_seconds     = 30
  canary_memory_limit_in_mb     = 960
  canary_active_tracing_enabled = false
  canary_environment_variables  = { FOO: "BAR" }
  set_canary_run_config_command = "aws synthetics update-canary --name ${local.canary_name} --run-config '${jsonencode({TimeoutInSeconds: local.canary_timeout_in_seconds, MemoryInMB: local.canary_memory_limit_in_mb, ActiveTracing: local.canary_active_tracing_enabled, EnvironmentVariables: local.canary_environment_variables })}'"
}

resource "aws_synthetics_canary" "healthchecks" {
  name                      = local.canary_name
  start_canary              = true
  s3_bucket                 = aws_s3_bucket_object.canary_script.bucket
  s3_key                    = aws_s3_bucket_object.canary_script.key
  artifact_s3_location      = "s3://${aws_s3_bucket.canary_bucket.id}/"
  execution_role_arn        = aws_iam_role.canary.arn
  handler                   = "apiCanaryBlueprint.handler"
  runtime_version           = "syn-nodejs-puppeteer-3.3"

  schedule {
    expression = "rate(1 hour)"
  }

  run_config {
    active_tracing      = local.canary_active_tracing_enabled
    memory_in_mb        = local.canary_memory_limit_in_mb
    timeout_in_seconds  = local.canary_timeout_in_seconds
  }
}

resource "null_resource" "add_environment_variables_to_canary" {
  # Run this command again whenever any of the run-config parameters change
  triggers = {
    canary_active_tracing_enabled = local.canary_active_tracing_enabled
    canary_memory_limit_in_mb     = local.canary_memory_limit_in_mb
    canary_timeout_in_seconds     = local.canary_timeout_in_seconds
    # Trigger values must be strings (or implicitly coerced into strings, like bools), so turn env vars into a string like FOO=bar,FIZZ=buzz
    canary_environment_variables  = join(",", [ for key, value in local.canary_environment_variables: "${key}=${value}" ])
  }
  provisioner "local-exec" {
    command = local.set_canary_run_config_command
  }

  depends_on = [ aws_synthetics_canary.healthchecks ]
}