Hello! I was trying to make Terraform Gitlab CI i...
# general
f
Hello! I was trying to make Terraform Gitlab CI implementation yet it looks that .gitlab-ci.yml file does not work for me. I changed the _TF_ROOT_ path. After I made the merge request, pipeline has failed. Followed the manual from here. The error I get is '_variables config should be a hash of key value pairs, value can be a hash_'. Tried googling it and found that sometimes variables need to have additional pair of quotation marks " ". Yet there was no such case in .gitlab.ci.yml. Perhaps anyone has an idea why the pipeline fails despite following the implementation manual step-by-step?
b
Hello! Is it possible to share the config you have so we could try to reproduce the issue?
f
Yes. May I clarify what kind of information you require?
b
I think we can start with the
variables
list (that includes
TF_ROOT
), of course please replace any sensitive information in there 🙂.
As you said, you were following our example, so I assume this is the only part that was changed, right?
f
Right, here is the only changed part. I changed TF_ROOT to directory of my repo:
Copy code
variables:
  # If your terraform files are in a subdirectory, set TF_ROOT accordingly
  TF_ROOT: code
  # This instructs the CLI to send cost estimates to Infracost Cloud. Our SaaS product
  #   complements the open source CLI by giving teams advanced visibility and controls.
  #   The cost estimates are transmitted in JSON format and do not contain any cloud 
  #   credentials or secrets (see <https://infracost.io/docs/faq/> for more information).
  INFRACOST_ENABLE_CLOUD: true
stages:
  - infracost
b
Okay, and you also tried to do
TF_ROOT: "code"
with no luck. Do I understand that correctly?
f
Yes
b
Hm, this is strange. Okay, let me try to do the same quickly and I'll get back to you. Our example config is well tested, so I wonder what could happen.
f
Everything looked fine for me, too. Sure, take your time.
b
Yep, I'm seeing it too. Looking for the issue 🙂
Okay, found the issue: apparently GitLab doesn't like a boolean value
true
for
INFRACOST_ENABLE_CLOUD: true
. If you change it to
INFRACOST_ENABLE_CLOUD: "true"
, it should start to work
Apparently, it's an old one and people keep struggling with this 🙂 https://gitlab.com/gitlab-org/gitlab/-/issues/21587
I'll update our example to fix that. Sorry that you have to deal with that, but thank you for reaching out so other users won't have to 🙂
f
Thank you for that. Looks great!
b
Awesome! Thank you! 🙂 Just FYI, this is a merge request with the fixes: https://gitlab.com/infracost/infracost-gitlab-ci/-/merge_requests/50/diffs
f
One little question, where can I see the infracost cost estimation part in your example?
b
By default, this example would post a comment to your merge request, like this one: https://gitlab.com/infracost/gitlab-ci-demo/-/merge_requests/31#note_1067358266 And, if you got your Infracost API key from Infracost Cloud, you should see it there: https://dashboard.infracost.io
f
Awesome, thank you 🙂
b
You're very welcome! 🙂
f
Sorry for ping again. I added API key for Infracost as variable, which is not protected yet I get error about it.
Copy code
Error: No INFRACOST_API_KEY environment variable is set.
We run a free Cloud Pricing API, to get an API key run infracost auth login
ParserError: 
Line |
   1 |                 --compare-to=infracost-base.json \
     |                   ~
     | Missing expression after unary operator '--'.
ParserError: 
Line |
   1 |                 --format=json \
     |                   ~
     | Missing expression after unary operator '--'.
ParserError: 
Line |
   1 |                 --out-file=infracost.json
     |                   ~
     | Missing expression after unary operator '--'.
Do I need to do any additional steps for it?
b
No worries. Hm, The error says that the CLI can't detect the env var. May I ask how you set the
INFRACOST_API_KEY
?
f
Settings - CI/CD - Variables
b
Interesting, these parse errors are also looking strange
f
As I understood, they are because of existing space after the lines. But not completely sure - tried googling.
I am using windows and shell
b
Oh, do you run these commands from your local shell or in CI?
f
I made everything for infracost and gitlab runner from local shell
The whole output:
Copy code
Running with gitlab-runner 15.2.1 (32fc1585)
  on LTUGPLTP0083 zJgg5nXb
Preparing the "shell" executor
00:00
Using Shell executor...
Preparing environment
00:00
Running on LTUGPLTP0083...
Getting source from Git repository
00:06
Fetching changes with git depth set to 20...
Reinitialized existing Git repository in C:/GitLab-Runner/builds/zJgg5nXb/0/gitlab/karolis.zivulskas/awstask/.git/
Checking out ef87834d as refs/merge-requests/4/head...
git-lfs/3.2.0 (GitHub; windows amd64; go 1.18.2)
Skipping Git submodules setup
Executing "step_script" stage of the job script
ParserError: 
Line |
 280 |                      --format=json \
     |                        ~
     | Missing expression after unary operator '--'.
$ infracost diff --path=${TF_ROOT} \ # collapsed multi-line command
Error: No INFRACOST_API_KEY environment variable is set.
We run a free Cloud Pricing API, to get an API key run infracost auth login
ParserError: 
Line |
   1 |                 --compare-to=infracost-base.json \
     |                   ~
     | Missing expression after unary operator '--'.
ParserError: 
Line |
   1 |                 --format=json \
     |                   ~
     | Missing expression after unary operator '--'.
ParserError: 
Line |
   1 |                 --out-file=infracost.json
     |                   ~
     | Missing expression after unary operator '--'.
Cleaning up project directory and file based variables
00:00
ERROR: Job failed: exit status 1
b
Oh, sorry, you're using a gitlab-runner. Sorry, I don't have much experience with GitLab, so this is something new to me 😅. Let me read about it, I'm not sure we actually tested our examples with it 🙂
f
Shall I use something else?
b
I had an impression that you were running it on gitlab.com. If you specify the CI/CD pipeline there, every new merge request would start a new job automatically.
Our example uses Infracost-ci linux docker image, so GitLab would use it to run the CLI commands
To resolve the parse errors, you could make all these commands as one-liners - putting all flags
--X
on the same line with
infracost
command and removing backslashes. We can start from this and see what fails next
f
How could I directly run it on gitlab?
b
You'll need to register on gitlab.com, create a repo there (it's free), and push your code there. Then you can create a new CI/CD pipeline using our example. GitLab has a good documentation, here's for the CI/CD setup https://docs.gitlab.com/ee/ci/quick_start/
f
I did it. I do everything from Gitlab website, just I used shell for gitlab runner setup and infracost setup.
b
Oops, okay, sorry for misunderstanding. So, if you set up the pipeline on GitLab.com, you probably can just push a new branch to your repo and create a new merge request. This will automatically kick off the pipeline job
f
Yes, this is what I did. Then, those errors occurred.
b
Okay, so if I understand correctly: you're using GitLab SaaS with a windows runner on your local machine. It loads the config, even fetches git code, but fails on the step with infracost commands. Does it sound right?
I'm sorry again, as I have very little knowledge about such setup and I got you and myself confused in the process :)
f
I don't know why but my infracost api key is missing according to the error. Yet I have it at my project CI/CD variables
This is the one error. Another one is the parser error: Missing expression after unary operator '--'. I will try your tip with flags in single line.
b
Seems like the windows shell doesn't recognize the env vars. Quick search gave me this: https://stackoverflow.com/questions/49214055/powershell-wont-recognize-gitlab-ci-environment-variables. We could try replacing
$X
env vars with
$env:X
and see if it works
f
Worked with single line flags and infracost api key as $env:X. That was it. Thank you for such a great help from start to the end.
b
Awesome! Happy to hear it worked out! 🙂
w
Should we add a note about this issue in the troubleshooting section?
b
Yep, sounds like a good idea
@famous-city-17122 May I ask you for a favour? We're going to update the troubleshooting guide with these issues, so maybe you can take a quick look and see if it makes sense to you? This can help other people in future 🙂 https://gitlab.com/infracost/infracost-gitlab-ci/-/merge_requests/50/diffs#8ec9a00bfd09b3190ac6b22251dbb1aa95a0579d_133_154 🙏
f
Super clear and I hope will be useful 🙂
b
Thanks! 🙂