From 5f57825e2eea98552f2c3912b189bbbd95af832b Mon Sep 17 00:00:00 2001 From: Niall Creech Date: Mon, 22 Jun 2015 13:13:49 +0100 Subject: [PATCH] Automatic tagging of cloudformation and resources with environment name By passing tags while creating our cloudformation stack we can propagate these tags to our created resources, enabling easy separation of environments for billing purposes (closes #12) --- CHANGELOG.md | 3 +++ bootstrap_cfn/cloudformation.py | 5 +++-- bootstrap_cfn/fab_tasks.py | 8 +++++++- setup.py | 2 ++ 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b791c3..bd9d8ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +##Unreleased +* Tag stack and created resources created with environment name + ## Version 0.5.3 [WIP] * Improve message content when cfn_create raises an exception and fails. * Cleanup SSL certificates when cfn_create raises an exception and fails. diff --git a/bootstrap_cfn/cloudformation.py b/bootstrap_cfn/cloudformation.py index 686a3c6..3196972 100644 --- a/bootstrap_cfn/cloudformation.py +++ b/bootstrap_cfn/cloudformation.py @@ -17,10 +17,11 @@ def __init__(self, aws_profile_name, aws_region_name='eu-west-1'): self.aws_region_name = aws_region_name self.conn_cfn = utils.connect_to_aws(boto.cloudformation, self) - def create(self, stack_name, template_body): + def create(self, stack_name, template_body, tags=[]): stack = self.conn_cfn.create_stack(stack_name=stack_name, template_body=template_body, - capabilities=['CAPABILITY_IAM']) + capabilities=['CAPABILITY_IAM'], + tags=tags) return stack def delete(self, stack_name): diff --git a/bootstrap_cfn/fab_tasks.py b/bootstrap_cfn/fab_tasks.py index 1bf3f47..6bea844 100755 --- a/bootstrap_cfn/fab_tasks.py +++ b/bootstrap_cfn/fab_tasks.py @@ -344,7 +344,13 @@ def cfn_create(test=False): # print cfn_config.process() # Inject security groups in stack template and create stacks. try: - stack = cfn.create(stack_name, cfn_config.process()) + # Set a top-level set of tags for the stack, these will propagate + # down so that many of the created resources will be tagged in + # addition. Notable omissions are EC2 volumes and route53 resources + tags = { + "Env": env.environment + } + stack = cfn.create(stack_name, cfn_config.process(), tags=tags) except: # cleanup ssl certificates if any if 'ssl' in cfn_config.data: diff --git a/setup.py b/setup.py index 050c402..c89bfce 100644 --- a/setup.py +++ b/setup.py @@ -25,6 +25,8 @@ setup_requires=[ 'mock>=1.0.1', 'testfixtures>=4.1.2', + 'troposphere>=1.0.0', + 'nose', ], classifiers=[