Skip to content

Commit 3a13ff1

Browse files
committed
Add pricing to host module
Signed-off-by: Webster Mudge <[email protected]>
1 parent bea0a62 commit 3a13ff1

File tree

3 files changed

+93
-2
lines changed

3 files changed

+93
-2
lines changed

modules/hosts/examples/ex01-minimal_inputs/main.tf

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ provider "aws" {
1919
}
2020
}
2121

22+
provider "aws" {
23+
alias = "cost_calculator"
24+
region = "us-east-1"
25+
}
26+
2227
# ------- VPC -------
2328

2429
resource "aws_vpc" "ex01" {
@@ -50,7 +55,6 @@ resource "aws_vpc_dhcp_options_association" "ex01" {
5055
module "ex01_network" {
5156
source = "../../../network"
5257

53-
region = var.region
5458
prefix = var.prefix
5559
vpc_id = aws_vpc.ex01.id
5660

@@ -79,6 +83,10 @@ resource "aws_vpc_security_group_ingress_rule" "ssh" {
7983
module "ex01_hosts" {
8084
source = "../.."
8185
depends_on = [aws_key_pair.ex01, data.aws_ami.ex01]
86+
providers = {
87+
aws = aws
88+
aws.pricing_calculator = aws.cost_calculator
89+
}
8290

8391
name = "${var.prefix}-host"
8492
quantity = 2

modules/hosts/main.tf

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,29 @@ terraform {
1818
aws = {
1919
source = "hashicorp/aws",
2020
version = ">= 4.60.0",
21+
configuration_aliases = [ aws.pricing_calculator ]
2122
}
2223
}
2324
}
2425

26+
# provider "aws" {
27+
# alias = "cost_calculator"
28+
# region = "us-east-1"
29+
# }
30+
31+
locals {
32+
pricing_os_map = {
33+
"Red Hat Enterprise Linux" = "RHEL"
34+
"Ubuntu" = "Linux"
35+
"Linux/UNIX" = "Linux"
36+
# "Windows" = "Windows"
37+
}
38+
pricing_arch_map = {
39+
"x86_64" = "64-bit"
40+
"arm64" = "ARM64"
41+
}
42+
}
43+
2544
data "aws_ami" "pvc_base" {
2645
filter {
2746
name = "image-id"
@@ -55,3 +74,51 @@ resource "aws_instance" "pvc_base" {
5574
Name = var.quantity == 0 ? var.name : format("%s-%02d", var.name, count.index + var.offset + 1)
5675
})
5776
}
77+
78+
data "aws_pricing_product" "pvc_base" {
79+
for_each = { for idx, instance in aws_instance.pvc_base : idx => instance }
80+
81+
provider = aws.pricing_calculator
82+
83+
service_code = "AmazonEC2"
84+
85+
filters {
86+
field = "instanceType"
87+
value = each.value.instance_type
88+
}
89+
90+
filters {
91+
field = "regionCode"
92+
value = each.value.region
93+
}
94+
95+
filters {
96+
field = "operatingSystem"
97+
value = local.pricing_os_map[data.aws_ami.pvc_base.platform_details]
98+
}
99+
100+
# filters {
101+
# field = "processorArchitecture"
102+
# value = local.pricing_arch_map[data.aws_ami.pvc_base.architecture]
103+
# }
104+
105+
filters {
106+
field = "marketoption"
107+
value = "OnDemand" # TODO Review marketoption as a variable to the module
108+
}
109+
110+
filters {
111+
field = "preInstalledSw"
112+
value = "NA"
113+
}
114+
115+
filters {
116+
field = "tenancy"
117+
value = "Shared"
118+
}
119+
120+
filters {
121+
field = "capacitystatus"
122+
value = "Used"
123+
}
124+
}

modules/hosts/outputs.tf

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,20 @@ output "hosts" {
2020
output "storage_volumes" {
2121
value = local.attached_volumes_by_instance
2222
description = "Additional Storage Volumes"
23-
}
23+
}
24+
25+
locals {
26+
pvc_base_pricing_data = values(
27+
values(
28+
jsondecode(values(data.aws_pricing_product.pvc_base)[0].result).terms.OnDemand
29+
)[0].priceDimensions
30+
)[0]
31+
}
32+
33+
output "pricing" {
34+
description = "The hourly cost and details for each instance of this module."
35+
value = {
36+
price_per_hour = local.pvc_base_pricing_data.pricePerUnit.USD
37+
price_description = local.pvc_base_pricing_data.description
38+
}
39+
}

0 commit comments

Comments
 (0)