How do I tag an AWS Glue interactive session?
I want to tag my AWS Glue interactive session for cost tracking.
Resolution
Use AWS Glue Studio Notebook to add tags to an interactive session when it's created. Use AWS Command Line Interface (CLI) to tag a session that already exists.
Tags can be used to assess costs with AWS Billing and Cost Management and AWS Cost and Usage Reports (CUR). For more information on tags, see Organizing and tracking costs using AWS cost allocation tags.
Note: After you tag a session, it can take up to 24 hours for tags to populate in the Billing console or CUR.
Create an AWS Glue interactive session with tags
To use an AWS Glue Studio notebook to tag an interactive session when it's created, complete the following steps. Replace example_ values with the values that correspond with your use case:
-
Open AWS Glue.
-
In the left side pane, choose Notebook.
-
Choose Author using an interactive code notebook.
-
Choose Create Notebook.
-
In the first cell, enter the following configuration, and then run the cell:
# Set the configuration of your sessions using magics # Tip: non-cell magics can share the same cell %idle_timeout 10 %glue_version 4.0 %worker_type G.1X %number_of_workers 2
-
In the second cell, enter the following code, and then run the cell:
import sys from awsglue.transforms import * from awsglue.utils import getResolvedOptions from pyspark.context import SparkContext from awsglue.context import GlueContext from awsglue.job import Job sc = SparkContext.getOrCreate() glueContext = GlueContext(sc) spark = glueContext.spark_session job = Job(glueContext)
-
In the third cell, enter your tags, and then run the cell:
# Set a tag to associate the session with billable department or cost center # Tip: tags is a cell magic (meaning it needs its own cell) %%tags {'example_key':'example_tag'}
Note: You can add multiple keys and tags, separated by a comma:
{'example_key_1':'example_tag_1', 'example_key_2':'example_tag_2'}
-
In the fourth cell, display the session's status, and then run the cell:
%status
-
Verify that the output resembles the following:
Session ID: *******-**** Status: READY ... ... Tags: {'owner': '********', 'example_key':'example_tag'} ..
Tag an interactive session that already exists
To use AWS CLI to tag an interactive session that already exists, complete the following steps. Replace example_ values with the values that correspond with your use case:
Note: If you receive errors when you run AWS Command Line Interface (AWS CLI) commands, then see Troubleshoot AWS CLI errors. Also, make sure that you're using the most recent AWS CLI version.
-
Open the command line.
-
List the sessions in your AWS account:
aws glue list-sessions
-or-
List sessions with a specific tag:aws glue list-sessions --tags example_key=example_tag
-
Locate the session that needs tags.
-
Add tags to the session:
aws glue tag-resource --resource-arn arn:aws:glue:example_region:example_AWS_Account_ID:session/example_session_id -tags-to-add example_key=example_tag
-or-
Add tags to an interactive session job:
aws glue tag-resource --resource-arn arn:aws:glue:example_region:example_AWS_Account_ ID:job/example_jobname -tags-to-add example_key=example_tag
Note: You can add multiple keys and tags, separated by a comma.
-
Verify that the tags are added:
aws glue get-tags --resource-arn arn:aws:glue:example_region:example_AWS_Account_ID:session/example_session_id
Use tags to assess costs in the Billing Console
To use tags to assess costs in the Billing Console, complete the following steps:
- Open the Billing console.
- In the navigation pane, choose Cost allocation tags.
- Use the Search function to locate and select your session's tags.
- Choose Activate, and then wait for the Billing console to apply the tags.
Note: After you choose Activate, it can take up to 24 hours for the Billing console to apply the tags. - In the navigation pane, under Cost Management, choose Cost explorer.
- In the Report parameters pane, under Group by - Dimension, choose Usage Type.
- In the Report parameters pane, in the Usage Type section of Filters, select example_region-GlueInteractiveSessions-DPU-Hour.
Note: substitute example-region with your AWS Region. - In the Report parameters pane, select one of the active tags.
- In the menu, under your active tag, select all of the tags that correspond with your use case.
- Choose Apply. The output resembles the following:
Usage type Usage type total July 2024* Total costs $0.85 $0.85 Total usage 1.93 1.93 DPU-Hour USE1-GlueInteractiveSession-DPU-Hour costs $0.85 $0.85 USE1-GlueInteractiveSession-DPU-Hour usage 1.93 DPU-Hour 1.93 DPU-Hour
Use tags to assess costs in CUR
Use CUR to analyze the cost breakdown of an individual interactive session. To assess costs by tag in CUR, complete the following steps:
- Create a CUR. For more information, see Creating Cost and Usage Reports.
Note: The CUR file's record begins with the month that it's activated. If the CUR must record previous months, then contact AWS Support. - Set up Amazon Athena. For more information, see Querying Cost and Usage Reports using Amazon Athena.
- Open the Athena console.
- Run the following command. Replace example_ values with the values that correspond with your use case:
SELECT SUM(CAST(line_item_unblended_cost AS DECIMAL(16, 2))) AS cost, line_item_resource_id as "interactive session arn" FROM example_Table_created_by_cur_setup where product_product_name = ('AWS Glue') and line_item_operation='GlueInteractiveSession' AND resource_tags_user_example_key_1 like 'example_tag_1' AND resource_tags_user_example_key_2 like 'example_tag_2' group by product_product_name,line_item_operation,line_item_usage_start_date, line_item_resource_id;
# cost interactive session arn 1 0.05 arn:aws:glue:us-east-1:111111111:session/82f3be20-1111-1111-1111-588b4677b96a 2 0.46 arn:aws:glue:us-east-1:111111111:session/28939cd1-1111-1111-1111-ccb95bb25b9f 3 0.12 arn:aws:glue:us-east-1:111111111:session/54f0943b-1111-1111-1111-383d69920fc6 4 0.22 arn:aws:glue:us-east-1:111111111:session/730d0637-1111-1111-1111-80cc76989f3c
Related information
Magics supported by AWS Glue interactive sessions for Jupyter
Relevanter Inhalt
- AWS OFFICIALAktualisiert vor 2 Jahren
- AWS OFFICIALAktualisiert vor 2 Jahren
- AWS OFFICIALAktualisiert vor 2 Jahren