https://terramate.io/docs/cli/getting-started/. This guide provides a comprehensive overview of setting up Terramate for your infrastructure projects.
git clone https://github.com/terramate-io/terramate-infracost.git
Just remember to install Terramate CLI and Terraform to follow along with this tutorial.
If you've cloned my repository, you'll discover three different stacks: first
, second
, and third
. Each stack contains a different resource configuration:
You can list all stacks in a terramate project with the
terramate list
command.
To witness Terramate in action, execute the following commands:
terramate run terraform init
terramate run terraform plan
These commands will initiate the init and plan processes for each stack independently, highlighting Terramate's flexibility and efficiency in managing infrastructure deployments.
Now, how can we run Infracost? Given that we have multiple stacks, we need to run Infracost on each one.
First, we need to install Infracost. Here are the two steps you need to do :
When you're ready, run the following command to ensure everything is set up properly:
terramate run infracost --version
Yep, that's right! The Terramate CLI can run any command, meaning we can run Infracost in each stack separately.
Before running Infracost, we need to add another step: cloud consumption estimations. If you have on-demand resources, you'll need to estimate how much these resources are going to be used.
To accomplish this, we create a config file. I prefer using one file in each stack, with each file containing only the resources needed in that stack.
As an example, let's create a new file called infracost-usage-medium.yml
in one stack, which specifies the usage for our SNS topic:
version: 0.1
resource_type_default_usage:
aws_sns_topic:
monthly_requests: 10000 # Monthly requests to SNS.
email_subscriptions: 1000 # Number of Email/Email-JSON subscriptions
With everything in place, all that's left is to run one command:
terramate run infracost breakdown --path . --usage-file infracost-usage-medium.yml
Alright, that worked flawlessly. But what if we want to calculate the costs of a single stack? In that case, Terramate has us covered by allowing us to specify the stack we want the command to run on:
terramate run -C stacks/first infracost breakdown --path . --usage-file infracost-usage-medium.yml
And here you have it, folks! Now you have learned how we can leverage Terramate and Infracost to calculate the cost of each stack.
Let's tackle another challenge before we conclude this article, shall we?
First, we need to save our INFRACOST_API_KEY
as a secret in GitHub Actions. For the Infracost action, we are going to use this action: infracost/actions/setup@v2.
To complete the puzzle, we need to write the GitHub action workflow. Essentially, we need to run Terraform and Infracost commands for each stack. Eventually, we end up with these commands:
- name: Create Terraform plan on changed stacks
run: |
terramate run --changed -- terraform init
terramate run --changed -- terraform validate
terramate run --changed -- terraform plan -out ./out.tfplan
- name: Generate Infracost cost estimate baseline
run: |
terramate run --changed -- infracost breakdown \
--path ./out.tfplan \
--format=json \
--usage-file infracost-usage-medium.yml \
--out-file=./infracost-base.json
- name: Post Infracost comment
run: |
terramate run --changed -- infracost comment github \
--path=./infracost-base.json \
--repo=$GITHUB_REPOSITORY \
--github-token=${{github.token}} \
--pull-request=${{github.event.pull_request.number}} \
--behavior=new
And there you have it, your GitHub Actions Pipeline is up and running.
Leveraging Terramate with Infracost to see how much we are going to be billed for each stack is truly something you're going to love. What really stands out is the ability to see the costs of each stack separately.
We've delved into how Terramate works and even explored how to use Infracost in a GitHub Actions Pipeline to comment on a pull request. Now, your pull requests will really stand out .
Check out our example repository https://github.com/terramate-io/terramate-infracost
I highly encourage you to join Terramate's Discord Community if you have questions or need help with Terramate. They have an active and supportive community ready to assist you!