Question about using terraform to compute a cost estimation.

351 views
Skip to first unread message

Rémy Léone

unread,
Jul 17, 2018, 4:30:46 AM7/17/18
to Terraform
Hello,

I'm a newcomer in the terraform community and I wonder about the following point:

Could it be possible to use terraform to have an estimation about how a certain plan is going to cost?
For instance, if I want to instantiate a certain resource, terraform if ordered with the right command/flags will fetch a billing API and provide the user with an estimation/exact cost of this deployment.

I think it could be useful to give to the user a rough estimation of the cost of a particular deployment.

What do you think about this idea? 

Best regards

Rémy

Steven Nemetz

unread,
Jul 17, 2018, 11:52:23 PM7/17/18
to Terraform
I can't think of any easy or direct way of doing this inside Terraform (but it is a nice idea).
Currently to do this, you'd need to take the output (either text or plan file) from plan and have another program process it to generate the estimate
But be aware the plan is not 100% accurate, specially from a cost standpoint, and the cost to deploy is very different from the cost to run.

Steven

Anshu Prateek

unread,
Jul 17, 2018, 11:59:17 PM7/17/18
to terrafo...@googlegroups.com
We have added this logic to our modules. Based on the ec2 instances/esb type and size, we calculate and update the cost tag. The base price is stored as a map variable. Similarly cost of all related variables could be stored and used.

AWS doesn’t provide an API for the cost listing, so it’s partly page scraping etc. also, this presently will only give on demand pricing estimates.

Sent from my phone
--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/7eadebdd-92d1-442f-b8d7-b57b63b73f11%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

⁞ Fernando Miguel

unread,
Jul 18, 2018, 1:48:11 AM7/18/18
to terrafo...@googlegroups.com
The api for pricing has been there for years 

Anshu Prateek

unread,
Aug 8, 2018, 8:04:03 AM8/8/18
to terrafo...@googlegroups.com
Thanks, somehow dont think I came across it earlier!
Now I am wondering if I scrapped the page or used the API?!


For more options, visit https://groups.google.com/d/optout.
--
regards
Anshu Prateek
+91.991.610.2967

ja...@fpcomplete.com

unread,
Aug 8, 2018, 9:46:39 AM8/8/18
to Terraform


On Tuesday, July 17, 2018 at 11:59:17 PM UTC-4, Anshu Prateek wrote:
We have added this logic to our modules. Based on the ec2 instances/esb type and size, we calculate and update the cost tag. The base price is stored as a map variable. Similarly cost of all related variables could be stored and used.

Sounds awesome, do you have an example in a public module?

Anshu Prateek

unread,
Aug 8, 2018, 11:12:08 AM8/8/18
to terrafo...@googlegroups.com
Snippet -

map("tag_monthly_cost", "${ceil(1.0 * lookup(var.ec2_cost,var.instance_type) * 24.0 * 30.0)}")

variable "ec2_cost" {
  type = "map"

  default = {
    t2.nano     = "0.0075"
    t2.micro    = "0.015"
..
}
}

For EBS:
 ( we do couple of volume mounts for LVM, hence the couple of lookups )

map("tag_monthly_cost", "${1.0 * lookup(merge(var.default_ebs_vol_sizes,var.ebs_vol_sizes),
      var.tag_disk_location[var.extra_ebs_volumes[count.index % length(var.extra_ebs_volumes)]]) * lookup(var.ebs_cost,
      lookup(var.extra_ebs_volume_type,
        lookup(var.tag_disk_location,var.extra_ebs_volumes[count.index % length(var.extra_ebs_volumes)]),"${var.default_ebs_volume_type}"))}")



--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Priyanka

unread,
Jun 28, 2019, 10:05:43 AM6/28/19
to Terraform
Hi Anshu,
Could you guide me to a github page or something where you have executed this module? 

David Jeche

unread,
Jul 1, 2019, 4:34:55 AM7/1/19
to Terraform
I do not believe that there is anything available to do this. I think it would an interesting tool though. You can compile JSON and then perform calculations on the resources to determine the cost. 


They have a decent system for this. It is not an IAC method but enables you to generate terraform files. Amazon has got a cost API which you can then plug in and use to obtain cost information from resources to determine the cost. 

Kurkure, Priyanka

unread,
Jul 1, 2019, 10:37:16 AM7/1/19
to terrafo...@googlegroups.com
The JSON file that is compiled, is it of the instances we have created ? Because i would like to compile the aws pricing api JSON file.

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.

David Jeche

unread,
Jul 1, 2019, 10:44:29 AM7/1/19
to Terraform
You would have to query the terraform state file. Then use the information from the terraform state file to get the relevant information from the AWS pricing API.

It would not be the easiest thing to write but it will be the best way. The reason I say not the easiest thing is that the terraform state file might not be the easiest to parse. You will need to know what the AWS cost API expects and make sure you can get the information from the state file. 


On Monday, 1 July 2019 15:37:16 UTC+1, Priyanka wrote:
The JSON file that is compiled, is it of the instances we have created ? Because i would like to compile the aws pricing api JSON file.

On Mon, Jul 1, 2019 at 4:34 AM David Jeche <david...@redmatter.com> wrote:
I do not believe that there is anything available to do this. I think it would an interesting tool though. You can compile JSON and then perform calculations on the resources to determine the cost. 


They have a decent system for this. It is not an IAC method but enables you to generate terraform files. Amazon has got a cost API which you can then plug in and use to obtain cost information from resources to determine the cost. 


On Friday, 28 June 2019 15:05:43 UTC+1, Priyanka wrote:
Hi Anshu,
Could you guide me to a github page or something where you have executed this module? 

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terrafo...@googlegroups.com.

Kurkure, Priyanka

unread,
Jul 1, 2019, 11:11:15 AM7/1/19
to terrafo...@googlegroups.com
Hi David,
Would you be interested in finding out what i have obtained till now and then maybe we could figure out a way to get this done? That is of course if you are available and interested in this.
I have the query to obtain the terraform state file for an ec2 instance. Also, i did get an out put stating the prices but it returned more than one element  stating an error that my query isn't precise. I'll be happy to share the snapshots of it for your better reference. 
As long as this goes "what the AWS cost API expects and make sure you can get the information from the state file" . I am inclined to think that its either the "id" or just the instance type.
Now, i am stuck at two things: 1) How do i make my query more precise. 2) Parsing the tfstate file / Finding out what the AWS cost API expects and get the information from the state file.

Hope to work with you on this.

Regards,
Priyanka



To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/0d1a1da9-da95-4d87-86f7-07a74631c7ee%40googlegroups.com.

David Jeche

unread,
Jul 2, 2019, 11:24:53 AM7/2/19
to Terraform
I would be interested in taking a look at what you are doing. It all depends on what you are using to parse the JSON. The AWS cost explorer API can be used by exploring the documentation for more information it should be documented somewhere. 

It all depends on the language that you are using to parse the state file. 

Kurkure, Priyanka

unread,
Jul 2, 2019, 11:57:34 AM7/2/19
to terrafo...@googlegroups.com
Hi David,
Glad to know that you are interested. So, i have tried one another primitive method but i am stuck at one point, just check if you can work it through.
1: Made a file called ec2pricing.tf  (Attached below)
2: Then ran terraform init and terraform plan (Provide the access key and private key of a user you created)
3: It gave me the following output stating the query isn't percise and have been since working on making it precise.

Also, i sent you the code which isn't precise but does give us the price of the instance type without us having to create an actual instance.
The excel file is indicating all the values after applying the same filters as that of the code.
Out of 78 rows only 21 are remaining.  I want a single row .

Let me know if you have any questions. And do give it a try.

To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/7cf11526-4a8c-4342-9f18-1d29ea437f15%40googlegroups.com.
EC2 COST 1.docx
cmd output.PNG
snippet of excel.PNG

David Jeche

unread,
Jul 3, 2019, 4:10:27 AM7/3/19
to Terraform
Correct me if I am wrong but you have started developing a module for terraform that you can use on to calculate the cost?

I think in finding the correct you need to ensure you have the correct key or filter. if it is too ambiguous then you may get issues. 

⁞ Fernando Miguel

unread,
Jul 3, 2019, 4:12:20 AM7/3/19
to terrafo...@googlegroups.com
AWS provides api endpoints for cost of every service. 
It would probably be better to consume those directly 

--
Fernando Miguel

To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/eb51146a-1b19-4d9a-85eb-054dcc0e3440%40googlegroups.com.

Kurkure, Priyanka

unread,
Jul 3, 2019, 9:53:23 AM7/3/19
to terrafo...@googlegroups.com
Yes David that is correct. Also, other wise i am working on parsing the aws templates to get the appropriate information like the instance type , volume size and then checking for its price by making a call to the aws api. Now the templates are stored in jenkins , so will have to formulate a code in python or any other language and that file will be like a pre-step in finding the cost of resources that , that template will make if we decide to go through with it.
Let me know if you guys get the idea?

Kurkure, Priyanka

unread,
Jul 3, 2019, 11:06:21 AM7/3/19
to terrafo...@googlegroups.com
It is somewhat similar to this  https://www.mobiquityinc.com/thought-leadership/estimating-aws-infrastructure-cost-from-terraform-templates
Has, any one of you tried to implement this?

David Jeche

unread,
Jul 8, 2019, 9:53:49 AM7/8/19
to Terraform
This looks cool, I do not know much about writing the terraform providers.

Iw ill investigate that and let you know what I think or what I find regards, 

If you have repo or something please let me know so I can catch up to what your doing.


On Wednesday, 3 July 2019 16:06:21 UTC+1, Priyanka wrote:
It is somewhat similar to this  https://www.mobiquityinc.com/thought-leadership/estimating-aws-infrastructure-cost-from-terraform-templates
Has, any one of you tried to implement this?


--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terrafo...@googlegroups.com.

Kurkure, Priyanka

unread,
Jul 8, 2019, 9:57:43 AM7/8/19
to terrafo...@googlegroups.com, david...@redmatter.com
So, it doesn't neccessarily have to be in terraform. A template or tool in any other language will also do, that will fetch the required details from the existing terraform templates in JENKINS and then retrieve the cost estimation for it through the aws cost api

To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/b652a4c8-01ff-4913-8af3-4ed87c8c1c86%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages