Skip to content

Commit

Permalink
Terraform workaround for adding tags (when spot). Close #1
Browse files Browse the repository at this point in the history
  • Loading branch information
huogerac committed Apr 1, 2020
1 parent c4f29d2 commit 297ab48
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions {{cookiecutter.devops_folder}}/ec2_userdata.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
#!/bin/bash

export AWS_DEFAULT_REGION="{{ cookiecutter.region }}"

# Update the apt package index:
sudo apt-get update

## Install Ansible requirements
sudo apt-get install -y python-minimal python-pip vim awscli
sudo apt-get install -y cloud-utils jq
sudo pip install docker

{%- if cookiecutter.use_spot_instance != 'n' %}
## Apply tags when spot
INSTANCEID=$(ec2metadata --instance-id)
VOLUMES=$(aws ec2 describe-volumes --filters "Name=attachment.instance-id,Values=$INSTANCEID" | jq '.Volumes[].VolumeId' | tr -d '"')
aws ec2 create-tags --resources $INSTANCEID $VOLUMES --tags "Key=Name,Value={{ cookiecutter.project_name }} Key=Product,Value={{ cookiecutter.tag_product }} Key=Owner,Value={{ cookiecutter.tag_owner }} Key=Environment,Value={{ cookiecutter.tag_environment }} Key=Client,Value={{ cookiecutter.tag_client }}"
{%- endif %}

1 comment on commit 297ab48

@ecarrara
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@huogerac this solution will tag only the ec2 instance it self, you can use the workaround proposed in this comment to tag the volumes too.

Please sign in to comment.