Skip to content
This repository has been archived by the owner on May 18, 2024. It is now read-only.

imdsv2 and tags - fixes #61

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions bin/create-ebs-volume
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,11 @@ function create_and_attach_volume() {
local availability_zone=$(get_metadata placement/availability-zone)
local region=${availability_zone%?}

local instance_tags=""
# Render instance tags to match: --tag-specification
# Output Example:
# {Key=Name,Value=Jenkins},{Key=Owner,Value=DevOps}
instance_tags=$(
# Find all non aws: instance tags
local instance_tags=$(
aws ec2 describe-tags \
--region $region \
--filters "Name=resource-id,Values=$instance_id" | jq -r .Tags | jq -c 'map({Key, Value})' | tr -d '[]"' | sed 's/{Key:/{Key=/g ; s/,Value:/,Value=/g ; s/{Key=aws:[^}]*}//g ; s/,\{2,\}/,/g ; s/,$//g ; s/^,//g'
--filters "Name=resource-id,Values=$instance_id" | jq -rc '.Tags | map({Key,Value} | select(.Key | test("^aws:") | not))'
)

local max_attempts=10
Expand Down Expand Up @@ -269,12 +266,8 @@ function create_and_attach_volume() {
local volume=""
for i in $(eval echo "{0..$max_attempts}") ; do

# The $instance_tags variable could be empty and will cause a TagSpecifications[0].Tags[0] error if
# it is passed as an empty value because it must be comma-separated from the other key-value pairs.
# Use a Shell Parameter Expansion to determine if the variable contains a value or not. If it has a value,
# append a comma at the end so the aws cli syntax is compliant when it is subbed into the tag_specification variable.
local instance_tags=${instance_tags:+${instance_tags},}
local tag_specification="ResourceType=volume,Tags=[$instance_tags{Key=source-instance,Value=$instance_id},{Key=amazon-ebs-autoscale-creation-time,Value=$timestamp}]"
local ebs_autoscale_tags='[{"Key":"source-instance","Value":"'$instance_id'"},{"Key":"amazon-ebs-autoscale-creation-time","Value":"'$timestamp'"}]'
local tag_specifications='[{"ResourceType":"volume","Tags":'$(jq -sc 'add' <(echo "$ebs_autoscale_tags") <(echo "$instance_tags"))'}]'

# Note: Shellcheck says the $vars in this command should be double quoted to prevent globbing and word-splitting,
# but this ends up making the '--encrypted' argument to fail during the execution of the install script. Conversely, NOT putting double-quotes
Expand All @@ -285,7 +278,7 @@ function create_and_attach_volume() {
--region $region \
--availability-zone $availability_zone \
$volume_opts \
--tag-specification "$tag_specification" \
--tag-specifications "$tag_specifications" \
2> $tmpfile
)

Expand Down
1 change: 1 addition & 0 deletions config/ebs-autoscale.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"mountpoint": "%%MOUNTPOINT%%",
"filesystem": "%%FILESYSTEM%%",
"imdsv2": "%%IMDSV2%%",
"lvm": {
"volume_group": "autoscale_vg",
"logical_volume": "autoscale_lv"
Expand Down
29 changes: 15 additions & 14 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,21 @@ done

eval set -- "$PARAMS"

# install default config
cat ${BASEDIR}/config/ebs-autoscale.json | \
sed -e "s#%%MOUNTPOINT%%#${MOUNTPOINT}#" | \
sed -e "s#%%VOLUMETYPE%%#${VOLUMETYPE}#" | \
sed -e "s#%%VOLUMEIOPS%%#${VOLUMEIOPS}#" | \
sed -e "s#%%VOLUMETHOUGHPUT%%#${VOLUMETHOUGHPUT}#" | \
sed -e "s#%%FILESYSTEM%%#${FILE_SYSTEM}#" | \
sed -e "s#%%IMDSV2%%#${IMDSV2}#" | \
sed -e "s#%%MINEBSVOLUMESIZE%%#${MIN_EBS_VOLUME_SIZE}#" | \
sed -e "s#%%MAXEBSVOLUMESIZE%%#${MAX_EBS_VOLUME_SIZE}#" | \
sed -e "s#%%MAXLOGICALVOLUMESIZE%%#${MAX_LOGICAL_VOLUME_SIZE}#" | \
sed -e "s#%%MAXATTACHEDVOLUMES%%#${MAX_ATTACHED_VOLUMES}#" | \
sed -e "s#%%INITIALUTILIZATIONTHRESHOLD%%#${INITIAL_UTILIZATION_THRESHOLD}#" \
> /etc/ebs-autoscale.json

initialize

# for backwards compatibility evaluate positional parameters like previous 2.0.x and 2.1.x releases
Expand Down Expand Up @@ -214,20 +229,6 @@ cp ${BASEDIR}/shared/utils.sh /usr/local/amazon-ebs-autoscale/shared
# install the logrotate config
cp ${BASEDIR}/config/ebs-autoscale.logrotate /etc/logrotate.d/ebs-autoscale

# install default config
cat ${BASEDIR}/config/ebs-autoscale.json | \
sed -e "s#%%MOUNTPOINT%%#${MOUNTPOINT}#" | \
sed -e "s#%%VOLUMETYPE%%#${VOLUMETYPE}#" | \
sed -e "s#%%VOLUMEIOPS%%#${VOLUMEIOPS}#" | \
sed -e "s#%%VOLUMETHOUGHPUT%%#${VOLUMETHOUGHPUT}#" | \
sed -e "s#%%FILESYSTEM%%#${FILE_SYSTEM}#" | \
sed -e "s#%%MINEBSVOLUMESIZE%%#${MIN_EBS_VOLUME_SIZE}#" | \
sed -e "s#%%MAXEBSVOLUMESIZE%%#${MAX_EBS_VOLUME_SIZE}#" | \
sed -e "s#%%MAXLOGICALVOLUMESIZE%%#${MAX_LOGICAL_VOLUME_SIZE}#" | \
sed -e "s#%%MAXATTACHEDVOLUMES%%#${MAX_ATTACHED_VOLUMES}#" | \
sed -e "s#%%INITIALUTILIZATIONTHRESHOLD%%#${INITIAL_UTILIZATION_THRESHOLD}#" \
> /etc/ebs-autoscale.json

## Create filesystem
if [ -e $MOUNTPOINT ] && ! [ -d $MOUNTPOINT ]; then
echo "ERROR: $MOUNTPOINT exists but is not a directory."
Expand Down
6 changes: 3 additions & 3 deletions shared/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@ function get_metadata() {
local key=$1
local metadata_ip='169.254.169.254'

if [ ! -z "$IMDSV2" ]; then
if [ ! -z "$(get_config_value .imdsv2)" ]; then
local token=$(curl -s -X PUT "http://$metadata_ip/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 60")
local token_wrapper='-H "X-aws-ec2-metadata-token: $token"'
Copy link

@nathanthorpe nathanthorpe Apr 4, 2023

Choose a reason for hiding this comment

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

I'm testing this locally and it doesn't expand out properly when using single quotes.

> token_wrapper='-H "X-aws-ec2-metadata-token: $token"'
> echo $token_wrapper
-H "X-aws-ec2-metadata-token: $token"

Changing it to this works

> token_wrapper='-H "X-aws-ec2-metadata-token: '"$token"'"'
> echo $token_wrapper
-H "X-aws-ec2-metadata-token: <snip>"

The AMI I'm using is ami-0c6ee50d15e7364d4

This shows up in cloudwatch

image

Choose a reason for hiding this comment

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

Nevermind, I see that eval fixes it...

fi

echo `curl -s $token_wrapper http://$metadata_ip/latest/meta-data/$key`
eval "curl -s $token_wrapper http://$metadata_ip/latest/meta-data/$key"
}

function initialize() {
export EBS_AUTOSCALE_CONFIG_FILE=/etc/ebs-autoscale.json
export AWS_AZ=$(get_metadata placement/availability-zone)
export AWS_REGION=$(echo ${AWS_AZ} | sed -e 's/[a-z]$//')
export INSTANCE_ID=$(get_metadata instance-id)
export EBS_AUTOSCALE_CONFIG_FILE=/etc/ebs-autoscale.json
}

function detect_init_system() {
Expand Down