-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.tf
More file actions
153 lines (129 loc) · 7.55 KB
/
Copy pathmain.tf
File metadata and controls
153 lines (129 loc) · 7.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* -------------------------------------------------------------------------- */
/* AWS billing export */
/* -------------------------------------------------------------------------- */
/* -------------------------- Billing export bucket ------------------------- */
resource "aws_s3_bucket" "export" {
# Create the s3 bucket only if var.create_s3_bucket is true
count = var.create_s3_bucket ? 1 : 0
bucket = var.s3_bucket_name
force_destroy = var.s3_force_destroy
tags = var.tags
}
/* --------------------------- Current account id --------------------------- */
# This is used to set s3 bucket policy
data "aws_caller_identity" "current" {}
/* -------------------- S3 bucket policy for data export -------------------- */
# https://docs.aws.amazon.com/cur/latest/userguide/dataexports-s3-bucket.html
resource "aws_s3_bucket_policy" "allow_data_export" {
# Create the s3 bucket policy only if var.create_s3_bucket is true
count = var.create_s3_bucket ? 1 : 0
bucket = aws_s3_bucket.export[0].id
policy = data.aws_iam_policy_document.allow_data_export[0].json
}
data "aws_iam_policy_document" "allow_data_export" {
# Create the policy only if var.create_s3_bucket is true
count = var.create_s3_bucket ? 1 : 0
statement {
sid = "EnableAWSDataExportsToWriteToS3AndCheckPolicy"
effect = "Allow"
principals {
type = "Service"
identifiers = [
"billingreports.amazonaws.com",
"bcm-data-exports.amazonaws.com"
]
}
actions = [
"s3:PutObject",
"s3:GetBucketPolicy",
]
resources = [
aws_s3_bucket.export[0].arn,
"${aws_s3_bucket.export[0].arn}/*",
]
condition {
test = "StringLike"
variable = "aws:SourceAccount"
values = [data.aws_caller_identity.current.account_id]
}
condition {
test = "StringLike"
variable = "aws:SourceArn"
values = [
"arn:aws:cur:us-east-1:${data.aws_caller_identity.current.account_id}:definition/*",
"arn:aws:bcm-data-exports:us-east-1:${data.aws_caller_identity.current.account_id}:export/*"
]
}
}
}
data "aws_s3_bucket" "export" {
# The bucket exist only if var.create_s3_bucket is false
count = var.create_s3_bucket ? 0 : 1
bucket = var.s3_bucket_name
}
locals {
s3_bucket = {
name = var.create_s3_bucket ? aws_s3_bucket.export[0].bucket : data.aws_s3_bucket.export[0].bucket
region = var.create_s3_bucket ? aws_s3_bucket.export[0].region : data.aws_s3_bucket.export[0].region
}
export_version = {
# FOCUS 1.2
# Announcement: https://aws.amazon.com/blogs/aws-cloud-financial-management/data-exports-for-focus-1-2-is-now-generally-available/
# Documentation: https://docs.aws.amazon.com/cur/latest/userguide/table-dictionary-focus-1-2-aws.html
"1.2" = {
query_statement = "SELECT AvailabilityZone, BilledCost, BillingAccountId, BillingAccountName, BillingAccountType, BillingCurrency, BillingPeriodEnd, BillingPeriodStart, CapacityReservationId, CapacityReservationStatus, ChargeCategory, ChargeClass, ChargeDescription, ChargeFrequency, ChargePeriodEnd, ChargePeriodStart, CommitmentDiscountCategory, CommitmentDiscountId, CommitmentDiscountName, CommitmentDiscountQuantity, CommitmentDiscountStatus, CommitmentDiscountType, CommitmentDiscountUnit, ConsumedQuantity, ConsumedUnit, ContractedCost, ContractedUnitPrice, EffectiveCost, InvoiceId, InvoiceIssuerName, ListCost, ListUnitPrice, PricingCategory, PricingCurrency, PricingCurrencyContractedUnitPrice, PricingCurrencyEffectiveCost, PricingCurrencyListUnitPrice, PricingQuantity, PricingUnit, ProviderName, PublisherName, RegionId, RegionName, ResourceId, ResourceName, ResourceType, ServiceCategory, ServiceName, ServiceSubcategory, SkuId, SkuMeter, SkuPriceDetails, SkuPriceId, SubAccountId, SubAccountName, SubAccountType, Tags, x_Discounts, x_Operation, x_ServiceCode FROM FOCUS_1_2_AWS"
table_configurations = { FOCUS_1_2_AWS : { TIME_GRANULARITY : "HOURLY" } }
}
# FOCUS 1.0 GA
# Announcement: https://aws.amazon.com/blogs/aws-cloud-financial-management/data-exports-for-focus-1-0-is-now-generally-available/
# Documentation: https://docs.aws.amazon.com/cur/latest/userguide/table-dictionary-focus-1-0-aws.html
"1.0" = {
query_statement = "SELECT AvailabilityZone, BilledCost, BillingAccountId, BillingAccountName, BillingCurrency, BillingPeriodEnd, BillingPeriodStart, ChargeCategory, ChargeClass, ChargeDescription, ChargeFrequency, ChargePeriodEnd, ChargePeriodStart, CommitmentDiscountCategory, CommitmentDiscountId, CommitmentDiscountName, CommitmentDiscountStatus, CommitmentDiscountType, ConsumedQuantity, ConsumedUnit, ContractedCost, ContractedUnitPrice, EffectiveCost, InvoiceIssuerName, ListCost, ListUnitPrice, PricingCategory, PricingQuantity, PricingUnit, ProviderName, PublisherName, RegionId, RegionName, ResourceId, ResourceName, ResourceType, ServiceCategory, ServiceName, SkuId, SkuPriceId, SubAccountId, SubAccountName, Tags, x_CostCategories, x_Discounts, x_Operation, x_ServiceCode, x_UsageType FROM FOCUS_1_0_AWS"
table_configurations = { FOCUS_1_0_AWS : {} }
}
# FOCUS 1.0 (Preview)
# Announcement: https://aws.amazon.com/blogs/aws-cloud-financial-management/announcing-data-exports-for-focus-1-0-preview-in-aws-billing-and-cost-management/
# Documentation: https://docs.aws.amazon.com/cur/latest/userguide/table-dictionary-focus-1-0-aws-preview.html
"1.0-preview" = {
query_statement = "SELECT AvailabilityZone, BilledCost, BillingAccountId, BillingAccountName, BillingCurrency, BillingPeriodEnd, BillingPeriodStart, ChargeCategory, ChargeClass, ChargeDescription, ChargeFrequency, ChargePeriodEnd, ChargePeriodStart, CommitmentDiscountCategory, CommitmentDiscountId, CommitmentDiscountName, CommitmentDiscountType, CommitmentDiscountStatus, ConsumedQuantity, ConsumedUnit, ContractedCost, ContractedUnitPrice, EffectiveCost, InvoiceIssuerName, ListCost, ListUnitPrice, PricingCategory, PricingQuantity, PricingUnit, ProviderName, PublisherName, RegionId, RegionName, ResourceId, ResourceName, ResourceType, ServiceCategory, ServiceName, SkuId, SkuPriceId, SubAccountId, SubAccountName, Tags, x_CostCategories, x_Discounts, x_Operation, x_ServiceCode, x_UsageType FROM FOCUS_1_0_AWS_PREVIEW"
table_configurations = { FOCUS_1_0_AWS_PREVIEW : {} }
}
}
}
/* ------------------------------ FOCUS export ------------------------------ */
# Config adapted from https://github.com/aws-samples/aws-cudos-framework-deployment/blob/cd2363e070b29ac4036b92919f863314aa15856e/cfn-templates/data-exports-aggregation.yaml#L516
resource "aws_bcmdataexports_export" "focus" {
# Only create this export if the export_type is FOCUS
count = var.export_type == "FOCUS" ? 1 : 0
export {
name = var.export_name
data_query {
query_statement = local.export_version[var.export_version].query_statement
table_configurations = local.export_version[var.export_version].table_configurations
}
description = "FOCUS export"
destination_configurations {
s3_destination {
s3_bucket = local.s3_bucket.name
s3_prefix = var.export_s3_prefix
s3_region = local.s3_bucket.region
s3_output_configurations {
overwrite = "OVERWRITE_REPORT"
format = "PARQUET"
compression = "PARQUET"
output_type = "CUSTOM"
}
}
}
refresh_cadence {
frequency = "SYNCHRONOUS"
}
}
tags = var.tags
lifecycle {
ignore_changes = [
export[0].data_query[0].table_configurations,
tags
]
}
}