Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ repos:

# see https://github.com/antonbabenko/pre-commit-terraform#terraform_validate
- id: terraform_validate
exclude: examples/.*
#exclude: examples/.*
exclude: 'modules/hosts/[^/]+$'

# see https://github.com/antonbabenko/pre-commit-terraform#terraform_providers_lock
- id: terraform_providers_lock
Expand Down
10 changes: 9 additions & 1 deletion modules/hosts/examples/ex01-minimal_inputs/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ provider "aws" {
}
}

provider "aws" {
alias = "cost_calculator"
region = "us-east-1"
}

# ------- VPC -------

resource "aws_vpc" "ex01" {
Expand Down Expand Up @@ -50,7 +55,6 @@ resource "aws_vpc_dhcp_options_association" "ex01" {
module "ex01_network" {
source = "../../../network"

region = var.region
prefix = var.prefix
vpc_id = aws_vpc.ex01.id

Expand Down Expand Up @@ -79,6 +83,10 @@ resource "aws_vpc_security_group_ingress_rule" "ssh" {
module "ex01_hosts" {
source = "../.."
depends_on = [aws_key_pair.ex01, data.aws_ami.ex01]
providers = {
aws = aws
aws.pricing_calculator = aws.cost_calculator
}

name = "${var.prefix}-host"
quantity = 2
Expand Down
71 changes: 69 additions & 2 deletions modules/hosts/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,31 @@ terraform {
required_version = ">= 0.13"
required_providers {
aws = {
source = "hashicorp/aws",
version = ">= 4.60.0",
source = "hashicorp/aws",
version = ">= 4.60.0",
configuration_aliases = [aws.pricing_calculator]
}
}
}

# provider "aws" {
# alias = "cost_calculator"
# region = "us-east-1"
# }

locals {
pricing_os_map = {
"Red Hat Enterprise Linux" = "RHEL"
"Ubuntu" = "Linux"
"Linux/UNIX" = "Linux"
# "Windows" = "Windows"
}
# pricing_arch_map = {
# "x86_64" = "64-bit"
# "arm64" = "ARM64"
# }
}

data "aws_ami" "pvc_base" {
filter {
name = "image-id"
Expand Down Expand Up @@ -55,3 +74,51 @@ resource "aws_instance" "pvc_base" {
Name = var.quantity == 0 ? var.name : format("%s-%02d", var.name, count.index + var.offset + 1)
})
}

data "aws_pricing_product" "pvc_base" {
for_each = { for idx, instance in aws_instance.pvc_base : idx => instance }

provider = aws.pricing_calculator

service_code = "AmazonEC2"

filters {
field = "instanceType"
value = each.value.instance_type
}

filters {
field = "regionCode"
value = each.value.region
}

filters {
field = "operatingSystem"
value = local.pricing_os_map[data.aws_ami.pvc_base.platform_details]
}

# filters {
# field = "processorArchitecture"
# value = local.pricing_arch_map[data.aws_ami.pvc_base.architecture]
# }

filters {
field = "marketoption"
value = "OnDemand" # TODO Review marketoption as a variable to the module
}

filters {
field = "preInstalledSw"
value = "NA"
}

filters {
field = "tenancy"
value = "Shared"
}

filters {
field = "capacitystatus"
value = "Used"
}
}
18 changes: 17 additions & 1 deletion modules/hosts/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,20 @@ output "hosts" {
output "storage_volumes" {
value = local.attached_volumes_by_instance
description = "Additional Storage Volumes"
}
}

locals {
pvc_base_pricing_data = values(
values(
jsondecode(values(data.aws_pricing_product.pvc_base)[0].result).terms.OnDemand
)[0].priceDimensions
)[0]
}

output "pricing" {
description = "The hourly cost and details for each instance of this module."
value = {
price_per_hour = local.pvc_base_pricing_data.pricePerUnit.USD
price_description = local.pvc_base_pricing_data.description
}
}
1 change: 0 additions & 1 deletion modules/network/examples/ex01-minimal_inputs/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ resource "aws_vpc_dhcp_options_association" "base" {
module "ex01_network" {
source = "../.."

region = var.region
prefix = var.prefix
vpc_id = aws_vpc.base.id

Expand Down