This message was deleted.
# general
b
This message was deleted.
πŸ‘‹ 4
e
We are trying to integrate Infracost with our terraform codes deployed through Github actions. When we are sending message to slack ,we are getting
invalid_blocks
error.
It seems Infracost is not able to truncate message within 3000 Char
May be we can take this up tomorrow.
This is the github workflow step.
l
Interesting… can you add a
wc -c /tmp/infracost_combined.json
after the
infracost output
line so we can see the length?
We do have logic for truncating to 3000 (https://github.com/infracost/infracost/blob/master/internal/output/slack_message.go#L134), so maybe this isn’t working as expected
s
@little-author-61621 I don't understand the truncateMiddle(). https://github.com/infracost/infracost/blob/8d16993d47c574740e7a7cff79bce2880805ccb0/internal/output/format.go#L134 You do all the length calculation and slice the message, append the fill text and at the end you again append the rest of the original message?! Wouldn't that exceed the maxLen again?
e
@little-author-61621 I will share the output for
wc -c
but as I am sending "/tmp/infracost_combined.json" after doing further formatting. Shouldn't I do character count for slack-message.json?
l
@enough-hair-29012 you’re right, sorry, yeah the character count of slack-message.json would be good to know.
@stocky-salesmen-15167 I don’t think so. https://github.com/infracost/infracost/blob/8d16993d47c574740e7a7cff79bce2880805ccb0/internal/output/format.go#L132 this line truncates to about half the maxLen, then we add the message, then we add the last bit of the original string that is also half the maxLen. So it’s stripping out the middle chunk of the original string.
s
Hmmm … that’s also what I’m seeing, so you β€œinject” the fill text in the middle of the original string.But why … maybe that’s what I don’t get?
I also test run it now in Go Playground and I was wrong with the length, it doesnt exceed the maxlength.
e
Copy code
- name: Post to Slack channel
        run: |
          infracost output --path=/tmp/infracost_combined.json  --format slack-message --out-file slack-message.json

          #Count the number of characters in slack-message.json
          wc -c slack-message.json
          
          SLACK_WEBHOOK_URL="<removed-value>"

          #Output slack-message.json on screen
          echo "-------Contents of slack-message.json------------------"
          cat slack-message.json
          
          # Skip posting to Slack if there's no cost change
          cost_change=$(cat /tmp/infracost_combined.json | jq -r "(.diffTotalMonthlyCost // 0) | tonumber")
          if [ "$cost_change" = "0" ]; then
            echo "Not posting to Slack since cost change is zero"
            exit 0
          fi

          #Post to Slack
          curl -X POST -H "Content-type: application/json" -d "@slack-message.json" $SLACK_WEBHOOK_URL
This is the output
it has 3970 char
l
@enough-hair-29012 are you able paste me the entire slack message so I can test it?
But why … maybe that’s what I don’t get?
@stocky-salesmen-15167 we probably need a better way of doing this in the future, but the start and end of the output tend to include the most important information, so we thought truncating in the middle made the most sense.
πŸ‘ 1
e
Copy code
{
  "attachments": [
    {
      "color": "#dcd8e1",
      "blocks": [
        {
          "type": "section",
          "text": {
            "type": "mrkdwn",
            "text": "*Infracost output*\n```Project: flysas-tech/cdx-terraform-infra/hub/prod/plan_hub-prod.json\n\n~ module.hub.azurerm_firewall.hub\n  -3,553 kr (12,434 kr β†’ 8,882 kr)\n\n    ~ Deployment (Premium β†’ Standard)\n      -3,553 kr (12,434 kr β†’ 8,882 kr)\n\nMonthly cost change for flysas-tech/cdx-terraform-infra/hub/prod/plan_hub-prod.json\nAmount:  -3,553 kr (SEK) (12,557 kr β†’ 9,005 kr)\nPercent: -28%\n\n──────────────────────────────────\nProject: flysas-tech/cdx-terraform-infra/hub/test/plan_hub-test.json\n\n~ module.hub.azurerm_firewall.hub\n  -3,553 kr (12,434 kr β†’ 8,882 kr)\n\n    ~ Deployment (Premium β†’ Standard)\n      -3,553 kr (12,434 kr β†’ 8,882 kr)\n\nMonthly cost change for flysas-tech/cdx-terraform-infra/hub/test/plan_hub-test.json\nAmount:  -3,553 kr (SEK) (12,557 kr β†’ 9,005 kr)\nPercent: -28%\n\n──────────────────────────────────\nProject: flysas-tech/cdx-terraform-infra/management-subscription/prod/plan_management-subscription-prod.json\n\n~ module.management_subscription.azurerm_log_analytics_workspace.management-log\n  Monthly cost depends on usage\n\n    + Log data retention\n      Monthly cost depends on usage\n        +1.27 kr per GB\n\nMonthly cost change for flysas-tech/cdx-terraform-infra/management-subscription/prod/plan_management-subscription-prod.json\nAmount:  0.00 kr (SEK) (0.00 kr β†’ 0.00 kr)\n\n──────────────────────────────────\nProject: flysas-tech/cdx-terraform-infra/management-subscription/test/plan_management-subscription-test.json\n\n~ module.management_subscription.azurerm_log_analytics_workspace.management-log\n  Monthly cost depends on usage\n\n    + Log data retention\n      Monthly cost depends on usage\n        +1.27 kr per GB\n\nMonthly cost change for flysas-tech/cdx-terraform-infra/management-subscription/test/plan_management-subscription-test.json\nAmount:  0.00 kr (SEK) (0.00 kr β†’ 0.00 kr)\n\n──────────────────────────────────\n\nThe following projects have no cost estimate changes: flysas-tech/cdx-terraform-infra/spokes/prod/plan_spokes-prod.json, flysas-tech/cdx-terraform-infra/spokes/test/plan_spokes-test.json\nRun infracost breakdown to see their full breakdown.\n\n──────────────────────────────────\nKey: ~ changed, + added, - removed\n\n156 cloud resources were detected, rerun with --show-skipped to see details:\nβˆ™ 46 were estimated, 8 include usage-based costs, see <https://infracost.io/usage-file>\nβˆ™ 42 weren't estimated, report them in <https://github.com/infracost/infracost>\nβˆ™ 68 were free```"
          }
        }
      ]
    }
  ],
  "blocks": [
    {
      "type": "section",
      "text": {
        "type": "mrkdwn",
        "text": "πŸ’° Infracost estimate: *monthly cost will decrease by 7,105 kr (-28%) πŸ“‰*"
      }
    },
    {
      "type": "divider"
    },
    {
      "type": "section",
      "fields": [
        {
          "type": "plain_text",
          "text": "Project"
        },
        {
          "type": "plain_text",
          "text": "Diff"
        },
        {
          "type": "plain_text",
          "text": "flysas-tech/cdx-terr.../plan_hub-prod.json"
        },
        {
          "type": "plain_text",
          "text": "-3,553 kr (12,557 kr β†’ 9,005 kr)"
        },
        {
          "type": "plain_text",
          "text": "flysas-tech/cdx-terr.../plan_hub-test.json"
        },
        {
          "type": "plain_text",
          "text": "-3,553 kr (12,557 kr β†’ 9,005 kr)"
        },
        {
          "type": "plain_text",
          "text": "flysas-tech/cdx-terr...scription-prod.json"
        },
        {
          "type": "plain_text",
          "text": "0.00 kr (0.00 kr β†’ 0.00 kr)"
        },
        {
          "type": "plain_text",
          "text": "flysas-tech/cdx-terr...scription-test.json"
        },
        {
          "type": "plain_text",
          "text": "0.00 kr (0.00 kr β†’ 0.00 kr)"
        },
        {
          "type": "plain_text",
          "text": "All projects"
        },
        {
          "type": "plain_text",
          "text": "-7,105 kr (25,115 kr β†’ 18,009 kr)"
        }
      ]
    },
    {
      "type": "section",
      "text": {
        "type": "mrkdwn",
        "text": "2 projects have no cost estimate changes."
      }
    }
  ]
}
l
Okay, the issue seems to be that Slack messages have a maximum of 5 rows in a section, so we can only show the β€œProject”/β€œDiff” headers plus 4 projects at the top. I’m not sure what the best fix for this would be. We could truncate that list. What do you think?
A workaround at the moment until we get a fix out is to run something like this:
Copy code
# Make sure we don't exceed the maximum Slack fields
cat slack-message.json | jq 'del(.blocks[2].fields[10:])' > slack-message-truncated.json
...

curl -X POST -H "Content-type: application/json" -d "@slack-message-truncated.json" $SLACK_WEBHOOK_URL
e
Sure ,we would work with the workaround.
Again,Thanks for all the helpπŸ™
Just one more thing, we have multiple terraform projects in a single repository. So,I don't thing merging the json for all the project would be a good idea from Slack point of view.
Can we split the slack messages and send multiple slack message in continuation, so we don't truncate important info?
l
Yeah that’s a good shout, we’d have to have a loop in the bash or something to do this. Another option would be adding a link in the Slack message instead so it shows you the full breakdown in your browser - similar to what we do with Share links: https://www.infracost.io/docs/features/share_links/
Just one more thing, we have multiple terraform projects in a single repository. So,I don’t thing merging the json for all the project would be a good idea from Slack point of view.
Are you wanting a separate slack message for each project?
@enough-hair-29012 also I’ve pushed a fix for this to chunk the projects (https://github.com/infracost/infracost/pull/1462). This should be out in our next release (v0.9.20) today or tomorrow. If you’re wanting a separate Slack message per project let me know, and I’ll try to come up with an example of how to do it for your existing action.
e
@little-author-61621 No I don't have requirements to have separate slack message per project. I am just saying we don't truncate important information when we have multiple projects.
l
@enough-hair-29012 could enabling the share links and then including this link to the full results in the slack message help?
e
After the fix (https://github.com/infracost/infracost/pull/1462), issue seem to be fixed. We will have a look into enabling the share links if that can be used.
l
Great, thanks @enough-hair-29012 πŸ™Œ. With share links we will need to make some changes to get them to work with the the Slack message properly, so let me know once you’ve tried it out and we can look at that.