![Logo](https://raw.githubusercontent.com/TheFulmini/cdk-cost-limit/main/images/logo.png)
A Collection of CDK Constructs to Deploy Cost-Aware Self-Limiting Resources
Website
Β·
Docs
Β·
Request Feature
This package lets you set spending limits on AWS. While existing AWS solutions merely alert, this library disables resources, using non-destructive operations, when budgets are hit.
Every month, companies and individual contributors face surging cloud bills1 due to mistakes in their application code, misunderstandings of services pricing model, or even malicious activities. This library shields you from such events.
This library includes an Aspect and a collection of AWS CDK Level-2 Constructs. They deploy additional resources to compute real-time spending and halt resources when budgets are met (e.g. Lambda Functions reserved concurrency is set to 0)
- π§ Availability - Disabled resources impacts your application availability
- π° Costs - Tracking spending and halting resources incur additional costs
- ποΈ Performance - Some implementations negatively impact performances under normal conditions
Important
π§βπ» We're actively working on reducing cost and perf impacts of this library! We'll keep you posted as we minimize and eventually remove complitely those tradeoffs
CDK Cost Limit is built upon the AWS CDK, so you need to install Node.js (>= 14.15.0), even those working in languages other than TypeScript or JavaScript (see AWS CDK Documentation).
npm install -s @revant-io/cdk-cost-limit
Using this library is as simple as importing the CostLimit
Aspect and using it on any node to apply a budget on the node and all of its children.
import { Aspects } from "aws-cdk-lib";
import { CostLimit } from "@revant-io/cdk-cost-limit";
Aspects.of(anyConstruct).add(new CostLimit({ budget: 1000 }))
Using an aspect allow setting a budget on a specific construct of the application, or even a whole stack. Multiple budgets can be set up at once.
import { App, Aspects, Stack } from "aws-cdk-lib";
import { Function } from "aws-cdk-lib/aws-lambda";
import { CostLimit } from "@revant-io/cdk-cost-limit";
const app = new App();
const stack = new Stack(app, "MyStack");
const myEC2Instance = new Instance(stack, "MyEC2Instance");
const myFirstLambdaFunction = new Function(stack, "MyFirstLambda");
const mySecondLambdaFunction = new Function(stack, "MySecondLambda");
// Sets one global budget on all resources within MyStack of $10,00
Aspects.of(stack).add(new CostLimit({ budget: 1000 }));
// Sets another budget on MyFirstLambda of $2,00
Aspects.of(myFirstLambdaFunction).add(new CostLimit({ budget: 200 }));
// Sets a final budget on MySecondLambda of $5,00
Aspects.of(mySecondLambdaFunction).add(new CostLimit({ budget: 500 }));
Once a budget is hit, all resources within the construct on which this budget has been applied are disabled to prevent further cost increase. In the above example:
- once
MyFirstLambda
incurred costs reach $2,00, the lambda function is disabled - once
MySecondLambda
incurred costs reach $5,00, the lambda function is disabled - once
MyStack
incurred costs reach $10,00 - in this specific case, the addition of incurred costs fromMyEC2Instance
,MyFirstLambda
andMySecondLambda
- all resources withinMyStack
are disabled
While CostLimit
aspect can be applied on any node type, budget capabilities are only enabled on a subset of resources listed below. Cost generated by resources other than the one specified are not taken into account with regards to the budget limit. Only resources listed below are disabled once budget is reached.
- β
AWS::Lambda::Function
- β³
AWS::EC2::Instance
-
π§βπ» Develop - Use this library to set your budget limit. Look at the catalog documentation to learn how to implement a budget. You can update budget amount and location anytime.
-
π€ Limit - Budgeted resources will disable automatically when budget is reached. Have a look at each service documentation to understand how resource are disabled.
-
π Restore
-
π Automatically - At the end of each month, resources that have been disabled are automatically re-enabled
-
βοΈ Manually - You might want to increase budget and keep on using disabled resources in the current month. In order to do so, you need to follow each service documentation procedure to identify and manually re-enable affected resources
-
Footnotes
-
π Have a look at those AWS billing horror stories aggregated by Victor Grenu! β©