Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AWS policy pack - Tagging Helpers for Cost_Center. Closes #865 #867

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
---
categories: ["storage", "tagging"]
primary_category: "tagging"
---

# Enforce Cost Center Tag Transform for AWS S3 Buckets

Enforcing the standardization of the `Cost_Center` tag is essential for maintaining consistency across AWS S3 Buckets. This policy ensures that variations in the tag format are automatically corrected, improving the organization of resources and supporting accurate cost allocation, budgeting, and compliance with financial governance policies.

This [policy pack](https://turbot.com/guardrails/docs/concepts/policy-packs) can help you configure the following settings for S3 buckets:

- Enforce `Cost_Center` tag key

- **[Review Policy settings →](https://hub.guardrails.turbot.com/policy-packs/aws_s3_enforce_cost_center_tag_transform_for_buckets/settings)**

## Getting Started

### Requirements

- [Terraform](https://developer.hashicorp.com/terraform/install)
- Guardrails mods:
- [@turbot/aws-s3](https://hub.guardrails.turbot.com/mods/aws/mods/aws-s3)

### Credentials

To create a policy pack through Terraform:

- Ensure you have `Turbot/Admin` permissions (or higher) in Guardrails
- [Create access keys](https://turbot.com/guardrails/docs/guides/iam/access-keys#generate-a-new-guardrails-api-access-key) in Guardrails

And then set your credentials:

```sh
export TURBOT_WORKSPACE=myworkspace.acme.com
export TURBOT_ACCESS_KEY=acce6ac5-access-key-here
export TURBOT_SECRET_KEY=a8af61ec-secret-key-here
```

Please see [Turbot Guardrails Provider authentication](https://registry.terraform.io/providers/turbot/turbot/latest/docs#authentication) for additional authentication methods.

## Usage

### Install Policy Pack

> [!NOTE]
> By default, installed policy packs are not attached to any resources.
>
> Policy packs must be attached to resources in order for their policy settings to take effect.

Clone:

```sh
git clone https://github.com/turbot/guardrails-samples.git
cd guardrails-samples/policy_packs/aws/s3/enforce_cost_center_tag_transform_for_buckets
```

Run the Terraform to create the policy pack in your workspace:

```sh
terraform init
terraform plan
```

Then apply the changes:

```sh
terraform apply
```

### Apply Policy Pack

Log into your Guardrails workspace and [attach the policy pack to a resource](https://turbot.com/guardrails/docs/guides/policy-packs#attach-a-policy-pack-to-a-resource).

If this policy pack is attached to a Guardrails folder, its policies will be applied to all accounts and resources in that folder. The policy pack can also be attached to multiple resources.

For more information, please see [Policy Packs](https://turbot.com/guardrails/docs/concepts/policy-packs).

### Enable Enforcement

> [!TIP]
> You can also update the policy settings in this policy pack directly in the Guardrails console.
>
> Please note your Terraform state file will then become out of sync and the policy settings should then only be managed in the console.

By default, the policies are set to `Check` in the pack's policy settings. To enable automated enforcements, you can switch these policies settings by adding a comment to the `Check` setting and removing the comment from one of the listed enforcement options:

```hcl
resource "turbot_policy_setting" "aws_s3_bucket_tags" {
resource = turbot_policy_pack.main.id
type = "tmod:@turbot/aws-s3#/policy/types/bucketTags"
# value = "Check: Tags are correct"
value = "Enforce: Set tags"
}
```

Then re-apply the changes:

```sh
terraform plan
terraform apply
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "turbot_policy_pack" "main" {
title = "Enforce Cost Center Tag Transform for AWS S3 Buckets"
description = "Enforcing a consistent format for the `Cost_Center` tag key ensures that all tags are uniform, helping to keep resources organized and making tracking and management easier."
akas = ["aws_s3_enforce_cost_center_tag_transform_for_buckets"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
locals {
yaml_string = <<-EOT
Cost_Center:
incorrectKeys:
- /.*cost.*cent.*/gi
replacementValue: undefined
EOT
}

# Turbot > File
resource "turbot_file" "aws_tag_transform_rules" {
parent = "tmod:@turbot/turbot#/"
title = "AWS Tag Transform Rules"
akas = ["aws_tag_transform_rules"]
content = jsonencode(yamldecode(local.yaml_string))
}

# AWS > S3 > Bucket > Tags
resource "turbot_policy_setting" "aws_s3_bucket_tags" {
resource = turbot_policy_pack.main.id
type = "tmod:@turbot/aws-s3#/policy/types/bucketTags"
value = "Check: Tags are correct"
# value = "Enforce: Set tags"
}

# AWS > S3 > Bucket > Tags > Template
resource "turbot_policy_setting" "aws_s3_bucket_tags_template" {
resource = turbot_policy_pack.main.id
type = "tmod:@turbot/aws-s3#/policy/types/bucketTagsTemplate"
template_input = <<-EOT
{
rules: resource(id:"aws_tag_transform_rules") {
data
}
resource {
turbot {
tags
}
}
}
EOT
template = <<-EOT
{%- set tags_map = $.resource.turbot.tags -%}
{%- set rules = $.rules.data -%}

{% for key,value in transformMap(tags_map, rules) -%}
- "{{key}}": "{{value}}"
{% endfor -%}

EOT
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
terraform {
required_providers {
turbot = {
source = "turbot/turbot"
version = ">= 1.11.0"
}
}
}

provider "turbot" {
}