Hi there :slightly_smiling_face: I am working on a...
# contributors
w
Hi there πŸ™‚ I am working on adding Athena query support to the Infracost CLI. The thing is I am working for a company that won't be able to approve contributing back to the open source project (potentially for months/years) but still want me to add some Athena services as a POC. I've followed the documentation and I can't get the athena_test.golden to show what is shows in the documentation here: https://github.com/infracost/infracost/blob/master/contributing/add_new_resource_guide.md#run-tests
Copy code
Name  Monthly Qty  Unit  Monthly Cost

 OVERALL TOTAL                   $0.00
──────────────────────────────────
1 cloud resource was detected:
βˆ™ 1 was estimated, 1 includes usage-based costs, see <https://infracost.io/usage-file>
No matter what I do to the cost component when I run my tests the comparison and golden look like this:
Copy code
Name  Monthly Qty  Unit  Monthly Cost   
                                         
 OVERALL TOTAL                  $0.00 

*Usage costs can be estimated by updating Infracost Cloud settings, see docs for other options.

──────────────────────────────────
1 cloud resource was detected:
βˆ™ 0 were estimated
βˆ™ 1 is not supported yet, see <https://infracost.io/requested-resources>:
  βˆ™ 1 x aws_athena_named_query

┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━━┓
┃ Project                                            ┃ Baseline cost ┃ Usage cost* ┃ Total cost ┃
┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━╋━━━━━━━━━━━━┫
┃ main                                               ┃         $0.00 ┃           - ┃      $0.00 ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━┻━━━━━━━━━━━━┛
It always says it is unsupported. Is there an approve list somewhere in source that dictates what services are/are not supported? I tried manipulating the registry.go, but that doesn't seem to do anything. I know a Git Issue is requested to be created, but I a not allowed to contribute back, at least right now. Thank you πŸ™‚
c
internal/providers/terraform/aws/registry.go
would be the right place. What change did you make there?
w
Thank you for the quick response! I added this:
Copy code
package aws

import "github.com/infracost/infracost/internal/schema"

var ResourceRegistry []*schema.RegistryItem = []*schema.RegistryItem{
	getAthenaRegistryItem(),
And this for good measure:
Copy code
// FreeResources grouped alphabetically
var FreeResources = []string{
	// AWS Athena
	"aws_athena",
I also tried adding it here before and it didn't help:
Copy code
var UsageOnlyResources = []string{
	"aws_data_transfer",
}
I think these are okay too but I'll add below because of the ref to
getAthenaRegistryItem()
My /terraform/aws/testdata/athena_test.go looks like this:
Copy code
package aws_test

import (
    "testing"

    "github.com/infracost/infracost/internal/providers/terraform/tftest"
)

func TestAthena(t *testing.T) {
    t.Parallel()

    if testing.Short() {
        t.Skip("skipping test in short mode")
    }

	tftest.GoldenFileResourceTests(t, "athena_test")
}
And my /terraform/aws/testdata/athena.go looks like this:
Copy code
package aws

import (
	"github.com/infracost/infracost/internal/resources/aws"
	"github.com/infracost/infracost/internal/schema"
)

func getAthenaRegistryItem() *schema.RegistryItem {
	return &schema.RegistryItem{
		Name:      "aws_athena",
		CoreRFunc: newAthena,
	}
}

func newAthena(d *schema.ResourceData) schema.CoreResource {
	region := d.Get("region").String()
	return &aws.Athena{
		Address: d.Address,
		Region:  region,
	}
}
And my output:
Copy code
INFRACOST_LOG_LEVEL=warn go test -timeout 30m -ldflags="-X 'github.com/infracost/infracost/internal/version.Version=v0.10.38+8-ef324706-dirty'" ./internal/providers/terraform/aws --run TestAthena -v -update
{"level":"debug","time":"2024-07-17T11:05:07-04:00","message":"Running command: /opt/homebrew/bin/terraform init -no-color"}
{"level":"debug","binary":"terraform","time":"2024-07-17T11:05:07-04:00"}
{"level":"debug","binary":"terraform","time":"2024-07-17T11:05:07-04:00","message":"Initializing the backend..."}
{"level":"debug","binary":"terraform","time":"2024-07-17T11:05:07-04:00"}
{"level":"debug","binary":"terraform","time":"2024-07-17T11:05:07-04:00","message":"Initializing provider plugins..."}
{"level":"debug","binary":"terraform","time":"2024-07-17T11:05:07-04:00","message":"- Reusing previous version of hashicorp/aws from the dependency lock file"}
{"level":"debug","binary":"terraform","time":"2024-07-17T11:05:07-04:00","message":"- Reusing previous version of hashicorp/google from the dependency lock file"}
{"level":"debug","binary":"terraform","time":"2024-07-17T11:05:07-04:00","message":"- Reusing previous version of hashicorp/azurerm from the dependency lock file"}
{"level":"debug","binary":"terraform","time":"2024-07-17T11:05:08-04:00","message":"- Reusing previous version of hashicorp/google-beta from the dependency lock file"}
{"level":"debug","binary":"terraform","time":"2024-07-17T11:05:08-04:00","message":"- Using previously-installed hashicorp/google-beta v5.34.0"}
{"level":"debug","binary":"terraform","time":"2024-07-17T11:05:08-04:00","message":"- Using previously-installed hashicorp/aws v5.54.1"}
{"level":"debug","binary":"terraform","time":"2024-07-17T11:05:08-04:00","message":"- Using previously-installed hashicorp/google v5.34.0"}
{"level":"debug","binary":"terraform","time":"2024-07-17T11:05:08-04:00","message":"- Using previously-installed hashicorp/azurerm v3.108.0"}
{"level":"debug","binary":"terraform","time":"2024-07-17T11:05:08-04:00"}
{"level":"debug","binary":"terraform","time":"2024-07-17T11:05:08-04:00","message":"Terraform has been successfully initialized!"}
=== RUN   TestAthena
=== PAUSE TestAthena
=== CONT  TestAthena
=== RUN   TestAthena/HCL
=== RUN   TestAthena/HCL_Graph
=== RUN   TestAthena/Terraform_CLI
--- PASS: TestAthena (2.60s)
    --- PASS: TestAthena/HCL (0.02s)
    --- PASS: TestAthena/HCL_Graph (0.02s)
    --- PASS: TestAthena/Terraform_CLI (2.56s)
PASS
ok      github.com/infracost/infracost/internal/providers/terraform/aws 4.964s
c
The RegitryItem name needs to be an exact match to the tf resource type, so
Name: "aws_athena_named_query"
w
@crooked-daybreak-55253 Absolutely beautiful that worked like a charm! Awesome. Thank you so much! Okay so here is my follow up question... If i want to add more resources like:
Copy code
resource "aws_athena_workgroup" "test" {
  name = "example"

  configuration {
    result_configuration {
      encryption_configuration {
        encryption_option = "SSE_KMS"
        kms_key_arn       = aws_kms_key.test.arn
      }
    }
  }
}

resource "aws_athena_database" "hoge" {
  name   = "users"
  bucket = aws_s3_bucket.hoge.id
}
In what file to I implement the logic to handle all of them? It seems like /terraform/aws/testdata is split up by services, not resources. I also see that for the big services there is a lot of logic in various files so I guess I assumed all athena terraform resources had to have the same athena.go, and shove all the cost components and logic in there for all. Is that right? wrong? Best way to do it? I read the naming conventions page but unsure on structure. (I have no go experience too)
c
in general there is a 1-1 correspondence between file and tf resource. The individual cases in testdata often have multiple resources in a single example tf file because a lot of the time it’s not possible to write valid terraform without the supporting resources.
🦜 1
πŸ™Œ 1
❀️ 1