File tree Expand file tree Collapse file tree 3 files changed +93
-2
lines changed
examples/ex01-minimal_inputs Expand file tree Collapse file tree 3 files changed +93
-2
lines changed Original file line number Diff line number Diff line change @@ -19,6 +19,11 @@ provider "aws" {
19
19
}
20
20
}
21
21
22
+ provider "aws" {
23
+ alias = " cost_calculator"
24
+ region = " us-east-1"
25
+ }
26
+
22
27
# ------- VPC -------
23
28
24
29
resource "aws_vpc" "ex01" {
@@ -50,7 +55,6 @@ resource "aws_vpc_dhcp_options_association" "ex01" {
50
55
module "ex01_network" {
51
56
source = " ../../../network"
52
57
53
- region = var. region
54
58
prefix = var. prefix
55
59
vpc_id = aws_vpc. ex01 . id
56
60
@@ -79,6 +83,10 @@ resource "aws_vpc_security_group_ingress_rule" "ssh" {
79
83
module "ex01_hosts" {
80
84
source = " ../.."
81
85
depends_on = [aws_key_pair . ex01 , data . aws_ami . ex01 ]
86
+ providers = {
87
+ aws = aws
88
+ aws.pricing_calculator = aws.cost_calculator
89
+ }
82
90
83
91
name = " ${ var . prefix } -host"
84
92
quantity = 2
Original file line number Diff line number Diff line change @@ -18,10 +18,29 @@ terraform {
18
18
aws = {
19
19
source = " hashicorp/aws" ,
20
20
version = " >= 4.60.0" ,
21
+ configuration_aliases = [ aws.pricing_calculator ]
21
22
}
22
23
}
23
24
}
24
25
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
+
25
44
data "aws_ami" "pvc_base" {
26
45
filter {
27
46
name = " image-id"
@@ -55,3 +74,51 @@ resource "aws_instance" "pvc_base" {
55
74
Name = var.quantity == 0 ? var.name : format (" %s-%02d" , var. name , count. index + var. offset + 1 )
56
75
})
57
76
}
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
+ }
Original file line number Diff line number Diff line change @@ -20,4 +20,20 @@ output "hosts" {
20
20
output "storage_volumes" {
21
21
value = local. attached_volumes_by_instance
22
22
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
+ }
You can’t perform that action at this time.
0 commit comments