Chapter 4. Integrate your AWS account with data filtering
Configure a function script in AWS to filter and export a subset of billing data to object storage, enabling selective data sharing with cost management while maintaining privacy control.
- If you created an unfiltered AWS integration, do not complete the following steps. Your AWS integration is already complete.
- AWS is a third-party product and its UI and documentation can change. The instructions for configuring third-party integrations are correct at the time of publishing. For the most up-to-date information, see the AWS documentation.
4.1. Initiate the AWS integration process Copy linkLink copied to clipboard!
Add an AWS integration to enable cost management to process your AWS Cost and Usage Reports and provide visibility into your cloud spending patterns.
Prerequisites
- You must have a Red Hat Hybrid Cloud Console service account.
- To add data integrations to cost management, you must have a Red Hat account with Cloud Administrator permissions.
Procedure
-
From Red Hat Hybrid Cloud Console, click Settings
> Integrations.
-
On the Settings page, click
to enter the Add a cloud integration wizard. - On the Select cloud provider step, select Amazon Web Services. Click Next.
- Enter a name for the integration and click Next.
- On the Select configuration step, select Manual configuration. Do not select the recommended configuration mode when you configure cost management integrations. The recommendation is for other workflows.
- In the Select application step, select Cost management. Click Next.
4.2. Prepare cost data for filtering with Athena Copy linkLink copied to clipboard!
Create an Amazon S3 bucket and data export using Athena to prepare your billing data for filtering before cost analysis.
Procedure
- Log in to your AWS account.
In Billing and Cost Management, create a data export to deliver to your S3 bucket. Specify the following values and accept the defaults for any other values:
- Export details: Legacy CUR export
- Export name: <rh_cost_report> (save this name, you will use it later)
- Additional export content: Include resource IDs
- S3 bucket: Select an S3 bucket that you configured previously or create a new bucket and accept the default settings.
- Time granularity: Hourly
- Report data integration: Amazon Athena which is required for lambda queries
- Compression type: Parquet
- S3 path prefix: cost
NoteFor more details about configuration, see the AWS Billing and Cost Management documentation.
4.3. Configure AWS storage for filtered cost data exports Copy linkLink copied to clipboard!
Create a second S3 bucket to store filtered billing data before sharing it with Red Hat, enabling you to control which cost information is exposed.
Procedure
- In your AWS account, navigate to S3 and click Create Bucket.
- Create a bucket and apply the default policy.
- Click Save.
- In the cost management Create an integration wizard, navigate to the Create storage step.
- Paste the name of your S3 bucket and select the region that it was created in.
- Click Next.
- On the Create cost and usage report step, select I wish to manually customize the CUR sent to Cost Management.
- Click Next.
4.4. Import tags to organize cost data Copy linkLink copied to clipboard!
Activate your tags in AWS and then give cost management permissions to import them automatically. Tags can help you organize your AWS resources in cost management.
For more information about tagging, see Adding tags to an AWS resource.
Procedure
- In the AWS Billing console, click Cost Allocation Tags.
Select the tags that you want to use in cost management. Click Activate.
-
If your organization is converting systems from CentOS 7 to RHEL and using hourly billing, activate the
com_redhat_rheltag for your systems.
-
If your organization is converting systems from CentOS 7 to RHEL and using hourly billing, activate the
4.5. Grant secure access to AWS Cost and Usage Reports Copy linkLink copied to clipboard!
Create an IAM policy and role in AWS to grant cost management secure, read-only access to your AWS Cost and Usage Reports for billing analysis.
Procedure
In the cost management Add a cloud integration wizard, on the Tags, aliases, and organizational units step, select any additional data points that you want to include, then click Next:
-
Select Include AWS account aliases to display an AWS account alias rather than an account number. In the next step of the wizard, this selection populates
iam:ListAccountAliasesin your IAM JSON policy. -
Select Include AWS organization units if you are using consolidated billing rather than the account ID. In the next step of the wizard, this selection populates
_organization:List*_and_organizations:Describe*_in your IAM JSON policy.
-
Select Include AWS account aliases to display an AWS account alias rather than an account number. In the next step of the wizard, this selection populates
- Copy the IAM JSON policy that is generated based on your selections.
In the AWS Identity and Access Management console, create a new IAM policy:
Select the JSON tab and enter the IAM JSON policy that you copied from the Red Hat Hybrid Cloud Console Add a cloud integration wizard.
Example IAM JSON policy
{ "Version": "2012-10-17", "Statement": [ { "Sid": "VisualEditor0", "Effect": "Allow", "Action": [ "s3:ListBucket", "s3:GetObject" ], "Resource": [ "arn:aws:s3:::<your_bucket_name>", "arn:aws:s3:::<your_bucket_name>/*" ] }, { "Sid": "VisualEditor1", "Effect": "Allow", "Action": [ "cur:DescribeReportDefinitions", ], "Resource": "*" } ] }- Enter a name and create your policy.
Create a new IAM role:
- Select Another AWS account as the type of trusted entity.
- Enter 589173575009 for the Account ID to give Red Hat Hybrid Cloud Console read access to the AWS account’s cost data.
- In the cost management Add a cloud integration wizard, click Next, then copy your External ID from the Create IAM role step.
In the AWS Identity and Access Management console, complete the IAM role configuration:
- Enter your External ID.
- Attach the IAM policy that you configured.
- Enter a name and description to finish creating your role.
-
In Roles, open the summary screen for the role that you created, then copy the Role ARN (starts with
arn:aws:). - In the cost management Add a cloud integration wizard, click Next, enter your Role ARN, then click Next again.
- Review the details of your cloud integration and click Add.
Verification
Verify that Cost management can access Cost and Usage data from your AWS account and linked AWS accounts.
NoteThe data can take a few days to populate before it shows on the cost management dashboard.
4.6. Configure data filtering Copy linkLink copied to clipboard!
Control and refine your billing reports in AWS before they are shared with Red Hat. While more complex to configure than an unfiltered connection, this method enables you to restrict data sharing to specific Red Hat products or business units.
4.6.1. Enable account access for data filtering with Athena and Lambda functions Copy linkLink copied to clipboard!
Create an IAM policy and role to grant your Lambda function and Athena the permissions needed to filter and process billing data before sharing.
Procedure
From the AWS Identity and Access Management (IAM) console, create an IAM policy for the Athena Lambda functions you will configure.
Select the JSON tab and paste the following content in the JSON policy text box:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "athena:*" ], "Resource": [ "*" ] }, { "Effect": "Allow", "Action": [ "glue:CreateDatabase", "glue:DeleteDatabase", "glue:GetDatabase", "glue:GetDatabases", "glue:UpdateDatabase", "glue:CreateTable", "glue:DeleteTable", "glue:BatchDeleteTable", "glue:UpdateTable", "glue:GetTable", "glue:GetTables", "glue:BatchCreatePartition", "glue:CreatePartition", "glue:DeletePartition", "glue:BatchDeletePartition", "glue:UpdatePartition", "glue:GetPartition", "glue:GetPartitions", "glue:BatchGetPartition" ], "Resource": [ "*" ] }, { "Effect": "Allow", "Action": [ "s3:GetBucketLocation", "s3:GetObject", "s3:ListBucket", "s3:ListBucketMultipartUploads", "s3:ListMultipartUploadParts", "s3:AbortMultipartUpload", "s3:CreateBucket", "s3:PutObject", "s3:PutBucketPublicAccessBlock" ], "Resource": [ "arn:aws:s3:::CHANGE-ME*"1 ] }, { "Effect": "Allow", "Action": [ "s3:GetObject", "s3:ListBucket" ], "Resource": [ "arn:aws:s3:::CHANGE-ME*"2 ] }, { "Effect": "Allow", "Action": [ "s3:ListBucket", "s3:GetBucketLocation", "s3:ListAllMyBuckets" ], "Resource": [ "*" ] }, { "Effect": "Allow", "Action": [ "sns:ListTopics", "sns:GetTopicAttributes" ], "Resource": [ "*" ] }, { "Effect": "Allow", "Action": [ "cloudwatch:PutMetricAlarm", "cloudwatch:DescribeAlarms", "cloudwatch:DeleteAlarms", "cloudwatch:GetMetricData" ], "Resource": [ "*" ] }, { "Effect": "Allow", "Action": [ "lakeformation:GetDataAccess" ], "Resource": [ "*" ] }, { "Effect": "Allow", "Action": [ "logs:*" ], "Resource": "*" }, { "Sid": "VisualEditor3", "Effect": "Allow", "Action": [ "secretsmanager:GetSecretValue", "secretsmanager:DescribeSecret" ], "Resource": "*" } ] }
Replace
CHANGE-ME*in both locations with the ARN for the S3 bucket you configured in step 2.2.- Enter a name and finish creating the policy.
Create a new IAM role:
- For the type of trusted entity, select AWS service.
- Select Lambda.
- Attach the IAM policy you just configured.
- Enter a role name and description and finish creating the role.
Store your Service Account information in AWS Secrets Manager and add it to the role you created.
NoteThe service account used to upload data must be added to a user group that has the
cost management:settings:writepermission. When configuring this in the Red Hat Hybrid Cloud Console, ensure that the group’s assigned role includes the cost-management application and the settings resource type with write operations enabled. This is required to access the Ingress Reports API for data uploads. .. Select Secret type: Other type of secret to create a secret. .. Create a key for your Red Hat Hybrid Cloud Console Service Accountclient_id. .. Create a key for your Red Hat Hybrid Cloud Console Service Accountclient_secret. .. Add the values for your Service Account to the corresponding key. .. Click Continue, then enter a name and store your secret.
4.6.2. Configure Athena for filtered report generation Copy linkLink copied to clipboard!
Configure an Athena table to query and filter your billing data before sharing it with cost management, enabling selective data exposure.
The following configuration only provides access to additional stored information. It does not provide access to anything else.
Procedure
-
In the AWS S3 console, go to the S3 bucket you configured in step 2.2. Then, go to the
crawler-cfn.ymlfile, which is in the path created by your data export you configured. For example:{bucket-name}/{S3_path_prefix}/{export_name}/crawler-cfn.yml. Copy the Object URL for thecrawler-cfn.yml. From Cloudformation in the AWS console, create a stack with new resources:
- Choose an existing template.
- Select Specify Template.
- Select Template Source: Amazon S3 URL.
- Paste the object URL you copied before.
- Enter a name and click Next.
- Click I acknowledge that AWS Cloudformation might create IAM resources and then click Submit.
4.6.3. Define specific data to query with Athena Copy linkLink copied to clipboard!
Create an Athena query to extract only Red Hat-related expenses from your billing data, reducing the data volume shared with cost management.
You might need just the query included with the example script, for example, if you are filtering for Red Hat spending. If you need something more advanced, create a custom query. If you are using RHEL metering, you must adjust the query to return data that is specific to your RHEL subscriptions. The following steps guide you through creating a RHEL subscription query.
Example Athena query for Red Hat spend:
SELECT *
FROM <your_export_name>
WHERE (
bill_billing_entity = 'AWS Marketplace'
AND line_item_legal_entity like '%Red Hat%'
)
OR (
line_item_legal_entity like '%Amazon Web Services%'
AND line_item_line_item_description like '%Red Hat%'
)
OR (
line_item_legal_entity like '%Amazon Web Services%'
AND line_item_line_item_description like '%RHEL%'
)
OR (
line_item_legal_entity like '%AWS%'
AND line_item_line_item_description like '%Red Hat%'
)
OR (
line_item_legal_entity like '%AWS%'
AND line_item_line_item_description like '%RHEL%'
)
OR (
line_item_legal_entity like '%AWS%'
AND product_product_name like '%Red Hat%'
)
OR (
line_item_legal_entity like '%Amazon Web Services%'
AND product_product_name like '%Red Hat%'
)
AND year = '2024'
AND month = '07'
Procedure
- In your link:https://aws.amazon.com/console/AWS account, go to Amazon Athena from the editor tab.
- From the Data source menu, select AwsDataCatalog.
-
From the Database menu, select your data export. Your data export name is prepended with
athenacurcfn_followed by your data export name. For example,{your_export_name}. Paste the following example query into the Query field. Replace the
your_export_namevalue with your data export name.SELECT column_name FROM information_schema.columns WHERE table_name = '<your_export_name>' AND column_name LIKE 'resource_tags_%';- Click Run. The results of this query returns all the tag related columns for your data set.
- Copy the tag column that matches the column used for your RHEL tags.
Paste in the following example query. Replace the
your_export_name, the tags column copied in the step before, and theyearandmonthyou want to query. The result returns EC2 instances appropriately tagged for RHEL subscriptions. Copy and save this query for use in the future Lambda function.SELECT * FROM <your_export_name> WHERE ( line_item_product_code = 'AmazonEC2' AND strpos(lower(<rhel_tag_column_name>), 'com_redhat_rhel') > 0 ) AND year = '<year>' AND month = '<month>'
4.6.4. Process filtered data with a Lambda function Copy linkLink copied to clipboard!
Create a Lambda function to automate filtering of Red Hat-related expenses from your billing data, streamlining the data preparation process.
Procedure
- In the AWS console, go to Lambda and click Create function.
- Click Author from scratch.
- Enter a name for your function.
- From the Runtime menu, select the latest version of Python available.
- From the Architecture menu, select x86_64.
- Under Permissions select the Athena role you created.
- To add the query you built as part of the Lambda function, click Create function to save your progress.
From the function Code tab, paste this script. Update the following lines:
your_integration_external_id- Enter the integration UUID you copied in the Configuring an IAM policy to enable account access for AWS Cost and Usage Reports step.
bucket- Enter the name of the S3 bucket you created to store filtered reports during the Creating a bucket for storing filtered data reporting step.
database- Enter the database name used in the Building your Athena query step.
export_name- Enter the name of your data export from when you created an AWS S3 bucket for storing your cost data.
Update the default query with your custom one by replacing the
whereclause, for example:# Athena query query = f"SELECT * FROM {database}.{export_name} WHERE (line_item_product_code = 'AmazonEC2' AND strpos(lower(<rhel_tag_column_name>), 'com_redhat_rhel') > 0) AND year = '{year}' AND month = '{month}'"- Click Deploy to test the function.
4.6.5. Deliver accessible filtered reports with a Lambda function Copy linkLink copied to clipboard!
Create a second Lambda function to deliver filtered billing reports to a Red Hat-accessible bucket, completing the secure data sharing pipeline.
Procedure
- Go to Lambda in the AWS console and click Create function.
- Click Author from scratch.
- Enter a name for your function.
- From the Runtime menu, select the latest version of Python available.
- Select x86_64 as the architecture type for your function.
- Under Permissions, select the Athena role that you created.
- Click Create function.
Paste this script into the function and replace the following lines:
secret_name = "CHANGEME"- Enter the secret name that you used in the Enabling account access for Athena step.
bucket = "<your_S3_Bucket_Name>"- Enter the name of the S3 bucket that you created to store filtered reports in the Creating a bucket for storing filtered data reporting step.
- Click Deploy to test the function.
4.7. Schedule automated report updates with an AmazonEventBridge Copy linkLink copied to clipboard!
Schedule automated execution of your Lambda functions using Amazon EventBridge to keep filtered billing data current without manual intervention.
Procedure
Create two AmazonEventBridge schedules to trigger each of the functions that you created. You must trigger these functions at different cadences so that the Athena query is completed before it sends the reports:
- Add a name and description.
- In the Group field, select Default.
- In the Occurrence field, select Recurring schedule.
- In the Type field, select Chron-based.
-
Set the cron-based schedules 12 hours apart. The following example triggers the function at 9AM and 9PM,
0 9 * * ? *and0 21 * * ? *. - Set a flexible time window.
- Click Next.
Set the Target detail to AWS Lambda invoke to associate this schedule with the Lambda function:
- Select the Lambda function you created before.
- Click Next.
Enable the schedule:
- Configure the retry logic.
- Ignore the encryption.
- Set the permissions to Create new role on the fly.
- Click Next.
- Review your selections and click Create.
4.8. Automate collection of finalized data for Cost Management Copy linkLink copied to clipboard!
AWS sends final reports for the last month at the start of the following month. Send these finalized reports to Cost management, which will analyze the extra information.
Procedure
Create Athena query for the Lambda function:
- Create a function for querying Athena.
- Select Author from scratch.
- Select the Python runtime.
- Select the x86_64 architecture.
- Select the role created before for permissions.
- Click Create.
Click the Code tab to add a script to collect the finalized data.
-
Copy the Athena query function and add it to the query. Update the
<integration_uuid>with theintegration_uuidfrom the integration you created on console.redhat.com, which you can find by going to the the Integrations page and clicking your integration. Update theBUCKETandDATABASEvariables with the bucket name and databases you created. Then, updateexport_namewith the name of the data export Athena query you created before. Remove the comment from the following code:
# last_month = now.replace(day=1) - timedelta(days=1) # year = last_month.strftime("%Y") # month = last_month.strftime("%m") # day = last_month.strftime("%d") # file_name = 'finalized-data.json'- Click Deploy. Then click Test to see the execution results.
-
Copy the Athena query function and add it to the query. Update the
Create a Lambda function to post the report files to cost management:
- Select Author from scratch.
- Name your function.
- Select the Python runtime.
- Select the x86_64 architecture.
- Select the role created before for permissions.
- Click Create.
Click the Code tab to add a script to post the finalized data.
-
Copy the post function and add it to the query. Update the
secret_namewith the name of your secret in AWS Secrets Manager. Update thebucketwith the bucket name you created. Remove the comment from the following code:
# file_name = 'finalized_data.json'- Click Deploy. Then click Test to see the execution results.
-
Copy the post function and add it to the query. Update the
Create an EventBridge schedule to trigger the two functions. For more information, see Section 4.7, “Schedule automated report updates with an AmazonEventBridge”.
-
Set the EventBridge schedule to run one time a month on or after the 15th of the month because your AWS bill for the earlier period is final by that date. For example,
(0 9 15 * ? *)and(0 21 15 * ? *).
-
Set the EventBridge schedule to run one time a month on or after the 15th of the month because your AWS bill for the earlier period is final by that date. For example,
Verification
Verify that cost management is collecting Cost and Usage data from your AWS account and linked AWS accounts.
NoteThe data can take a few days to populate before it shows on the cost management dashboard.