Skip to content

Commit

Permalink
Merge pull request #21 from wtfzambo/fix/python-compatibility
Browse files Browse the repository at this point in the history
feat: custom cost allocation tag and python-compatibility fix
  • Loading branch information
wtfzambo authored Apr 26, 2024
2 parents 172a8b3 + 50a5978 commit 0732971
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 7 deletions.
156 changes: 151 additions & 5 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,13 +311,13 @@ Return default SNS topic only if the defultTopic prop has been passed when insta
```typescript
import { ApplicationCostMonitoring } from 'cost-monitoring-construct'

new ApplicationCostMonitoring(stack: Stack, props: IApplicationCostMonitoringProps)
new ApplicationCostMonitoring(stack: Stack, props: ApplicationCostMonitoringProps)
```

| **Name** | **Type** | **Description** |
| --- | --- | --- |
| <code><a href="#cost-monitoring-construct.ApplicationCostMonitoring.Initializer.parameter.stack">stack</a></code> | <code>aws-cdk-lib.Stack</code> | - default stack to track its resources and it will be used to define Budget resources in it. |
| <code><a href="#cost-monitoring-construct.ApplicationCostMonitoring.Initializer.parameter.props">props</a></code> | <code><a href="#cost-monitoring-construct.IApplicationCostMonitoringProps">IApplicationCostMonitoringProps</a></code> | *No description.* |
| <code><a href="#cost-monitoring-construct.ApplicationCostMonitoring.Initializer.parameter.props">props</a></code> | <code><a href="#cost-monitoring-construct.ApplicationCostMonitoringProps">ApplicationCostMonitoringProps</a></code> | *No description.* |

---

Expand All @@ -331,7 +331,7 @@ default stack to track its resources and it will be used to define Budget resour

##### `props`<sup>Required</sup> <a name="props" id="cost-monitoring-construct.ApplicationCostMonitoring.Initializer.parameter.props"></a>

- *Type:* <a href="#cost-monitoring-construct.IApplicationCostMonitoringProps">IApplicationCostMonitoringProps</a>
- *Type:* <a href="#cost-monitoring-construct.ApplicationCostMonitoringProps">ApplicationCostMonitoringProps</a>

---

Expand Down Expand Up @@ -445,6 +445,141 @@ public readonly applicationName: string;
---


### ApplicationCostMonitoringProps <a name="ApplicationCostMonitoringProps" id="cost-monitoring-construct.ApplicationCostMonitoringProps"></a>

- *Implements:* <a href="#cost-monitoring-construct.IApplicationCostMonitoringProps">IApplicationCostMonitoringProps</a>

#### Initializers <a name="Initializers" id="cost-monitoring-construct.ApplicationCostMonitoringProps.Initializer"></a>

```typescript
import { ApplicationCostMonitoringProps } from 'cost-monitoring-construct'

new ApplicationCostMonitoringProps(applicationName: string, monthlyLimitInDollars: number, otherStacksIncludedInBudget?: Stack[], defaultTopic?: string, subscribers?: string[], costAllocationTag?: string)
```

| **Name** | **Type** | **Description** |
| --- | --- | --- |
| <code><a href="#cost-monitoring-construct.ApplicationCostMonitoringProps.Initializer.parameter.applicationName">applicationName</a></code> | <code>string</code> | *No description.* |
| <code><a href="#cost-monitoring-construct.ApplicationCostMonitoringProps.Initializer.parameter.monthlyLimitInDollars">monthlyLimitInDollars</a></code> | <code>number</code> | *No description.* |
| <code><a href="#cost-monitoring-construct.ApplicationCostMonitoringProps.Initializer.parameter.otherStacksIncludedInBudget">otherStacksIncludedInBudget</a></code> | <code>aws-cdk-lib.Stack[]</code> | *No description.* |
| <code><a href="#cost-monitoring-construct.ApplicationCostMonitoringProps.Initializer.parameter.defaultTopic">defaultTopic</a></code> | <code>string</code> | *No description.* |
| <code><a href="#cost-monitoring-construct.ApplicationCostMonitoringProps.Initializer.parameter.subscribers">subscribers</a></code> | <code>string[]</code> | *No description.* |
| <code><a href="#cost-monitoring-construct.ApplicationCostMonitoringProps.Initializer.parameter.costAllocationTag">costAllocationTag</a></code> | <code>string</code> | *No description.* |

---

##### `applicationName`<sup>Required</sup> <a name="applicationName" id="cost-monitoring-construct.ApplicationCostMonitoringProps.Initializer.parameter.applicationName"></a>

- *Type:* string

---

##### `monthlyLimitInDollars`<sup>Required</sup> <a name="monthlyLimitInDollars" id="cost-monitoring-construct.ApplicationCostMonitoringProps.Initializer.parameter.monthlyLimitInDollars"></a>

- *Type:* number

---

##### `otherStacksIncludedInBudget`<sup>Optional</sup> <a name="otherStacksIncludedInBudget" id="cost-monitoring-construct.ApplicationCostMonitoringProps.Initializer.parameter.otherStacksIncludedInBudget"></a>

- *Type:* aws-cdk-lib.Stack[]

---

##### `defaultTopic`<sup>Optional</sup> <a name="defaultTopic" id="cost-monitoring-construct.ApplicationCostMonitoringProps.Initializer.parameter.defaultTopic"></a>

- *Type:* string

---

##### `subscribers`<sup>Optional</sup> <a name="subscribers" id="cost-monitoring-construct.ApplicationCostMonitoringProps.Initializer.parameter.subscribers"></a>

- *Type:* string[]

---

##### `costAllocationTag`<sup>Optional</sup> <a name="costAllocationTag" id="cost-monitoring-construct.ApplicationCostMonitoringProps.Initializer.parameter.costAllocationTag"></a>

- *Type:* string

---



#### Properties <a name="Properties" id="Properties"></a>

| **Name** | **Type** | **Description** |
| --- | --- | --- |
| <code><a href="#cost-monitoring-construct.ApplicationCostMonitoringProps.property.applicationName">applicationName</a></code> | <code>string</code> | *No description.* |
| <code><a href="#cost-monitoring-construct.ApplicationCostMonitoringProps.property.monthlyLimitInDollars">monthlyLimitInDollars</a></code> | <code>number</code> | *No description.* |
| <code><a href="#cost-monitoring-construct.ApplicationCostMonitoringProps.property.costAllocationTag">costAllocationTag</a></code> | <code>string</code> | *No description.* |
| <code><a href="#cost-monitoring-construct.ApplicationCostMonitoringProps.property.defaultTopic">defaultTopic</a></code> | <code>string</code> | *No description.* |
| <code><a href="#cost-monitoring-construct.ApplicationCostMonitoringProps.property.otherStacksIncludedInBudget">otherStacksIncludedInBudget</a></code> | <code>aws-cdk-lib.Stack[]</code> | *No description.* |
| <code><a href="#cost-monitoring-construct.ApplicationCostMonitoringProps.property.subscribers">subscribers</a></code> | <code>string[]</code> | *No description.* |

---

##### `applicationName`<sup>Required</sup> <a name="applicationName" id="cost-monitoring-construct.ApplicationCostMonitoringProps.property.applicationName"></a>

```typescript
public readonly applicationName: string;
```

- *Type:* string

---

##### `monthlyLimitInDollars`<sup>Required</sup> <a name="monthlyLimitInDollars" id="cost-monitoring-construct.ApplicationCostMonitoringProps.property.monthlyLimitInDollars"></a>

```typescript
public readonly monthlyLimitInDollars: number;
```

- *Type:* number

---

##### `costAllocationTag`<sup>Optional</sup> <a name="costAllocationTag" id="cost-monitoring-construct.ApplicationCostMonitoringProps.property.costAllocationTag"></a>

```typescript
public readonly costAllocationTag: string;
```

- *Type:* string

---

##### `defaultTopic`<sup>Optional</sup> <a name="defaultTopic" id="cost-monitoring-construct.ApplicationCostMonitoringProps.property.defaultTopic"></a>

```typescript
public readonly defaultTopic: string;
```

- *Type:* string

---

##### `otherStacksIncludedInBudget`<sup>Optional</sup> <a name="otherStacksIncludedInBudget" id="cost-monitoring-construct.ApplicationCostMonitoringProps.property.otherStacksIncludedInBudget"></a>

```typescript
public readonly otherStacksIncludedInBudget: Stack[];
```

- *Type:* aws-cdk-lib.Stack[]

---

##### `subscribers`<sup>Optional</sup> <a name="subscribers" id="cost-monitoring-construct.ApplicationCostMonitoringProps.property.subscribers"></a>

```typescript
public readonly subscribers: string[];
```

- *Type:* string[]

---


### IBudgetStrategy <a name="IBudgetStrategy" id="cost-monitoring-construct.IBudgetStrategy"></a>

#### Initializers <a name="Initializers" id="cost-monitoring-construct.IBudgetStrategy.Initializer"></a>
Expand Down Expand Up @@ -572,7 +707,7 @@ Return default SNS topic only if the defultTopic prop has been passed when insta

- *Extends:* <a href="#cost-monitoring-construct.IBudgetStrategyProps">IBudgetStrategyProps</a>

- *Implemented By:* <a href="#cost-monitoring-construct.IApplicationCostMonitoringProps">IApplicationCostMonitoringProps</a>
- *Implemented By:* <a href="#cost-monitoring-construct.ApplicationCostMonitoringProps">ApplicationCostMonitoringProps</a>, <a href="#cost-monitoring-construct.IApplicationCostMonitoringProps">IApplicationCostMonitoringProps</a>


#### Properties <a name="Properties" id="Properties"></a>
Expand All @@ -583,6 +718,7 @@ Return default SNS topic only if the defultTopic prop has been passed when insta
| <code><a href="#cost-monitoring-construct.IApplicationCostMonitoringProps.property.defaultTopic">defaultTopic</a></code> | <code>string</code> | *No description.* |
| <code><a href="#cost-monitoring-construct.IApplicationCostMonitoringProps.property.subscribers">subscribers</a></code> | <code>string[]</code> | *No description.* |
| <code><a href="#cost-monitoring-construct.IApplicationCostMonitoringProps.property.applicationName">applicationName</a></code> | <code>string</code> | *No description.* |
| <code><a href="#cost-monitoring-construct.IApplicationCostMonitoringProps.property.costAllocationTag">costAllocationTag</a></code> | <code>string</code> | *No description.* |
| <code><a href="#cost-monitoring-construct.IApplicationCostMonitoringProps.property.otherStacksIncludedInBudget">otherStacksIncludedInBudget</a></code> | <code>aws-cdk-lib.Stack[]</code> | *No description.* |

---
Expand Down Expand Up @@ -627,6 +763,16 @@ public readonly applicationName: string;

---

##### `costAllocationTag`<sup>Optional</sup> <a name="costAllocationTag" id="cost-monitoring-construct.IApplicationCostMonitoringProps.property.costAllocationTag"></a>

```typescript
public readonly costAllocationTag: string;
```

- *Type:* string

---

##### `otherStacksIncludedInBudget`<sup>Optional</sup> <a name="otherStacksIncludedInBudget" id="cost-monitoring-construct.IApplicationCostMonitoringProps.property.otherStacksIncludedInBudget"></a>

```typescript
Expand Down Expand Up @@ -762,7 +908,7 @@ public readonly tags: ITag[];

### IBudgetStrategyProps <a name="IBudgetStrategyProps" id="cost-monitoring-construct.IBudgetStrategyProps"></a>

- *Implemented By:* <a href="#cost-monitoring-construct.IApplicationCostMonitoringProps">IApplicationCostMonitoringProps</a>, <a href="#cost-monitoring-construct.IBudgetStrategyProps">IBudgetStrategyProps</a>
- *Implemented By:* <a href="#cost-monitoring-construct.ApplicationCostMonitoringProps">ApplicationCostMonitoringProps</a>, <a href="#cost-monitoring-construct.IApplicationCostMonitoringProps">IApplicationCostMonitoringProps</a>, <a href="#cost-monitoring-construct.IBudgetStrategyProps">IBudgetStrategyProps</a>


#### Properties <a name="Properties" id="Properties"></a>
Expand Down
19 changes: 17 additions & 2 deletions src/application-cost-monitoring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,24 @@ import { TimeUnit } from "./utils";
export interface IApplicationCostMonitoringProps extends IBudgetStrategyProps {
applicationName: string;
otherStacksIncludedInBudget?: Stack[];
costAllocationTag?: string;
}

export class ApplicationCostMonitoringProps implements IApplicationCostMonitoringProps {
constructor(
public applicationName: string,
public monthlyLimitInDollars: number,
public otherStacksIncludedInBudget?: Stack[],
public defaultTopic?: string,
public subscribers?: string[],
public costAllocationTag?: string
) {}
}

export class ApplicationCostMonitoring extends IBudgetStrategy {
readonly applicationName: string;
private otherStacks: Stack[];
private costAllocationTag?: string

/**
* Default Application CostMonitoring class that implements daily and monthly budgets.
Expand All @@ -21,6 +34,7 @@ export class ApplicationCostMonitoring extends IBudgetStrategy {
* @param props.monthlyLimitInDollars - montly limit in US Dollors.
* @param props.defaultTopic - default SNS topic name. Only if provided, the BudgetStratgy creates an SNS topic and send notifications to it.
* @param props.subscribers - list of email address that the CostMonitoring will use to send alerts to.
* @param props.costAllocationTag - Tag key used to track resources' expenditure. Only if provided, it will be used to tag the application resources.
*
* @example tracking budget for an application called `my-application`:
* const app = new cdk.App();
Expand All @@ -41,7 +55,7 @@ export class ApplicationCostMonitoring extends IBudgetStrategy {
* budgetStratgy.monitor();
*
*/
constructor(stack: Stack, props: IApplicationCostMonitoringProps) {
constructor(stack: Stack, props: ApplicationCostMonitoringProps) {
super(stack, props);

if (props.monthlyLimitInDollars < 30) {
Expand All @@ -52,6 +66,7 @@ export class ApplicationCostMonitoring extends IBudgetStrategy {

this.applicationName = props.applicationName;
this.otherStacks = props.otherStacksIncludedInBudget ?? [];
this.costAllocationTag = props.costAllocationTag;
}

protected createDailyBudgets(
Expand Down Expand Up @@ -133,6 +148,6 @@ export class ApplicationCostMonitoring extends IBudgetStrategy {
* Default key name for application tag.
*/
protected get applicationTagKey(): string {
return "cm:application";
return this.costAllocationTag || "cm:application";
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export { AccountCostMonitoring } from "./account-cost-monitoring";
export {
ApplicationCostMonitoring,
ApplicationCostMonitoringProps,
IApplicationCostMonitoringProps,
} from "./application-cost-monitoring";
export * from "./budget-strategy";
Expand Down

0 comments on commit 0732971

Please sign in to comment.