Title
#general
k

Karolis Zivulskas

08/17/2022, 9:53 AM
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?
Vadim (Infracost)

Vadim (Infracost)

08/17/2022, 10:05 AM
Hello! Is it possible to share the config you have so we could try to reproduce the issue?
k

Karolis Zivulskas

08/17/2022, 10:06 AM
Yes. May I clarify what kind of information you require?
Vadim (Infracost)

Vadim (Infracost)

08/17/2022, 10:08 AM
I think we can start with the
variables
list (that includes
TF_ROOT
), of course please replace any sensitive information in there 🙂.
10:09 AM
As you said, you were following our example, so I assume this is the only part that was changed, right?
k

Karolis Zivulskas

08/17/2022, 10:10 AM
Right, here is the only changed part. I changed TF_ROOT to directory of my repo:
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
Vadim (Infracost)

Vadim (Infracost)

08/17/2022, 10:12 AM
Okay, and you also tried to do
TF_ROOT: "code"
with no luck. Do I understand that correctly?
k

Karolis Zivulskas

08/17/2022, 10:14 AM
Yes
Vadim (Infracost)

Vadim (Infracost)

08/17/2022, 10:15 AM
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.
k

Karolis Zivulskas

08/17/2022, 10:16 AM
Everything looked fine for me, too. Sure, take your time.
Vadim (Infracost)

Vadim (Infracost)

08/17/2022, 10:22 AM
Yep, I'm seeing it too. Looking for the issue 🙂
10:26 AM
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
10:27 AM
Apparently, it's an old one and people keep struggling with this 🙂 https://gitlab.com/gitlab-org/gitlab/-/issues/21587
10:28 AM
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 🙂
10:30 AM
k

Karolis Zivulskas

08/17/2022, 10:40 AM
Thank you for that. Looks great!
Vadim (Infracost)

Vadim (Infracost)

08/17/2022, 10:42 AM
Awesome! Thank you! 🙂 Just FYI, this is a merge request with the fixes: https://gitlab.com/infracost/infracost-gitlab-ci/-/merge_requests/50/diffs
k

Karolis Zivulskas

08/17/2022, 10:48 AM
One little question, where can I see the infracost cost estimation part in your example?
Vadim (Infracost)

Vadim (Infracost)

08/17/2022, 10:50 AM
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
k

Karolis Zivulskas

08/17/2022, 10:55 AM
Awesome, thank you 🙂
Vadim (Infracost)

Vadim (Infracost)

08/17/2022, 10:55 AM
You're very welcome! 🙂
k

Karolis Zivulskas

08/17/2022, 11:30 AM
Sorry for ping again. I added API key for Infracost as variable, which is not protected yet I get error about it.
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?
Vadim (Infracost)

Vadim (Infracost)

08/17/2022, 11:35 AM
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
?
k

Karolis Zivulskas

08/17/2022, 11:37 AM
Settings - CI/CD - Variables
Vadim (Infracost)

Vadim (Infracost)

08/17/2022, 11:41 AM
Interesting, these parse errors are also looking strange
k

Karolis Zivulskas

08/17/2022, 11:42 AM
As I understood, they are because of existing space after the lines. But not completely sure - tried googling.
11:43 AM
I am using windows and shell
Vadim (Infracost)

Vadim (Infracost)

08/17/2022, 11:44 AM
Oh, do you run these commands from your local shell or in CI?
k

Karolis Zivulskas

08/17/2022, 11:45 AM
I made everything for infracost and gitlab runner from local shell
11:47 AM
The whole output:
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
Vadim (Infracost)

Vadim (Infracost)

08/17/2022, 11:49 AM
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 🙂
k

Karolis Zivulskas

08/17/2022, 11:55 AM
Shall I use something else?
Vadim (Infracost)

Vadim (Infracost)

08/17/2022, 11:57 AM
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.
11:59 AM
Our example uses Infracost-ci linux docker image, so GitLab would use it to run the CLI commands
12:04 PM
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
k

Karolis Zivulskas

08/17/2022, 12:23 PM
How could I directly run it on gitlab?
Vadim (Infracost)

Vadim (Infracost)

08/17/2022, 12:25 PM
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/
k

Karolis Zivulskas

08/17/2022, 12:27 PM
I did it. I do everything from Gitlab website, just I used shell for gitlab runner setup and infracost setup.
Vadim (Infracost)

Vadim (Infracost)

08/17/2022, 12:29 PM
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
k

Karolis Zivulskas

08/17/2022, 12:32 PM
Yes, this is what I did. Then, those errors occurred.
Vadim (Infracost)

Vadim (Infracost)

08/17/2022, 12:39 PM
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?
12:40 PM
I'm sorry again, as I have very little knowledge about such setup and I got you and myself confused in the process 😃
k

Karolis Zivulskas

08/17/2022, 12:40 PM
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
12:42 PM
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.
Vadim (Infracost)

Vadim (Infracost)

08/17/2022, 12:43 PM
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
k

Karolis Zivulskas

08/17/2022, 1:00 PM
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.
Vadim (Infracost)

Vadim (Infracost)

08/17/2022, 1:02 PM
Awesome! Happy to hear it worked out! 🙂
Ali (Infracost)

Ali (Infracost)

08/17/2022, 1:29 PM
Should we add a note about this issue in the troubleshooting section?
Vadim (Infracost)

Vadim (Infracost)

08/17/2022, 1:30 PM
Yep, sounds like a good idea
4:35 PM
@Karolis Zivulskas 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 🙏
k

Karolis Zivulskas

08/18/2022, 6:36 AM
Super clear and I hope will be useful 🙂
Vadim (Infracost)

Vadim (Infracost)

08/18/2022, 7:26 AM
Thanks! 🙂