Trying to pass terraform variables through cli

497 Views Asked by At

I'm trying to pass terraform variables through CLI because they are dependent on my github actions workflow. So my main.tf currently looks like this.

resource "aws_lambda_function" "etl_pipeline_test" {
  function_name = var.project_name
  image_uri = "${var.ecr_repo_uri}:${var.image_tag}"
  package_type = "Image"
  role = aws_iam_role.etl_pipeline_test.arn
}

It's just a terraform file which creates a lambda function from an ECR docker image. But I'm not using any variables.tf file so I just want to pass the ecr_repo_uri and image_tag through cli. So currently I'm running this command. terraform init && terraform apply -var "ecr_repo_uri=<ecr_repo_uri_var>" -var "image_tag=v0.0" To remove any confusion there is a valid ecr_repo_uri. Now I keep getting this error.

╷
│ Error: Value for undeclared variable
│
│ A variable named "ecr_repo_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.
╵
╷
│ Error: Value for undeclared variable
│
│ A variable named "image_tag" 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.

I think the problem is in my main.tf, maybe you need to address variables through cli different then with env.var like I'm doing right now. All help is greatly appreciated.

1

There are 1 best solutions below

0
On BEST ANSWER

I forgot to create a variable.tf file where you need to define the variables you are passing through with cli. For other people who might encouter this error, this is my variable.tf file.

variable "image_tag" {
  type=string
}
variable "ecr_repo_uri" {
  type=string
}

I thought you only had to create a variable.tf file when you already defined the variables, but you also have to do this when passing them through cli.