Skip to content
This repository was archived by the owner on Dec 29, 2021. It is now read-only.

Commit f8ef0a7

Browse files
committed
Make tagging function versatile
1 parent 99b9f5d commit f8ef0a7

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

example.tfvars

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
aws_region = "eu-central-1"
1+
aws_region = "eu-east-1"
22

33
# Used for naming related resources
44
cluster_name = "my-minikube"
55

6+
# The Key of the tag which determines resources belonging to the application. All resources with matching tag and its value will be tagged with additional tags.
7+
search_tag_key = "kubernetes.io/cluster/my-minikube"
8+
search_tag_value = "owned"
9+
610
# Tags
711
tags = {
812
Owner = "johndoe"

template/tagging_lambda.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,16 @@
1313

1414
# Tagging configuration generated on ${timestamp}
1515
region = "${aws_region}"
16-
cluster = "${cluster_name}"
1716
setTags = ${tags}
1817

1918
if 'Name' not in setTags.keys():
20-
setTags['Name'] = "${cluster_name}-resource"
19+
setTags['Name'] = "${name}-resource"
2120

2221
# Tag the resources ...
2322
def lambda_handler(event, context):
24-
kubeClusterTagKey = 'kubernetes.io/cluster/' + cluster
25-
kubeClusterTagValue = 'owned'
26-
filter = [{'Name':'tag:' + kubeClusterTagKey, 'Values':[kubeClusterTagValue]}]
23+
searchTagKey = '${var.search_tag_key}'
24+
searchTagValue = '${var.search_tag_value}'
25+
filter = [{'Name':'tag:' + searchTagKey, 'Values':[searchTagValue]}]
2726

2827
ec2 = boto3.resource('ec2', region_name=region)
2928

@@ -101,8 +100,8 @@ def lambda_handler(event, context):
101100
rtg.create_tags(Tags=newTags)
102101

103102
# VPC
104-
# Current setup is VPC == Cluster. Therefore we can tag things through VPC. With shared VPC, which should be avoided.
105-
# The untagged things under the VPC should be anyway related to VPC and not Kubernetes cluster. So if they are only
103+
# Current setup is VPC == Application. Therefore we can tag things through VPC. With shared VPC this should be avoided.
104+
# The untagged things under the VPC should be anyway related to VPC and not application. So if they are only
106105
# here, they are not ours in shared VPC
107106
vpcs = ec2.vpcs.filter(Filters=filter)
108107
for vpc in vpcs:
@@ -137,7 +136,7 @@ def lambda_handler(event, context):
137136
asgs = autoscaling.describe_auto_scaling_groups(MaxRecords=100)
138137
for asg in asgs['AutoScalingGroups']:
139138
for tag in asg['Tags']:
140-
if tag['Key'] == kubeClusterTagKey and tag['Value'] == kubeClusterTagValue:
139+
if tag['Key'] == searchTagKey and tag['Value'] == searchTagValue:
141140
newTags = prepare_new_tags(asg['Tags'])
142141

143142
if len(newTags) > 0:
@@ -155,7 +154,7 @@ def lambda_handler(event, context):
155154
for elb in elbs['LoadBalancerDescriptions']:
156155
tags = loadbalancing.describe_tags(LoadBalancerNames=[elb['LoadBalancerName']])
157156
for tag in tags['TagDescriptions'][0]['Tags']:
158-
if tag['Key'] == kubeClusterTagKey and tag['Value'] == kubeClusterTagValue:
157+
if tag['Key'] == searchTagKey and tag['Value'] == searchTagValue:
159158
newTags = prepare_new_tags(tags['TagDescriptions'][0]['Tags'])
160159
if len(newTags) > 0:
161160
LOGGER.info("Adding tags to load balancer %s", elb['LoadBalancerName'])

variables.tf

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
variable "aws_region" {
22
description = "Region where Cloud Formation is created"
3-
default = "eu-central-1"
3+
default = "eu-east-1"
44
}
55

6-
variable "cluster_name" {
7-
description = "Name of the AWS Minikube cluster - will be used to name all created resources"
6+
variable "name" {
7+
description = "Name of the installation we are tagging - will be used to name all resources unless they already have a `Name` tag."
8+
}
9+
10+
variable "search_tag_key" {
11+
description = "The Key of the tag which determines resources belonging to the application. All resources with matching tag and its value will be tagged with additional tags."
12+
}
13+
14+
variable "search_tag_value" {
15+
description = "The Valur of the tag which determines resources belonging to the application. All resources with matching tag and its value will be tagged with additional tags."
16+
}
17+
18+
variable "name" {
19+
description = "Name of the installation we are tagging - will be used to name all resources unless they already have a `Name` tag."
820
}
921

1022
variable "tags" {

0 commit comments

Comments
 (0)