From fcd5b5b396b905e7575dd4f508a0eaf515cc671f Mon Sep 17 00:00:00 2001 From: Veli-Matti Lintu Date: Sun, 14 Jan 2018 23:49:36 +0200 Subject: [PATCH] Add tags also to the EBS volumes when they are created. This follows the same functionality as is already in place for snapshots and AMIs. --- bootstrapvz/providers/ec2/README.rst | 25 +++++++++++++++++++++++++ bootstrapvz/providers/ec2/ebsvolume.py | 8 +++++--- bootstrapvz/providers/ec2/tasks/ebs.py | 10 +++++++++- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/bootstrapvz/providers/ec2/README.rst b/bootstrapvz/providers/ec2/README.rst index 0b13dec..3a6d797 100644 --- a/bootstrapvz/providers/ec2/README.rst +++ b/bootstrapvz/providers/ec2/README.rst @@ -169,6 +169,31 @@ Example: region: us-west-1 +Tags +~~~~ + +EBS volumes, snapshots and AMIs are tagged using AWS resource tags +with the tag names and values defined in the manifest. Tags can be used to +categorize AWS resources, e.g. by purpose or environment. They can also be +used to limit access to resources using `IAM policies`__. + +__ https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_examples_ec2_ebs-owner.html + +Example: + +.. code-block:: yaml + + --- + tags: + Name: "Stretch 9.0 alpha" + Debian: "9.0~{%Y}{%m}{%d}{%H}{%M}" + Role: "test" + +Restrictions on tag names and values are defined in `EC2 docs`__. + +__ https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_Tags.html#tag-restrictions + + Dependencies ------------ diff --git a/bootstrapvz/providers/ec2/ebsvolume.py b/bootstrapvz/providers/ec2/ebsvolume.py index a3aa8d8..33e60e2 100644 --- a/bootstrapvz/providers/ec2/ebsvolume.py +++ b/bootstrapvz/providers/ec2/ebsvolume.py @@ -4,16 +4,18 @@ from bootstrapvz.base.fs.exceptions import VolumeError class EBSVolume(Volume): - def create(self, conn, zone): - self.fsm.create(connection=conn, zone=zone) + def create(self, conn, zone, tags=[]): + self.fsm.create(connection=conn, zone=zone, tags=tags) def _before_create(self, e): self.conn = e.connection zone = e.zone + tags = e.tags size = self.size.bytes.get_qty_in('GiB') self.volume = self.conn.create_volume(Size=size, AvailabilityZone=zone, - VolumeType='gp2') + VolumeType='gp2', + TagSpecifications=[{'ResourceType': 'volume', 'Tags': tags}]) self.vol_id = self.volume['VolumeId'] waiter = self.conn.get_waiter('volume_available') waiter.wait(VolumeIds=[self.vol_id], diff --git a/bootstrapvz/providers/ec2/tasks/ebs.py b/bootstrapvz/providers/ec2/tasks/ebs.py index 5efd805..c87a76e 100644 --- a/bootstrapvz/providers/ec2/tasks/ebs.py +++ b/bootstrapvz/providers/ec2/tasks/ebs.py @@ -8,7 +8,15 @@ class Create(Task): @classmethod def run(cls, info): - info.volume.create(info._ec2['connection'], info._ec2['host']['availabilityZone']) + tags = [] + + # Setting up tags on the EBS volume + if 'tags' in info.manifest.data: + raw_tags = info.manifest.data['tags'] + formatted_tags = {k: v.format(**info.manifest_vars) for k, v in raw_tags.items()} + tags = [{'Key': k, 'Value': v} for k, v in formatted_tags.items()] + + info.volume.create(info._ec2['connection'], info._ec2['host']['availabilityZone'], tags) class Attach(Task):