Skip to content

Commit df5a1cb

Browse files
authored
Merge pull request #7 from timmyeats/dev
Merge dev for new feature
2 parents 0aa2c35 + e6c92e4 commit df5a1cb

17 files changed

Lines changed: 385 additions & 96 deletions

.editorconfig

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# EditorConfig is awesome: https://EditorConfig.org
2+
3+
# top-most EditorConfig file
4+
root = true
5+
6+
# Unix-style newlines with a newline ending every file
7+
[*]
8+
end_of_line = lf
9+
charset = utf-8
10+
insert_final_newline = true
11+
indent_style = space
12+
indent_size = 2
13+
trim_trailing_whitespace = true
14+
15+
# Dockerfile
16+
[{Dockerfile,Dockerfile.template.erb,Dockerfile.sample}]
17+
indent_style = space
18+
indent_size = 4
19+
20+
# Batch Files
21+
[*.{cmd,bat}]
22+
end_of_line = crlf
23+
24+
# Bash Files
25+
[*.sh]
26+
end_of_line = lf
27+
28+
# tsv use tab to separate fields
29+
[*.tsv]
30+
indent_style = tab
31+
32+
# Applies to all Markdown files
33+
[*.{md,mdx}]
34+
trim_trailing_whitespace = false
35+
36+
# Matches multiple files with brace expansion notation
37+
# Set default charset
38+
[*.{js,py}]
39+
charset = utf-8
40+
41+
# Web Files
42+
[*.{htm,html,js,jsm,ts,tsx,cjs,cts,ctsx,mjs,mts,mtsx,css,sass,scss,less,pcss,svg,vue}]
43+
indent_size = 2
44+
45+
# 4 space indentation
46+
[*.py]
47+
indent_style = space
48+
indent_size = 4
49+
max_line_length = 79
50+
51+
# Tab indentation (no size specified)
52+
[Makefile]
53+
indent_style = tab
54+
55+
# Indentation override for all JS under lib directory
56+
[lib/**.js]
57+
indent_style = space
58+
indent_size = 2
59+
60+
# Matches the exact files either package.json or .travis.yml
61+
[{package.json,.travis.yml}]
62+
indent_style = space
63+
indent_size = 2

.pre-commit-config.yaml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
default_language_version:
2+
python: python3.9
3+
repos:
4+
# General
5+
- repo: https://github.com/pre-commit/pre-commit-hooks
6+
rev: v4.5.0
7+
hooks:
8+
- id: detect-aws-credentials
9+
args: [--allow-missing-credentials]
10+
- id: detect-private-key
11+
- id: check-added-large-files
12+
- id: check-merge-conflict
13+
- id: check-ast
14+
- id: check-case-conflict
15+
- id: check-executables-have-shebangs
16+
- id: check-json
17+
- id: check-yaml
18+
- id: check-toml
19+
- id: check-symlinks
20+
- id: check-xml
21+
- id: destroyed-symlinks
22+
- id: end-of-file-fixer
23+
- id: check-byte-order-marker
24+
- id: mixed-line-ending
25+
- id: name-tests-test
26+
- id: requirements-txt-fixer
27+
- id: trailing-whitespace
28+
args: [--markdown-linebreak-ext=md]
29+
- repo: https://github.com/zricethezav/gitleaks
30+
rev: v8.18.1
31+
hooks:
32+
- id: gitleaks
33+
34+
# Python
35+
- repo: https://github.com/psf/black # Can choose between black or autopep8
36+
rev: 24.1.1
37+
hooks:
38+
- id: black
39+
args: [-t, py39]
40+
- repo: https://github.com/asottile/pyupgrade
41+
rev: v3.15.0
42+
hooks:
43+
- id: pyupgrade
44+
args: [--py39-plus]
45+
- repo: https://github.com/asottile/add-trailing-comma
46+
rev: v3.1.0
47+
hooks:
48+
- id: add-trailing-comma
49+
- repo: https://github.com/asottile/reorder_python_imports
50+
rev: v3.1.0
51+
hooks:
52+
- id: reorder-python-imports
53+
54+
# Terraform
55+
- repo: https://github.com/terraform-docs/terraform-docs
56+
rev: v0.17.0
57+
hooks:
58+
- id: terraform-docs-go
59+
args:
60+
["markdown", "table", "--output-file", "README.md", "."]
61+
- repo: https://github.com/antonbabenko/pre-commit-terraform
62+
rev: v1.86.0
63+
hooks:
64+
- id: terraform_fmt
65+
args: [--args=-diff]

README.md

Lines changed: 71 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# AWS Magic Tagging Resources
22

3-
Use the EventBridge to trigger a Lambda function to tag resources in AWS.
3+
This script is designed to automatically tag AWS resources when certain events occur. It is intended to be used as an AWS Lambda function, triggered by AWS EventBridge.
44

55
## Prerequisites
66

@@ -31,6 +31,8 @@ resource_tags = {
3131
- IAM Policy
3232
- Log Group
3333
- EventBridge Rule
34+
- Resource Group
35+
- CloudWatch Dashboard
3436

3537
## How to use this
3638

@@ -62,23 +64,84 @@ Deploy this terraform and verify the resource tags are created.
6264

6365
`sh multi-region-destroy.sh`
6466

67+
## Functionality
68+
69+
1. **Extracting Tag Information**: The `get_tag_information` function extracts tag information from the event, including the source IP address, event time, and user agent. It also calls the `taggers.get_event_time` and `taggers.get_identity_type` functions to get additional tag information.
70+
71+
2. **Adding Tags**: The `lambda_handler` function is the entry point for the Lambda function. It first calls the `get_tag_information` function to get the tag information, then decides which type of AWS resource to add tags to based on the source of the event. For example, if the source of the event is "aws.ec2", it calls the `ec2_tagger.tagger` function to add tags to EC2 resources.
72+
6573

6674
### Support auto-tagging resources
67-
75+
6876
- EC2
6977
- ELB
7078
- RDS
7179
- CloudFront
7280
- Lambda
7381
- SNS
7482
- IAM
83+
- AutoScaling
7584

7685

7786
### Default tags
78-
79-
- Owner
80-
- SourceIP
81-
- UserType
82-
- EventTime
83-
- UserName / RoleName
8487

88+
- Owner
89+
- SourceIP
90+
- UserType
91+
- EventTime
92+
- UserName / RoleName
93+
94+
95+
<!-- BEGIN_TF_DOCS -->
96+
## Requirements
97+
98+
| Name | Version |
99+
|------|---------|
100+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.14 |
101+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | 4.22.0 |
102+
103+
## Providers
104+
105+
| Name | Version |
106+
|------|---------|
107+
| <a name="provider_aws"></a> [aws](#provider\_aws) | 4.22.0 |
108+
109+
## Modules
110+
111+
| Name | Source | Version |
112+
|------|--------|---------|
113+
| <a name="module_lambda"></a> [lambda](#module\_lambda) | terraform-aws-modules/lambda/aws | 3.3.1 |
114+
115+
## Resources
116+
117+
| Name | Type |
118+
|------|------|
119+
| [aws_cloudwatch_dashboard.dashboard](https://registry.terraform.io/providers/hashicorp/aws/4.22.0/docs/resources/cloudwatch_dashboard) | resource |
120+
| [aws_cloudwatch_event_rule.event_rule](https://registry.terraform.io/providers/hashicorp/aws/4.22.0/docs/resources/cloudwatch_event_rule) | resource |
121+
| [aws_cloudwatch_event_target.event_rule](https://registry.terraform.io/providers/hashicorp/aws/4.22.0/docs/resources/cloudwatch_event_target) | resource |
122+
| [aws_iam_policy.lambda_tagging_policy](https://registry.terraform.io/providers/hashicorp/aws/4.22.0/docs/resources/iam_policy) | resource |
123+
| [aws_iam_role.lambda_function_role](https://registry.terraform.io/providers/hashicorp/aws/4.22.0/docs/resources/iam_role) | resource |
124+
| [aws_iam_role_policy_attachment.lambda_basic_policy](https://registry.terraform.io/providers/hashicorp/aws/4.22.0/docs/resources/iam_role_policy_attachment) | resource |
125+
| [aws_iam_role_policy_attachment.lambda_tagging_policy](https://registry.terraform.io/providers/hashicorp/aws/4.22.0/docs/resources/iam_role_policy_attachment) | resource |
126+
| [aws_lambda_permission.event_rule](https://registry.terraform.io/providers/hashicorp/aws/4.22.0/docs/resources/lambda_permission) | resource |
127+
| [aws_resourcegroups_group.resource_group](https://registry.terraform.io/providers/hashicorp/aws/4.22.0/docs/resources/resourcegroups_group) | resource |
128+
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/4.22.0/docs/data-sources/caller_identity) | data source |
129+
| [aws_iam_role.lambda_function_role](https://registry.terraform.io/providers/hashicorp/aws/4.22.0/docs/data-sources/iam_role) | data source |
130+
| [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/4.22.0/docs/data-sources/region) | data source |
131+
132+
## Inputs
133+
134+
| Name | Description | Type | Default | Required |
135+
|------|-------------|------|---------|:--------:|
136+
| <a name="input_aws_region"></a> [aws\_region](#input\_aws\_region) | The AWS region to use | `string` | `"us-east-1"` | no |
137+
| <a name="input_enable_cloudwatch_dashboard"></a> [enable\_cloudwatch\_dashboard](#input\_enable\_cloudwatch\_dashboard) | Enable the CloudWatch Dashboard | `bool` | `false` | no |
138+
| <a name="input_lambda_function_name"></a> [lambda\_function\_name](#input\_lambda\_function\_name) | The name of the Lambda function | `string` | `"AWSAutoTaggingFunction"` | no |
139+
| <a name="input_lambda_function_role_name"></a> [lambda\_function\_role\_name](#input\_lambda\_function\_role\_name) | The name of the Lambda function role | `string` | `"AWSAutoTaggingFunctionRole"` | no |
140+
| <a name="input_resource_tags"></a> [resource\_tags](#input\_resource\_tags) | Tags to apply to resources | `map(string)` | `null` | no |
141+
142+
## Outputs
143+
144+
| Name | Description |
145+
|------|-------------|
146+
| <a name="output_completed_region"></a> [completed\_region](#output\_completed\_region) | The region that the Lambda function was created |
147+
<!-- END_TF_DOCS -->

cw_dashboard.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
locals {
22
dashboard_query = <<-EOT
3-
SOURCE '/aws/lambda/${var.lambda_function_name}'
3+
SOURCE '/aws/lambda/${var.lambda_function_name}'
44
| fields @timestamp, @message
55
| filter @message not like 'START RequestId'
66
| filter @message not like 'REPORT RequestId'
77
| filter @message not like 'END RequestId'
8+
| filter @message not like 'INIT_START'
89
| sort @timestamp desc
910
| limit 100
1011
EOT

eventbridge.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ locals {
3535
service = "IAM"
3636
source = ["aws.iam"]
3737
event_name = ["CreateRole", "CreatePolicy"]
38+
},
39+
{
40+
service = "AutoScaling"
41+
source = ["aws.autoscaling"]
42+
event_name = ["CreateAutoScalingGroup"]
3843
}
3944
]
4045
}

handler/main.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import resource_tagger.autoscaling_tagger as asg_tagger
2+
import resource_tagger.cloudfront_tagger as cf_tagger
13
import resource_tagger.ec2_tagger as ec2_tagger
24
import resource_tagger.elb_tagger as elb_tagger
5+
import resource_tagger.iam_tagger as iam_tagger
6+
import resource_tagger.lambda_tagger as lambda_tagger
37
import resource_tagger.rds_tagger as rds_tagger
4-
import resource_tagger.cloudfront_tagger as cf_tagger
58
import resource_tagger.sns_tagger as sns_tagger
6-
import resource_tagger.lambda_tagger as lambda_tagger
7-
import resource_tagger.iam_tagger as iam_tagger
89
import resource_tagger.taggers as taggers
910

1011

@@ -33,7 +34,7 @@ def lambda_handler(event, context):
3334

3435
elif event["source"] == "aws.rds":
3536
response = rds_tagger.tagger(event, tags)
36-
37+
3738
elif event["source"] == "aws.cloudfront":
3839
response = cf_tagger.tagger(event, tags)
3940

@@ -46,10 +47,13 @@ def lambda_handler(event, context):
4647
elif event["source"] == "aws.iam":
4748
response = iam_tagger.tagger(event, tags)
4849

50+
elif event["source"] == "aws.autoscaling":
51+
response = asg_tagger.tagger(event, tags)
52+
4953
else:
5054
response = "[LOG] No support source found!"
5155
else:
5256
response = "[LOG] No tags found!"
53-
57+
5458
print(response)
5559
return response
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import boto3
2+
3+
from .taggers import changing_tag_to_array
4+
5+
6+
# Create tags for AWS resources
7+
def add_tags_in_resource(tags, resource):
8+
asg_converted_tags = []
9+
converted_tags = changing_tag_to_array(tags)
10+
11+
try:
12+
for tag in converted_tags:
13+
tag["ResourceId"] = resource
14+
tag["ResourceType"] = "auto-scaling-group"
15+
tag["PropagateAtLaunch"] = True
16+
asg_converted_tags.append(tag)
17+
client = boto3.client("autoscaling")
18+
response = client.create_or_update_tags(Tags=asg_converted_tags)
19+
except Exception as e:
20+
response = {"[LOG] Error: ": str(e)}
21+
22+
return response, converted_tags
23+
24+
25+
def tagger(event, tags):
26+
request_parameters = event["detail"]["requestParameters"]
27+
autoscaling_group_name = request_parameters["autoScalingGroupName"]
28+
29+
if autoscaling_group_name is not None:
30+
response, converted_tags = add_tags_in_resource(tags, autoscaling_group_name)
31+
response["autoscaling_group_name"] = autoscaling_group_name
32+
response["converted_tags"] = converted_tags
33+
return response
34+
35+
else:
36+
return "[LOG] No resource id found!"

0 commit comments

Comments
 (0)