Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot set variables into gitlab-ci #2110

Closed
masbolgankezderim opened this issue Apr 22, 2024 · 1 comment
Closed

Cannot set variables into gitlab-ci #2110

masbolgankezderim opened this issue Apr 22, 2024 · 1 comment
Labels
rejected Idea rejected

Comments

@masbolgankezderim
Copy link

When I try set variables via gitlab-ci. I got ERROR bellow

╷
│ 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

.gitlab-ci.yaml

deploy:
  stage: deploy-test
  image: 
    name: devopsinfra/docker-terragrunt:tf-1.8.0-tg-0.57.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
  }
}

On my local machine terragrunt normaly work, but version - 0.57.2

@ChristophShyper
Copy link
Member

Hi! It's a rather issues with your understanding of Terraform and Terragrunt and I would advise to look up their documentation.
Right of the bat I see terragrunt plan -var="function_name=${PROJECT_NAME}" , which is not have you pass Terraform variables (-var is Terraform parameter, not Terragrunt) in CLI, it's rather terragrunt plan -var function_name="${PROJECT_NAME}" .
Also function_name = "var.function_name" is just a hardcoded string value. function_name in inputs map is passed to Terraform, so it acts as -var function_name="var.function_name" and doesn't pass any dynamic value.
You need to decide how to pass those values instead of trying two different approaches. Is ${PROJECT_NAME} an env var here? If so you can simply use it in terragrunt.hcl like function_name = get_env("PROJECT_NAME").

@ChristophShyper ChristophShyper added the rejected Idea rejected label Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rejected Idea rejected
Development

No branches or pull requests

2 participants