1. Controlling AI Compute Costs Using AWS Budgets Alarms.

    Python

    To help you control your AI compute costs on AWS, you can utilize AWS Budgets to create cost budgets and alarms that notify you when your costs exceed predefined thresholds. This method allows you to monitor your AI infrastructure's expenditure and take action when necessary.

    The key components for setting up cost control using AWS Budgets are:

    1. AWS Budget: This enables you to define the expected monthly costs and usage for your AI compute resources. It provides options to include historical costs, planned costs, and various filters to tailor the budget to specific resources or tags.

    2. AWS Budget Actions: When a budget threshold is breached, Budget Actions can be configured to automate responses like sending notifications or even adjusting resources. They can help enforce cost controls and prevent budget overruns.

    3. CloudWatch Alarms: These can be set up in conjunction with budgets to send notifications to an SNS topic, which can then notify you via email, SMS, or any other supported notification method.

    Below is a Pulumi program written in Python, which sets up an AWS Budget to monitor the AI compute costs, and a CloudWatch Alarm to trigger a notification if the budget threshold is exceeded.

    The program performs the following actions:

    1. Creates a budget to monitor the AI compute costs associated with EC2 instances.
    2. Sets up an SNS topic to publish alarms.
    3. Configures a CloudWatch Alarm to trigger when the actual or forecasted costs exceed the budgeted amount.
    4. Adds an email subscription to the SNS topic to receive the notification.
    import pulumi import pulumi_aws as aws # Define your AWS Budget for AI compute costs. # Here 'limit_amount' specifies the budgeted cost threshold for triggering the alarm. # 'time_unit' defines the period for the budget, and 'cost_filters' allows you to specify that the budget only applies # to costs related to AI compute resources by filtering on tags or other properties. ai_compute_budget = aws.budgets.Budget("aiComputeBudget", budget_type="COST", # You can also set this to "USAGE" to monitor the actual usage against your budget. limit_amount="1000", # Set the budgeted amount. This would be your threshold. limit_unit="USD", # The currency for the limit_amount specified. time_unit="MONTHLY", # The period that this budget applies to. cost_filters={ "Service": "AmazonEC2", # Assuming EC2 instances are being used for AI compute. }, notifications=[ { "comparison_operator": "GREATER_THAN", # The comparison used for triggering the notification. "notification_type": "ACTUAL", # 'FORECASTED' to predict future cost breaches, 'ACTUAL' for current cost monitoring. "subscriber_email_addresses": ["your-email@example.com"], # The email that will receive the budget notifications. "threshold": 100, # The percentage of the budget limit at which to send a notification. "threshold_type": "PERCENTAGE", # You can set this to 'ABSOLUTE_VALUE' to trigger at a specific cost amount. }, ] ) # Create an SNS topic to publish budget alarms. budget_alarm_topic = aws.sns.Topic("budgetAlarmTopic") # Subscribe your email to the SNS topic to get alarm notifications. email_subscription = aws.sns.TopicSubscription("emailSubscription", topic=budget_alarm_topic.arn, protocol="email", endpoint="your-email@example.com", # The email address to receive notifications. ) # Create a CloudWatch Alarm that triggers when your budget is breached. budget_alarm = aws.cloudwatch.MetricAlarm("budgetAlarm", alarm_name="aiComputeCostAlarm", comparison_operator="GREATER_THAN_THRESHOLD", evaluation_periods=1, # Number of periods to evaluate for the threshold breach. metric_name="EstimatedCharges", namespace="AWS/Budgets", threshold=ai_compute_budget.limit_amount.apply(lambda amount: float(amount)), # Uses the budget amount as the alarm threshold. alarm_description="Alarm when AI compute costs exceed the budget", period=86400, # Time period in seconds over which the metric is evaluated (24 hours here). statistic="Maximum", alarm_actions=[budget_alarm_topic.arn], # The action to take when the alarm state is reached. ok_actions=[budget_alarm_topic.arn], # The action to take when the alarm returns to the OK state. Not always needed. datapoints_to_alarm=1, # The number of data points that must be breaching to trigger the alarm. ) # Export the sns topic arn and budget id for further use. pulumi.export('budget_alarm_topic_arn', budget_alarm_topic.arn) pulumi.export('ai_compute_budget_id', ai_compute_budget.id)

    Here's a breakdown of what the above code does:

    • The aws.budgets.Budget resource creates a cost budget for your AI compute infrastructure, which estimates and tracks the costs associated with your AWS resources.

    • The aws.sns.Topic resource sets up an SNS topic that can be used to send notifications when the budgeted amount is exceeded.

    • The aws.sns.TopicSubscription resource adds an email subscription to the SNS topic. When an alarm is published to this topic, a notification will be sent to the specified email address.

    • Finally, the aws.cloudwatch.MetricAlarm resource sets up an alarm based on the EstimatedCharges metric for AWS Budgets. This alarm will trigger if the actual or forecasted costs exceed the threshold defined in the budget. When this alarm goes off, it will send a message to the SNS topic, which in turn sends an email notification to the subscribed email addresses.

    Remember to replace your-email@example.com with your real email. When you run this Pulumi program, it will set up the budget, SNS topic, email subscription, and CloudWatch alarm on your AWS account. Whenever your AI compute costs go over the budget, you'll get an email notification. This way, you can keep an eye on your expenditure and quickly take action if needed.

    To run this program, you will need to have Pulumi installed and configured for use with AWS. Save the file with a .py extension and use pulumi up to launch the stack.

    Remember that AWS Free Tier or credits might affect your actual costs, and you should take that into account when setting your budget amounts. Also, managing costs in AWS is complex, and this solution is just a starting point. You may need to adjust your budgets and alarms based on your actual usage patterns and costs.