gruntwork-io / terragrunt

Terragrunt is a flexible orchestration tool that allows Infrastructure as Code written in OpenTofu/Terraform to scale.
https://terragrunt.gruntwork.io/
MIT License
8.09k stars 981 forks source link

Help with set variables via gitlab-ci #3081

Open masbolgankezderim opened 6 months ago

masbolgankezderim commented 6 months ago

Describe the bug I cannot set variables via gitlab-ci

deploy:
  stage: deploy-test
  image: 
    name: alpine/terragrunt:1.8.1
  before_script:
    - terragrunt plan -var="function_name=${PROJECT_NAME}" -var="image_uri=123456789.dkr.ecr.${ECR_REGION}.amazonaws.com/${CI_PROJECT_NAME}:${PROJECT_NAME}-${CI_COMMIT_SHORT_SHA}"
  script:
    - terragrunt apply -var="function_name=${PROJECT_NAME}" -var="image_uri=123456789.dkr.ecr.${ECR_REGION}.amazonaws.com/${CI_PROJECT_NAME}:${PROJECT_NAME}-${CI_COMMIT_SHORT_SHA}" --auto-approve

terragrunt.hcl

include "root" {
  path = find_in_parent_folders()
}

terraform {
  source = "https://github.com/terraform-aws-modules/terraform-aws-lambda.git"
}

inputs = {
  function_name   = "var.function_name"
  description     = "My awesome lambda function 5555555"
  create_package  = false
  image_uri       = "var.image_uri"
  package_type    = "Image"
}

terragrunt.hcl in root folder

remote_state {
  backend = "s3"
  generate = {
    path      = "backend.tf"
    if_exists = "overwrite"
  }
  config = {
    bucket         = "test-bucket"
    key            = "${path_relative_to_include()}/terraform.tfstate"
    region         = "eu-north-1"
    encrypt        = true
  }
}

Nice to have

╷
│ Error: Value for undeclared variable
│ 
│ A variable named "function_name" was assigned on the command line, but the
│ root module does not declare a variable of that name. To use this value,
│ add a "variable" block to the configuration.
╵
╷
│ Error: Value for undeclared variable
│ 
│ A variable named "image_uri" was assigned on the command line, but the root
│ module does not declare a variable of that name. To use this value, add a
│ "variable" block to the configuration.
╵
time=2024-04-18T09:26:[30](https://gitlab-test.dev/test-ops/-/jobs/127566#L30)Z level=error
time=2024-04-18T09:26:30Z level=error 
ERROR: Job failed: exit code 1

Screenshot

Screenshot 2024-04-22 at 13 14 54

Versions

tmaier commented 5 months ago
  function_name   = "var.function_name"
  description     = "My awesome lambda function 5555555"
  create_package  = false
  image_uri       = "var.image_uri"

var.function_name and var.image_uri are never called, since they are wrapped in double quotes and thus, handled as string.

I think the following should work:

  function_name   = var.function_name
  description     = "My awesome lambda function 5555555"
  create_package  = false
  image_uri       = var.image_uri
denis256 commented 5 months ago

Hi, also, variables can be set through env variables TF_name

https://developer.hashicorp.com/terraform/cli/config/environment-variables#tf_var_name