Skip to content

Commit

Permalink
feat: infracost integration (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
leg100 authored Aug 21, 2024
1 parent a278994 commit b06646b
Show file tree
Hide file tree
Showing 98 changed files with 1,744 additions and 496 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ jobs:
run: make vet
- name: Setup provider mirror for tests
run: ./hacks/setup_mirror.sh
- name: Install terragrunt for tests
run: make install-terragrunt
- name: Install test dependencies
run: make install-terragrunt install-infracost
- name: Tests
run: make test
release-please:
Expand Down
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,10 @@ install-terragrunt:
mkdir -p ~/.local/bin
curl -L https://github.com/gruntwork-io/terragrunt/releases/download/v0.60.0/terragrunt_linux_amd64 -o ~/.local/bin/terragrunt
chmod +x ~/.local/bin/terragrunt

.PHONY: install-infracost
install-infracost:
mkdir -p ~/.local/bin
curl -L https://github.com/infracost/infracost/releases/download/v0.10.38/infracost-linux-amd64.tar.gz | tar -zxf -
mv infracost-linux-amd64 ~/.local/bin/infracost
chmod +x ~/.local/bin/infracost
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ A terminal user interface for terraform power users.
* Supports terraform, tofu and terragrunt
* Supports terragrunt dependencies
* Supports workspaces
* Calculate costs using infracost
* Automatically loads workspace variable files
* Backend agnostic (s3, cloud, etc)

Expand Down Expand Up @@ -128,6 +129,7 @@ Press `w` to go to the workspaces page.
|`a`|Run `terraform apply`|✓|
|`d`|Run `terraform apply -destroy`|✓|
|`C`|Run `terraform workspace select`|✗|
|`$`|Run `infracost breakdown`|✓|

### State

Expand Down Expand Up @@ -291,6 +293,18 @@ A task can be canceled at any stage. If it is `running` then the current terrafo

When a workspace is loaded into Pug for the first time, a task is created to invoke `terraform state pull`, which retrieves workspace's state, and then the state is loaded into Pug. The task is also triggered after any task that alters the state, such as an apply or moving a resource in the state.

## Infracost integration

NOTE: Requires `infracost` to be installed on your machine, along with configured API key.

Pug integrates with infracost to provide cost estimation. Select workspaces on the workspace page and press `$` to run calculate their costs:

![Infracost output screenshot](./demo/infracost_output.png)

Once the task has finished, the costs are visible on the workspaces page:

![Worksapces with costs screenshot](./demo/workspaces_with_cost.png)

## Tofu support

To use tofu, set `--program=tofu`. Ensure it is installed first.
Expand Down
Binary file modified demo/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions demo/do_cost_money/modules/a/dev.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
instance_type = "n1-standard-96"
59 changes: 59 additions & 0 deletions demo/do_cost_money/modules/a/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
terraform {
backend "local" {}
}

# Configure the AWS Provider
provider "google" {
region = "us-east-1"
}

resource "google_compute_instance" "default" {
name = "my-instance"
machine_type = var.instance_type
zone = "us-central1-a"

tags = ["foo", "bar"]

boot_disk {
initialize_params {
image = "debian-cloud/debian-11"
labels = {
my_label = "value"
}
}
}

// Local SSD disk
scratch_disk {
interface = "NVME"
}

network_interface {
network = "default"

access_config {
// Ephemeral public IP
}
}

metadata = {
foo = "bar"
}

metadata_startup_script = "echo hi > /test.txt"

service_account {
# Google recommends custom service accounts that have cloud-platform scope and permissions granted via IAM Roles.
email = google_service_account.default.email
scopes = ["cloud-platform"]
}
}

resource "google_service_account" "default" {
account_id = "my-custom-sa"
display_name = "Custom SA for VM Instance"
}

variable "instance_type" {
default = "n2-standard-2"
}
59 changes: 59 additions & 0 deletions demo/do_cost_money/modules/b/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
terraform {
backend "local" {}
}

# Configure the AWS Provider
provider "google" {
region = "us-east-1"
}

resource "google_compute_instance" "default" {
name = "my-instance"
machine_type = var.instance_type
zone = "us-central1-a"

tags = ["foo", "bar"]

boot_disk {
initialize_params {
image = "debian-cloud/debian-11"
labels = {
my_label = "value"
}
}
}

// Local SSD disk
scratch_disk {
interface = "NVME"
}

network_interface {
network = "default"

access_config {
// Ephemeral public IP
}
}

metadata = {
foo = "bar"
}

metadata_startup_script = "echo hi > /test.txt"

service_account {
# Google recommends custom service accounts that have cloud-platform scope and permissions granted via IAM Roles.
email = google_service_account.default.email
scopes = ["cloud-platform"]
}
}

resource "google_service_account" "default" {
account_id = "my-custom-sa"
display_name = "Custom SA for VM Instance"
}

variable "instance_type" {
default = "n2-standard-2"
}
59 changes: 59 additions & 0 deletions demo/do_cost_money/modules/c/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
terraform {
backend "local" {}
}

# Configure the AWS Provider
provider "google" {
region = "us-east-1"
}

resource "google_compute_instance" "default" {
name = "my-instance"
machine_type = var.instance_type
zone = "us-central1-a"

tags = ["foo", "bar"]

boot_disk {
initialize_params {
image = "debian-cloud/debian-11"
labels = {
my_label = "value"
}
}
}

// Local SSD disk
scratch_disk {
interface = "NVME"
}

network_interface {
network = "default"

access_config {
// Ephemeral public IP
}
}

metadata = {
foo = "bar"
}

metadata_startup_script = "echo hi > /test.txt"

service_account {
# Google recommends custom service accounts that have cloud-platform scope and permissions granted via IAM Roles.
email = google_service_account.default.email
scopes = ["cloud-platform"]
}
}

resource "google_service_account" "default" {
account_id = "my-custom-sa"
display_name = "Custom SA for VM Instance"
}

variable "instance_type" {
default = "n2-standard-2"
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file modified demo/filter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demo/infracost_output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified demo/logs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified demo/modules.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 0 additions & 17 deletions demo/modules/a/.terraform.lock.hcl

This file was deleted.

16 changes: 0 additions & 16 deletions demo/modules/b/.terraform.lock.hcl

This file was deleted.

16 changes: 0 additions & 16 deletions demo/modules/c/.terraform.lock.hcl

This file was deleted.

16 changes: 0 additions & 16 deletions demo/modules/d/.terraform.lock.hcl

This file was deleted.

16 changes: 0 additions & 16 deletions demo/modules/e/.terraform.lock.hcl

This file was deleted.

16 changes: 0 additions & 16 deletions demo/modules/f/.terraform.lock.hcl

This file was deleted.

16 changes: 0 additions & 16 deletions demo/modules/g/.terraform.lock.hcl

This file was deleted.

16 changes: 0 additions & 16 deletions demo/modules/h/.terraform.lock.hcl

This file was deleted.

16 changes: 0 additions & 16 deletions demo/modules/i/.terraform.lock.hcl

This file was deleted.

Loading

0 comments on commit b06646b

Please sign in to comment.