Skip to main content

AWS Cost and Usage Reports

The AWS Cost and Usage Reports (CUR) integration reads AWS billing data and attributes cost to instances.

Prerequisites​

  • An AWS account with billing access
  • Permission to create IAM users, S3 buckets, and CUR report definitions
  • OpenTofu or Terraform, and AWS provider 5.16 or later

Setup​

Step 1: Apply the module​

shell
git clone https://github.com/massdriver-cloud/integrations.git
cd integrations/aws-cost-and-usage-reports
tofu init
tofu apply

CUR report definitions exist only in us-east-1, so run the module with the AWS provider in us-east-1. The bucket is created in the provider's region. Set bucket_region to that region; it defaults to us-west-2.

VariableDefaultDescription
name_prefixmassdriver-costsPrefix for the bucket, report, and IAM user names
bucket_regionus-west-2Region of the S3 bucket
cost_allocation_tags[]Tag keys to activate as cost allocation tags: md-package, md-instance, or both. See cost attribution.
cur_report_additional_artifacts[]Extra CUR artifacts: REDSHIFT, QUICKSIGHT, ATHENA
tags{}Tags for the created resources. managed-by = massdriver is always set.

Step 2: Read the outputs​

shell
tofu output -json massdriver_integration_config
json
{
"access_key_id": "AKIAIOSFODNN7EXAMPLE",
"secret_access_key": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
"bucket_name": "massdriver-costs-a1b2c3d4",
"bucket_region": "us-west-2"
}

Step 3: Create the integration​

Under Organization settings → Integrations, click Integrate on AWS Cost and Usage Reports, or call createIntegration with id: "aws-cost-and-usage-reports".

FieldKeySource
Access Key IDauth.access_key_idaccess_key_id output
Secret Access Keyauth.secret_access_keysecret_access_key output
S3 Bucket Nameconfig.bucket_namebucket_name output
AWS Regionconfig.regionbucket_region output
Report Formatconfig.formatzip (default) for ZIP-compressed CSV, or parquet. Must match the report's format in AWS.

Resources created​

ResourceNamePurpose
S3 bucket{name_prefix}-{8 hex characters of md5(account id)}Stores the reports
S3 bucket policy—Lets AWS billing (billingreports.amazonaws.com) write reports
CUR report definition{name_prefix}Daily report with resource IDs
IAM user{name_prefix}Identity Massdriver uses
IAM user policy{name_prefix}-policyRead access to the bucket, and tag:GetResources
Access key—Credentials for the IAM user
Cost allocation tagsthe cost_allocation_tags keysOnly when cost_allocation_tags is set

IAM permissions​

ActionResource
s3:HeadBucket, s3:ListBucketThe bucket
s3:GetObjectObjects in the bucket
tag:GetResources*

Report configuration​

The module's report is:

  • Time unit: daily
  • Format: ZIP-compressed CSV, under the reports prefix
  • Additional schema elements: RESOURCES
  • Versioning: OVERWRITE_REPORT

Massdriver also reads Parquet reports. If you change the report to Parquet in AWS, set Report Format to parquet. Parquet column names are snake_case; Massdriver maps them to the same fields it reads from CSV.

Data collection​

Every 24 hours, Massdriver:

  1. Reads the report manifest from the bucket and downloads the current report.
  2. Keeps only Usage line items. Credits, fees, Reserved Instance, and Savings Plans line items are not counted, so costs show what usage costs without credits.
  3. Attributes each line item to an instance and totals the cost per instance.

Cost attribution modes​

Massdriver attributes each line item to an instance by its md-instance tag, which holds the instance id. Resources without md-instance are attributed by the legacy md-package tag. Massdriver chooses the mode from the report.

Resource Groups Tagging API (default)​

Massdriver looks up each resource's current tags with the Resource Groups Tagging API, using the IAM user's tag:GetResources permission. It needs no access to the billing account.

Cost allocation tag column​

When md-instance or md-package is an active cost allocation tag, AWS adds a resourceTags/user:md-instance or resourceTags/user:md-package column to the report. If the report has either column, Massdriver reads the tag from each line item and does not call the Tagging API. The column records the tag at the time of usage, so resources that were deleted or retagged since are still attributed.

To activate tags, list them in cost_allocation_tags on the OpenTofu module. Organizations on the legacy default tags activate md-package:

hcl
module "massdriver_cur" {
source = "github.com/massdriver-cloud/integrations//aws-cost-and-usage-reports?ref=v2.0.0"
cost_allocation_tags = ["md-package"]
}

Version v1.0.0 of the module uses activate_cost_allocation_tag = true for md-package. In v2.0.0, use cost_allocation_tags = ["md-package"]. The upgrade keeps the existing activation.

note

Activating a cost allocation tag is a billing setting that must be applied in the AWS management (payer) account. It applies going forward only and can take up to 24 hours to appear in reports. Until the column appears, Massdriver uses the Tagging API.

Switching to custom default tags​

Organizations that use the legacy default tags send md-package on every resource. When an organization switches to a custom list of default tags, Massdriver stops sending md-package and sends md-instance instead. Each resource gets the new tags at the next deployment of its instance.

Tagging API mode: no change is necessary. Massdriver reads md-instance from each resource.

Cost allocation tag mode: activate md-instance as a cost allocation tag before you switch. When the report has a cost allocation tag column, Massdriver reads the instance tag only from the report columns. Line items for resources that have only md-instance are not attributed until md-instance is active.

Activate md-instance in the AWS management (payer) account. Add md-instance to cost_allocation_tags on the OpenTofu module, or use the Billing and Cost Management console under Cost allocation tags. Keep md-package in the list while resources still carry it.

hcl
module "massdriver_cur" {
source = "github.com/massdriver-cloud/integrations//aws-cost-and-usage-reports?ref=v2.0.0"
cost_allocation_tags = ["md-package", "md-instance"]
}

AWS can only activate a tag key after a resource in the account has that tag, and the key can take up to 24 hours to appear. Until then, tofu apply fails when cost_allocation_tags includes md-instance. To activate md-instance before you switch, add an md-instance tag to one resource manually, then activate the key. If you switch first, costs for line items between the deployment and the activation are not attributed, because the resources no longer have md-package and the report has no md-instance column yet. Activation does not apply to costs from before it.

Troubleshooting​

Enable fails with access_denied​

S3 returned 403. Check that the module applied and the IAM user policy is attached, and that AWS Region is the bucket's region.

Enable fails with bucket_not_found​

S3 returned 404. Check S3 Bucket Name and AWS Region.

No cost data appears​

  • Check that resources have the md-instance or md-package tag.
  • If the report has a cost allocation tag column, check that the tag your resources carry is activated.
  • The first report can take up to 24 hours to appear.
  • Check that AWS is writing the report to the bucket.

Cleanup​

shell
cd integrations/aws-cost-and-usage-reports
tofu destroy
danger

This deletes the S3 bucket and its reports. Cost data Massdriver already collected is kept.