Home > front end >  How to access terraform variables in python canary code?
How to access terraform variables in python canary code?

Time:05-10

I have created an aws_synthetics_canary. I want to access LOGIN_ID and PASSWORD terraform variables in python canary script. I have tried this

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

On running the code with above terraform snippet it's showing error

 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

Can anyone please guide me on doing this? Thank you :)

CodePudding user response:

Please check the version of the AWS provider that you are using by running terraform version after running terraform init. The environment_variable key in the run_config block was added in version 4.5.0 of the AWS provider. If you're referring to the documentation, it'll show you the latest version of the provider by default, which is 4.13.0 at the time of this posting.

v4.5.0 provider documentation.

If upgrading the version of the AWS provider from what you are currently using to at least 4.5.0 is an option, then you will be able to use the environment_variables block. If not, you'll have to find another solution.

CodePudding user response:

I have found the solution here.

We have to create a null resource which will execute aws synthetics update-canary command upon updation of certain paramters which can be mentioned in the trigger block. In provisioner block we have to set the command to be executed.

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 ]
}
  • Related