Skip to content

Commit 8aa976a

Browse files
authored
initial implementation (#3)
1 parent 3acc998 commit 8aa976a

File tree

16 files changed

+615
-102
lines changed

16 files changed

+615
-102
lines changed

README.md

Lines changed: 78 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
2929
-->
3030

31-
Terraform module to manage [AWS Budgets](https://docs.aws.amazon.com/cost-management/latest/userguide/budgets-managing-costs.html).
31+
Terraform module to create [AWS Budgets](https://docs.aws.amazon.com/cost-management/latest/userguide/budgets-managing-costs.html) and an associated SNS topic and Lambda function to send notifications to Slack.
3232

3333
---
3434

@@ -109,11 +109,60 @@ module "label" {
109109
name = "example"
110110
}
111111
112-
module "example" {
112+
locals {
113+
budgets = [
114+
{
115+
name = "budget-ec2-monthly"
116+
budget_type = "COST"
117+
limit_amount = "1200"
118+
limit_unit = "USD"
119+
time_period_end = "2087-06-15_00:00"
120+
time_unit = "MONTHLY"
121+
122+
cost_filter = {
123+
Service = ["Amazon Elastic Compute Cloud - Compute"]
124+
}
125+
126+
notification = {
127+
comparison_operator = "GREATER_THAN"
128+
threshold = "100"
129+
threshold_type = "PERCENTAGE"
130+
notification_type = "FORECASTED"
131+
}
132+
},
133+
{
134+
name = "100-total-monthly"
135+
budget_type = "COST"
136+
limit_amount = "100"
137+
limit_unit = "USD"
138+
time_unit = "MONTHLY"
139+
},
140+
{
141+
name = "s3-3GB-limit-monthly"
142+
budget_type = "USAGE"
143+
limit_amount = "3"
144+
limit_unit = "GB"
145+
time_unit = "MONTHLY"
146+
}
147+
]
148+
}
149+
150+
module "budgets" {
113151
source = "cloudposse/budgets/aws"
114152
# Cloud Posse recommends pinning every module to a specific version
115153
# version = "x.x.x"
116154
155+
budgets = local.budgets
156+
157+
# create an SNS topic and lambda for Slack notifications
158+
notifications_enabled = true
159+
slack_webhook_url = "https://slack-webhook-url"
160+
slack_username = "AWS Budgets"
161+
slack_channel = "notifications"
162+
163+
# encrypt SNS topic, this also creates a KMS CMK that allows `budgets.amazonaws.com` to use it
164+
encryption_enabled = true
165+
117166
context = module.label.this
118167
}
119168
```
@@ -146,46 +195,59 @@ Available targets:
146195
| Name | Version |
147196
|------|---------|
148197
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
149-
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 2.2 |
198+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.0 |
150199

151200
## Providers
152201

153202
| Name | Version |
154203
|------|---------|
155-
| <a name="provider_random"></a> [random](#provider\_random) | >= 2.2 |
204+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.0 |
156205

157206
## Modules
158207

159208
| Name | Source | Version |
160209
|------|--------|---------|
210+
| <a name="module_kms_key"></a> [kms\_key](#module\_kms\_key) | cloudposse/kms-key/aws | 0.12.1 |
211+
| <a name="module_slack_notify_lambda"></a> [slack\_notify\_lambda](#module\_slack\_notify\_lambda) | cloudposse/sns-lambda-notify-slack/aws | 0.5.6 |
212+
| <a name="module_sns_topic"></a> [sns\_topic](#module\_sns\_topic) | cloudposse/sns-topic/aws | 0.20.1 |
161213
| <a name="module_this"></a> [this](#module\_this) | cloudposse/label/null | 0.25.0 |
162214

163215
## Resources
164216

165217
| Name | Type |
166218
|------|------|
167-
| [random_integer.example](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/integer) | resource |
219+
| [aws_budgets_budget.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/budgets_budget) | resource |
220+
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
221+
| [aws_iam_policy_document.kms_key_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
168222

169223
## Inputs
170224

171225
| Name | Description | Type | Default | Required |
172226
|------|-------------|------|---------|:--------:|
173227
| <a name="input_additional_tag_map"></a> [additional\_tag\_map](#input\_additional\_tag\_map) | Additional key-value pairs to add to each map in `tags_as_list_of_maps`. Not added to `tags` or `id`.<br>This is for some rare cases where resources want additional configuration of tags<br>and therefore take a list of maps with tag key, value, and additional configuration. | `map(string)` | `{}` | no |
174228
| <a name="input_attributes"></a> [attributes](#input\_attributes) | ID element. Additional attributes (e.g. `workers` or `cluster`) to add to `id`,<br>in the order they appear in the list. New attributes are appended to the<br>end of the list. The elements of the list are joined by the `delimiter`<br>and treated as a single ID element. | `list(string)` | `[]` | no |
229+
| <a name="input_budgets"></a> [budgets](#input\_budgets) | A list of Budgets to be managed by this module, see https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/budgets_budget#argument-reference<br>for a list of possible attributes. For a more specific example, see `examples/complete/fixtures.us-east-2.tfvars` in this repository. | `any` | `[]` | no |
175230
| <a name="input_context"></a> [context](#input\_context) | Single object for setting entire context at once.<br>See description of individual variables for details.<br>Leave string and numeric variables as `null` to use default value.<br>Individual variable settings (non-null) override settings in context object,<br>except for attributes, tags, and additional\_tag\_map, which are merged. | `any` | <pre>{<br> "additional_tag_map": {},<br> "attributes": [],<br> "delimiter": null,<br> "descriptor_formats": {},<br> "enabled": true,<br> "environment": null,<br> "id_length_limit": null,<br> "label_key_case": null,<br> "label_order": [],<br> "label_value_case": null,<br> "labels_as_tags": [<br> "unset"<br> ],<br> "name": null,<br> "namespace": null,<br> "regex_replace_chars": null,<br> "stage": null,<br> "tags": {},<br> "tenant": null<br>}</pre> | no |
176231
| <a name="input_delimiter"></a> [delimiter](#input\_delimiter) | Delimiter to be used between ID elements.<br>Defaults to `-` (hyphen). Set to `""` to use no delimiter at all. | `string` | `null` | no |
177232
| <a name="input_descriptor_formats"></a> [descriptor\_formats](#input\_descriptor\_formats) | Describe additional descriptors to be output in the `descriptors` output map.<br>Map of maps. Keys are names of descriptors. Values are maps of the form<br>`{<br> format = string<br> labels = list(string)<br>}`<br>(Type is `any` so the map values can later be enhanced to provide additional options.)<br>`format` is a Terraform format string to be passed to the `format()` function.<br>`labels` is a list of labels, in order, to pass to `format()` function.<br>Label values will be normalized before being passed to `format()` so they will be<br>identical to how they appear in `id`.<br>Default is `{}` (`descriptors` output will be empty). | `any` | `{}` | no |
178233
| <a name="input_enabled"></a> [enabled](#input\_enabled) | Set to false to prevent the module from creating any resources | `bool` | `null` | no |
234+
| <a name="input_encryption_enabled"></a> [encryption\_enabled](#input\_encryption\_enabled) | Whether or not to use encryption. If set to `true` and no custom value for KMS key (kms\_master\_key\_id) is provided, a KMS key is created. | `bool` | `true` | no |
179235
| <a name="input_environment"></a> [environment](#input\_environment) | ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT' | `string` | `null` | no |
180-
| <a name="input_example"></a> [example](#input\_example) | Example variable | `string` | `"hello world"` | no |
181236
| <a name="input_id_length_limit"></a> [id\_length\_limit](#input\_id\_length\_limit) | Limit `id` to this many characters (minimum 6).<br>Set to `0` for unlimited length.<br>Set to `null` for keep the existing setting, which defaults to `0`.<br>Does not affect `id_full`. | `number` | `null` | no |
237+
| <a name="input_kms_enable_key_rotation"></a> [kms\_enable\_key\_rotation](#input\_kms\_enable\_key\_rotation) | Specifies whether key rotation is enabled | `bool` | `true` | no |
238+
| <a name="input_kms_key_deletion_window_in_days"></a> [kms\_key\_deletion\_window\_in\_days](#input\_kms\_key\_deletion\_window\_in\_days) | Duration in days after which the key is deleted after destruction of the resources | `number` | `7` | no |
239+
| <a name="input_kms_master_key_id"></a> [kms\_master\_key\_id](#input\_kms\_master\_key\_id) | The ID of a KMS CMK to be used for encryption (see https://docs.aws.amazon.com/cost-management/latest/userguide/budgets-sns-policy.html#protect-sns-sse for appropriate key policies). | `string` | `null` | no |
182240
| <a name="input_label_key_case"></a> [label\_key\_case](#input\_label\_key\_case) | Controls the letter case of the `tags` keys (label names) for tags generated by this module.<br>Does not affect keys of tags passed in via the `tags` input.<br>Possible values: `lower`, `title`, `upper`.<br>Default value: `title`. | `string` | `null` | no |
183241
| <a name="input_label_order"></a> [label\_order](#input\_label\_order) | The order in which the labels (ID elements) appear in the `id`.<br>Defaults to ["namespace", "environment", "stage", "name", "attributes"].<br>You can omit any of the 6 labels ("tenant" is the 6th), but at least one must be present. | `list(string)` | `null` | no |
184242
| <a name="input_label_value_case"></a> [label\_value\_case](#input\_label\_value\_case) | Controls the letter case of ID elements (labels) as included in `id`,<br>set as tag values, and output by this module individually.<br>Does not affect values of tags passed in via the `tags` input.<br>Possible values: `lower`, `title`, `upper` and `none` (no transformation).<br>Set this to `title` and set `delimiter` to `""` to yield Pascal Case IDs.<br>Default value: `lower`. | `string` | `null` | no |
185243
| <a name="input_labels_as_tags"></a> [labels\_as\_tags](#input\_labels\_as\_tags) | Set of labels (ID elements) to include as tags in the `tags` output.<br>Default is to include all labels.<br>Tags with empty values will not be included in the `tags` output.<br>Set to `[]` to suppress all generated tags.<br>**Notes:**<br> The value of the `name` tag, if included, will be the `id`, not the `name`.<br> Unlike other `null-label` inputs, the initial setting of `labels_as_tags` cannot be<br> changed in later chained modules. Attempts to change it will be silently ignored. | `set(string)` | <pre>[<br> "default"<br>]</pre> | no |
186244
| <a name="input_name"></a> [name](#input\_name) | ID element. Usually the component or solution name, e.g. 'app' or 'jenkins'.<br>This is the only ID element not also included as a `tag`.<br>The "name" tag is set to the full `id` string. There is no tag with the value of the `name` input. | `string` | `null` | no |
187245
| <a name="input_namespace"></a> [namespace](#input\_namespace) | ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique | `string` | `null` | no |
246+
| <a name="input_notifications_enabled"></a> [notifications\_enabled](#input\_notifications\_enabled) | Whether or not to setup Slack notifications. Set to `true` to create an SNS topic and Lambda function to send alerts to Slack. | `bool` | `false` | no |
188247
| <a name="input_regex_replace_chars"></a> [regex\_replace\_chars](#input\_regex\_replace\_chars) | Terraform regular expression (regex) string.<br>Characters matching the regex will be removed from the ID elements.<br>If not set, `"/[^a-zA-Z0-9-]/"` is used to remove all characters other than hyphens, letters and digits. | `string` | `null` | no |
248+
| <a name="input_slack_channel"></a> [slack\_channel](#input\_slack\_channel) | The name of the channel in Slack for notifications. Only used when `notifications_enabled` is `true` | `string` | `""` | no |
249+
| <a name="input_slack_username"></a> [slack\_username](#input\_slack\_username) | The username that will appear on Slack messages. Only used when `notifications_enabled` is `true` | `string` | `""` | no |
250+
| <a name="input_slack_webhook_url"></a> [slack\_webhook\_url](#input\_slack\_webhook\_url) | The URL of Slack webhook. Only used when `notifications_enabled` is `true` | `string` | `""` | no |
189251
| <a name="input_stage"></a> [stage](#input\_stage) | ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release' | `string` | `null` | no |
190252
| <a name="input_tags"></a> [tags](#input\_tags) | Additional tags (e.g. `{'BusinessUnit': 'XYZ'}`).<br>Neither the tag keys nor the tag values will be modified by this module. | `map(string)` | `{}` | no |
191253
| <a name="input_tenant"></a> [tenant](#input\_tenant) | ID element \_(Rarely used, not included by default)\_. A customer identifier, indicating who this instance of a resource is for | `string` | `null` | no |
@@ -194,9 +256,16 @@ Available targets:
194256

195257
| Name | Description |
196258
|------|-------------|
197-
| <a name="output_example"></a> [example](#output\_example) | Example output |
198-
| <a name="output_id"></a> [id](#output\_id) | ID of the created example |
199-
| <a name="output_random"></a> [random](#output\_random) | Stable random number for this example |
259+
| <a name="output_budgets"></a> [budgets](#output\_budgets) | List of Budgets that are being managed by this module |
260+
| <a name="output_kms_key_arn"></a> [kms\_key\_arn](#output\_kms\_key\_arn) | ARN of the KMS CMK that was created specifically for budget notifications |
261+
| <a name="output_kms_key_id"></a> [kms\_key\_id](#output\_kms\_key\_id) | ID of the KMS CMK that is used for SNS notifications |
262+
| <a name="output_lambda_cloudwatch_log_group_arn"></a> [lambda\_cloudwatch\_log\_group\_arn](#output\_lambda\_cloudwatch\_log\_group\_arn) | The ARN of the Log Group used by the Slack Notify Lambda |
263+
| <a name="output_lambda_function_arn"></a> [lambda\_function\_arn](#output\_lambda\_function\_arn) | The ARN of the Lambda function |
264+
| <a name="output_lambda_function_invoke_arn"></a> [lambda\_function\_invoke\_arn](#output\_lambda\_function\_invoke\_arn) | The ARN to be used for invoking lambda function from API Gateway |
265+
| <a name="output_lambda_function_name"></a> [lambda\_function\_name](#output\_lambda\_function\_name) | The name of the Lambda function |
266+
| <a name="output_lambda_iam_role_arn"></a> [lambda\_iam\_role\_arn](#output\_lambda\_iam\_role\_arn) | The ARN of the IAM role used by Lambda function |
267+
| <a name="output_sns_topic_arn"></a> [sns\_topic\_arn](#output\_sns\_topic\_arn) | ARN of the SNS topic created for notifications |
268+
| <a name="output_sns_topic_name"></a> [sns\_topic\_name](#output\_sns\_topic\_name) | The name of the SNS topic created for notifications |
200269
<!-- markdownlint-restore -->
201270

202271

README.yaml

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ references:
4848

4949
# Short description of this project
5050
description: |-
51-
Terraform module to manage [AWS Budgets](https://docs.aws.amazon.com/cost-management/latest/userguide/budgets-managing-costs.html).
51+
Terraform module to create [AWS Budgets](https://docs.aws.amazon.com/cost-management/latest/userguide/budgets-managing-costs.html) and an associated SNS topic and Lambda function to send notifications to Slack.
5252
5353
# Introduction to the project
5454
# introduction: |-
@@ -72,11 +72,60 @@ usage: |-
7272
name = "example"
7373
}
7474
75-
module "example" {
75+
locals {
76+
budgets = [
77+
{
78+
name = "budget-ec2-monthly"
79+
budget_type = "COST"
80+
limit_amount = "1200"
81+
limit_unit = "USD"
82+
time_period_end = "2087-06-15_00:00"
83+
time_unit = "MONTHLY"
84+
85+
cost_filter = {
86+
Service = ["Amazon Elastic Compute Cloud - Compute"]
87+
}
88+
89+
notification = {
90+
comparison_operator = "GREATER_THAN"
91+
threshold = "100"
92+
threshold_type = "PERCENTAGE"
93+
notification_type = "FORECASTED"
94+
}
95+
},
96+
{
97+
name = "100-total-monthly"
98+
budget_type = "COST"
99+
limit_amount = "100"
100+
limit_unit = "USD"
101+
time_unit = "MONTHLY"
102+
},
103+
{
104+
name = "s3-3GB-limit-monthly"
105+
budget_type = "USAGE"
106+
limit_amount = "3"
107+
limit_unit = "GB"
108+
time_unit = "MONTHLY"
109+
}
110+
]
111+
}
112+
113+
module "budgets" {
76114
source = "cloudposse/budgets/aws"
77115
# Cloud Posse recommends pinning every module to a specific version
78116
# version = "x.x.x"
79117
118+
budgets = local.budgets
119+
120+
# create an SNS topic and lambda for Slack notifications
121+
notifications_enabled = true
122+
slack_webhook_url = "https://slack-webhook-url"
123+
slack_username = "AWS Budgets"
124+
slack_channel = "notifications"
125+
126+
# encrypt SNS topic, this also creates a KMS CMK that allows `budgets.amazonaws.com` to use it
127+
encryption_enabled = true
128+
80129
context = module.label.this
81130
}
82131
```

0 commit comments

Comments
 (0)