From aa831cac183c7fea160d645600f72d5edce0644f Mon Sep 17 00:00:00 2001 From: Michael Kroell Date: Wed, 7 Apr 2021 13:30:31 -0400 Subject: [PATCH 01/25] Updated calc_new_size and config file, confirmed working --- bin/ebs-autoscale | 25 +++++++++++++++++++++---- config/ebs-autoscale.json | 1 + 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/bin/ebs-autoscale b/bin/ebs-autoscale index 17dddb6..e288758 100755 --- a/bin/ebs-autoscale +++ b/bin/ebs-autoscale @@ -32,6 +32,7 @@ initialize +MAX_EBS_VOLUME_SIZE=$(get_config_value .limits.max_ebs_volume_size) MAX_LOGICAL_VOLUME_SIZE=$(get_config_value .limits.max_logical_volume_size) MAX_EBS_VOLUME_COUNT=$(get_config_value .limits.max_ebs_volume_count) @@ -120,13 +121,29 @@ calc_new_size() { local new_size=150 if [ "$num_devices" -ge "4" ] && [ "$num_devices" -le "6" ]; then - new_size=300 + if [ "$MAX_EBS_VOLUME_SIZE" -ge "299" ]; then + new_size=300 + else + new_size=$MAX_EBS_VOLUME_SIZE + fi elif [ "$num_devices" -gt "6" ] && [ "$num_devices" -le "10" ]; then - new_size=1000 + if [ "$MAX_EBS_VOLUME_SIZE" -ge "999" ]; then + new_size=1000 + else + new_size=$MAX_EBS_VOLUME_SIZE + fi elif [ "$num_devices" -gt "10" ]; then - new_size=1500 + if [ "$MAX_EBS_VOLUME_SIZE" -ge "1499" ]; then + new_size=1500 + else + new_size=$MAX_EBS_VOLUME_SIZE + fi else - new_size=150 + if [ "$MAX_EBS_VOLUME_SIZE" -ge "149" ]; then + new_size=150 + else + new_size=$MAX_EBS_VOLUME_SIZE + fi fi echo ${new_size} diff --git a/config/ebs-autoscale.json b/config/ebs-autoscale.json index f20ef99..b5af2fc 100644 --- a/config/ebs-autoscale.json +++ b/config/ebs-autoscale.json @@ -12,6 +12,7 @@ }, "detection_interval": 1, "limits": { + "max_ebs_volume_size": 1500, "max_logical_volume_size": 8000, "max_ebs_volume_count": 16 }, From f64f64efdb2c6ce27db1ade324f5b9ef7a0bd416 Mon Sep 17 00:00:00 2001 From: kmavrommatis Date: Fri, 9 Apr 2021 16:49:26 -0700 Subject: [PATCH 02/25] replaced multiple calls to DescribeVolumes --- bin/ebs-autoscale | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/bin/ebs-autoscale b/bin/ebs-autoscale index e288758..7191a54 100755 --- a/bin/ebs-autoscale +++ b/bin/ebs-autoscale @@ -97,7 +97,7 @@ calc_threshold() { # calculates percent utilization threshold for adding additional ebs volumes # as more ebs volumes are added, the threshold level increases - local num_devices=$(get_num_devices) + local num_devices=$1 local threshold=50 if [ "$num_devices" -ge "4" ] && [ "$num_devices" -le "6" ]; then @@ -116,32 +116,32 @@ calc_threshold() { calc_new_size() { # calculates the size to use for new ebs volumes to expand space # new volume sizes increase as the number of attached volumes increase - - local num_devices=$(get_num_devices) + local num_devices=$1 + #local num_devices=$(get_num_devices) local new_size=150 if [ "$num_devices" -ge "4" ] && [ "$num_devices" -le "6" ]; then if [ "$MAX_EBS_VOLUME_SIZE" -ge "299" ]; then - new_size=300 - else + new_size=300 + else new_size=$MAX_EBS_VOLUME_SIZE fi elif [ "$num_devices" -gt "6" ] && [ "$num_devices" -le "10" ]; then if [ "$MAX_EBS_VOLUME_SIZE" -ge "999" ]; then - new_size=1000 - else + new_size=1000 + else new_size=$MAX_EBS_VOLUME_SIZE fi elif [ "$num_devices" -gt "10" ]; then if [ "$MAX_EBS_VOLUME_SIZE" -ge "1499" ]; then - new_size=1500 - else + new_size=1500 + else new_size=$MAX_EBS_VOLUME_SIZE fi else if [ "$MAX_EBS_VOLUME_SIZE" -ge "149" ]; then - new_size=150 - else + new_size=150 + else new_size=$MAX_EBS_VOLUME_SIZE fi fi @@ -150,7 +150,8 @@ calc_new_size() { } add_space () { - local num_devices=$(get_num_devices) + #local num_devices=$(get_num_devices) + local num_devices=$1 if [ "${num_devices}" -ge "$MAX_EBS_VOLUME_COUNT" ]; then logthis "No more volumes can be safely added." return 0 @@ -158,7 +159,7 @@ add_space () { local curr_size=$(df -BG ${MOUNTPOINT} | grep ${MOUNTPOINT} | awk '{print $2} ' | cut -d'G' -f1) if [ "${curr_size}" -lt "$MAX_LOGICAL_VOLUME_SIZE" ]; then - local vol_size=$(calc_new_size) + local vol_size=$(calc_new_size ${num_devices}) logthis "Extending logical volume ${MOUNTPOINT} by ${vol_size}GB" DEVICE=$(${BASEDIR}/create-ebs-volume --size ${vol_size} --max-attached-volumes ${MAX_EBS_VOLUME_COUNT}) @@ -198,25 +199,29 @@ LOG_COUNT=$LOG_INTERVAL # time in seconds between event loops # keep this low so that rapid increases in utilization are detected DETECTION_INTERVAL=$(get_config_value .detection_interval) - -THRESHOLD=$(calc_threshold) +# get the number of devices once when the script first starts +NUM_DEVICES=$(get_num_devices) +THRESHOLD=$(calc_threshold "${NUM_DEVICES}") while true; do - NUM_DEVICES=$(get_num_devices) + STATS=$(df -BG ${MOUNTPOINT} | grep -v Filesystem) TOTAL_SIZE=$(echo ${STATS} | awk '{print $2}') USED=$(echo ${STATS} | awk '{print $3}') AVAILABLE=$(echo ${STATS} | awk '{print $4}') PCT_UTILIZATION=$(echo ${STATS} | awk '{print $5}' | cut -d"%" -f1 -) if [ $PCT_UTILIZATION -ge "${THRESHOLD}" ]; then + # get number of devices only when we need to add more space + NUM_DEVICES=$(get_num_devices) logthis "LOW DISK (${PCT_UTILIZATION}%): Adding more." - add_space + add_space "$NUM_DEVICES" + NUM_DEVICES=$(expr $NUM_DEVICES + 1 ) + THRESHOLD=$(calc_threshold "$NUM_DEVICES") LOG_COUNT=LOG_INTERVAL fi if [ "${LOG_COUNT}" -ge "${LOG_INTERVAL}" ]; then logthis "Devices ${NUM_DEVICES} : Size ${TOTAL_SIZE} : Used ${USED} : Available ${AVAILABLE} : Used% ${PCT_UTILIZATION}% : Threshold ${THRESHOLD}%" LOG_COUNT=0 fi - THRESHOLD=$(calc_threshold) LOG_COUNT=$(expr $LOG_COUNT + 1 ) sleep $DETECTION_INTERVAL done From 6c5e4744c40994c1f5bb8ebad083fceacde78fe8 Mon Sep 17 00:00:00 2001 From: kmavrommatis Date: Fri, 9 Apr 2021 16:50:14 -0700 Subject: [PATCH 03/25] Check for disk space every 10s instead of 1s --- config/ebs-autoscale.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/ebs-autoscale.json b/config/ebs-autoscale.json index b5af2fc..44f921a 100644 --- a/config/ebs-autoscale.json +++ b/config/ebs-autoscale.json @@ -10,7 +10,7 @@ "iops": 3000, "encrypted": 1 }, - "detection_interval": 1, + "detection_interval": 10, "limits": { "max_ebs_volume_size": 1500, "max_logical_volume_size": 8000, From 1c665f78f08c77ea234af76c5f906d6ffc75487c Mon Sep 17 00:00:00 2001 From: kmavrommatis Date: Mon, 12 Apr 2021 14:01:41 -0700 Subject: [PATCH 04/25] Interval time is set to 2s --- config/ebs-autoscale.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/ebs-autoscale.json b/config/ebs-autoscale.json index 44f921a..1e62c30 100644 --- a/config/ebs-autoscale.json +++ b/config/ebs-autoscale.json @@ -10,7 +10,7 @@ "iops": 3000, "encrypted": 1 }, - "detection_interval": 10, + "detection_interval": 2, "limits": { "max_ebs_volume_size": 1500, "max_logical_volume_size": 8000, From d292a89933a81b5d702d052e04a6a045908b5622 Mon Sep 17 00:00:00 2001 From: Jonas Gresens <5290643+jgresens@users.noreply.github.com> Date: Fri, 16 Apr 2021 17:51:35 +0200 Subject: [PATCH 05/25] Fix variable assignment and clean code * use variable value instead of name * remove trailing whitespace --- bin/ebs-autoscale | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/bin/ebs-autoscale b/bin/ebs-autoscale index e288758..5d3c798 100755 --- a/bin/ebs-autoscale +++ b/bin/ebs-autoscale @@ -116,32 +116,32 @@ calc_threshold() { calc_new_size() { # calculates the size to use for new ebs volumes to expand space # new volume sizes increase as the number of attached volumes increase - + local num_devices=$(get_num_devices) local new_size=150 if [ "$num_devices" -ge "4" ] && [ "$num_devices" -le "6" ]; then if [ "$MAX_EBS_VOLUME_SIZE" -ge "299" ]; then - new_size=300 - else + new_size=300 + else new_size=$MAX_EBS_VOLUME_SIZE fi elif [ "$num_devices" -gt "6" ] && [ "$num_devices" -le "10" ]; then if [ "$MAX_EBS_VOLUME_SIZE" -ge "999" ]; then - new_size=1000 - else + new_size=1000 + else new_size=$MAX_EBS_VOLUME_SIZE fi elif [ "$num_devices" -gt "10" ]; then if [ "$MAX_EBS_VOLUME_SIZE" -ge "1499" ]; then - new_size=1500 - else + new_size=1500 + else new_size=$MAX_EBS_VOLUME_SIZE fi else if [ "$MAX_EBS_VOLUME_SIZE" -ge "149" ]; then - new_size=150 - else + new_size=150 + else new_size=$MAX_EBS_VOLUME_SIZE fi fi @@ -210,7 +210,7 @@ while true; do if [ $PCT_UTILIZATION -ge "${THRESHOLD}" ]; then logthis "LOW DISK (${PCT_UTILIZATION}%): Adding more." add_space - LOG_COUNT=LOG_INTERVAL + LOG_COUNT=$LOG_INTERVAL fi if [ "${LOG_COUNT}" -ge "${LOG_INTERVAL}" ]; then logthis "Devices ${NUM_DEVICES} : Size ${TOTAL_SIZE} : Used ${USED} : Available ${AVAILABLE} : Used% ${PCT_UTILIZATION}% : Threshold ${THRESHOLD}%" From 4a0807753ddcfd65e22c5ba674c2f5bc4f288c2a Mon Sep 17 00:00:00 2001 From: Mark Schreiber Date: Tue, 17 Aug 2021 15:25:55 -0400 Subject: [PATCH 06/25] add a small random jitter --- bin/ebs-autoscale | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/ebs-autoscale b/bin/ebs-autoscale index c7d4092..17494d2 100755 --- a/bin/ebs-autoscale +++ b/bin/ebs-autoscale @@ -87,7 +87,7 @@ get_num_devices() { break fi - sleep $(( 2 ** i )) + sleep $(( 2 ** i + $RANDOM %3 )) done echo "$attached_volumes" From ad3ad4602b47272b2c6107016cde58e7ceb15bad Mon Sep 17 00:00:00 2001 From: Mark Schreiber Date: Tue, 17 Aug 2021 15:30:13 -0400 Subject: [PATCH 07/25] Add random jitter to sleep --- bin/create-ebs-volume | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/bin/create-ebs-volume b/bin/create-ebs-volume index 47840c5..b679731 100755 --- a/bin/create-ebs-volume +++ b/bin/create-ebs-volume @@ -187,7 +187,7 @@ function create_and_attach_volume() { logthis "Could not determine the number of attached_volumes after $i attempts. Last response was: $attached_volumes" break fi - sleep $(( 2 ** i )) + sleep $(( 2 ** i + $RANDOM % 3)) done local created_volumes="" @@ -204,7 +204,7 @@ function create_and_attach_volume() { logthis "Could not determine the number of created_volumes after $i attempts. Last response was: $created_volumes" break fi - sleep $(( 2 ** i )) + sleep $(( 2 ** i + $RANDOM % 3)) done local total_created_size="" @@ -223,7 +223,7 @@ function create_and_attach_volume() { logthis "Could not determine the total_created_size after $i attempts. Last response was: $total_created_size" break fi - sleep $(( 2 ** i )) + sleep $(( 2 ** i + $RANDOM % 3)) done # check how much EBS storage this instance has created @@ -272,7 +272,7 @@ function create_and_attach_volume() { logthis "Could not create a volume after $i attempts. Last response was: $volume" break fi - sleep $(( 2 ** i )) + sleep $(( 2 ** i + $RANDOM % 3)) done local volume_id=`echo $volume | jq -r '.VolumeId'` @@ -341,4 +341,4 @@ function create_and_attach_volume() { echo $device } -create_and_attach_volume \ No newline at end of file +create_and_attach_volume From 95af98803bbf3c085d90d6d48a2fda0cdabebd41 Mon Sep 17 00:00:00 2001 From: Gil Freund Date: Thu, 23 Sep 2021 11:44:01 +0000 Subject: [PATCH 08/25] Suggested for issue #33 --- bin/create-ebs-volume | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/create-ebs-volume b/bin/create-ebs-volume index b679731..6c0bfd6 100755 --- a/bin/create-ebs-volume +++ b/bin/create-ebs-volume @@ -168,7 +168,7 @@ function create_and_attach_volume() { 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 '[]"' | tr : = + --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/,,/,/g ; s/,$//g ; s/^,//g' ) local max_attempts=10 From 536c143fa7c7d3ab961a46ce480be251b99a99a6 Mon Sep 17 00:00:00 2001 From: Gil Freund Date: Sun, 24 Oct 2021 07:36:18 +0300 Subject: [PATCH 09/25] Make removal or aws prefixes more general Handle aws- as well as as aws: Co-authored-by: Christopher Mundus <17323411+crashGoBoom@users.noreply.github.com> --- bin/create-ebs-volume | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/create-ebs-volume b/bin/create-ebs-volume index 6c0bfd6..6857564 100755 --- a/bin/create-ebs-volume +++ b/bin/create-ebs-volume @@ -168,7 +168,7 @@ function create_and_attach_volume() { 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/,,/,/g ; s/,$//g ; s/^,//g' + --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/,,/,/g ; s/,$//g ; s/^,//g' ) local max_attempts=10 From 4509c443d001b362e4c5a37f545dbef555d01799 Mon Sep 17 00:00:00 2001 From: ajfriedman18 Date: Tue, 26 Oct 2021 19:19:39 -0700 Subject: [PATCH 10/25] Update README.md Added additional information for customers using a CMK which requires additional IAM permissions --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 54fa484..51de820 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,8 @@ In the above, we assume that the `MyInstanceProfileWithProperPermissions` EC2 In } ``` +Please note that if you enable EBS encryption and use a Customer Managed Key with AWS Key Management Service, then you should also ensure that you provide [appropriate IAM permissions](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSEncryption.html#ebs-encryption-permissions) to use that key. + ## License Summary This sample code is made available under the MIT license. From 94b197e9a2131d4acff8a1d65a0a7b6fc354de51 Mon Sep 17 00:00:00 2001 From: Ward Vandewege Date: Mon, 21 Feb 2022 17:31:37 -0500 Subject: [PATCH 11/25] Add missing permission The "ec2:DescribeTags" permission is required because of the `aws ec2 describe-tags` call on line 169 in `bin/create-ebs-volume`. --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 51de820..c5c94e7 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,7 @@ In the above, we assume that the `MyInstanceProfileWithProperPermissions` EC2 In "ec2:AttachVolume", "ec2:DescribeVolumeStatus", "ec2:DescribeVolumes", + "ec2:DescribeTags", "ec2:ModifyInstanceAttribute", "ec2:DescribeVolumeAttribute", "ec2:CreateVolume", From 6e88326d0084217aa06f9d0eb44057a07cf10be1 Mon Sep 17 00:00:00 2001 From: Graham Wright Date: Tue, 22 Feb 2022 15:23:27 -0500 Subject: [PATCH 12/25] Check instance_tags not null Added check to `aws ec2 create-volume` command to ensure that $instance_tags will only be passed if it is not empty. --- bin/create-ebs-volume | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/bin/create-ebs-volume b/bin/create-ebs-volume index 6857564..df99af6 100755 --- a/bin/create-ebs-volume +++ b/bin/create-ebs-volume @@ -257,12 +257,20 @@ function create_and_attach_volume() { local volume="" for i in $(eval echo "{0..$max_attempts}") ; do + # If only tags on resource are prefixed with `aws:` they will all be deleted and cause a parameter validation on + # TagSpecifications[0].Tags[0] error to be thrown since the payload is determined to be a string, not dictionary. + if [ ! -z $instance_tags ]; then + local tag_specification="ResourceType=volume,Tags=[$instance_tags,{Key=source-instance,Value=$instance_id},{Key=amazon-ebs-autoscale-creation-time,Value=$timestamp}]" + else: + local tag_specification="ResourceType=volume,Tags=[{Key=source-instance,Value=$instance_id},{Key=amazon-ebs-autoscale-creation-time,Value=$timestamp}]" + fi + local volume=$(\ aws ec2 create-volume \ --region $region \ --availability-zone $availability_zone \ $volume_opts \ - --tag-specification "ResourceType=volume,Tags=[$instance_tags,{Key=source-instance,Value=$instance_id},{Key=amazon-ebs-autoscale-creation-time,Value=$timestamp}]" \ + --tag-specification "$tag_specification" \ 2> $tmpfile ) From 2d09af8260f5d9572e9f29e6435974e3ac80d4f4 Mon Sep 17 00:00:00 2001 From: Kevin Galens Date: Mon, 28 Feb 2022 16:30:40 -0500 Subject: [PATCH 13/25] Make parsing of aws tags less greedy, instead of removing ',,', remove any sequence of two or more commas --- bin/create-ebs-volume | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/create-ebs-volume b/bin/create-ebs-volume index 6857564..dfe732f 100755 --- a/bin/create-ebs-volume +++ b/bin/create-ebs-volume @@ -168,7 +168,7 @@ function create_and_attach_volume() { 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/,,/,/g ; s/,$//g ; s/^,//g' + --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' ) local max_attempts=10 From ab760dbf523d1a65b8e8c6f5bf4e678b80438a91 Mon Sep 17 00:00:00 2001 From: "W. Lee Pang, PhD" Date: Fri, 11 Mar 2022 15:34:50 -0800 Subject: [PATCH 14/25] fix typo resolve #45 --- bin/create-ebs-volume | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/create-ebs-volume b/bin/create-ebs-volume index aee319c..b165341 100755 --- a/bin/create-ebs-volume +++ b/bin/create-ebs-volume @@ -261,7 +261,7 @@ function create_and_attach_volume() { # TagSpecifications[0].Tags[0] error to be thrown since the payload is determined to be a string, not dictionary. if [ ! -z $instance_tags ]; then local tag_specification="ResourceType=volume,Tags=[$instance_tags,{Key=source-instance,Value=$instance_id},{Key=amazon-ebs-autoscale-creation-time,Value=$timestamp}]" - else: + else local tag_specification="ResourceType=volume,Tags=[{Key=source-instance,Value=$instance_id},{Key=amazon-ebs-autoscale-creation-time,Value=$timestamp}]" fi From 79957580facdbb9c157262d153032594105b0957 Mon Sep 17 00:00:00 2001 From: Graham Wright Date: Fri, 11 Mar 2022 19:32:48 -0500 Subject: [PATCH 15/25] Fixing typo I introduced with my previous PR and making logic tighter. --- bin/create-ebs-volume | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/bin/create-ebs-volume b/bin/create-ebs-volume index aee319c..c572333 100755 --- a/bin/create-ebs-volume +++ b/bin/create-ebs-volume @@ -257,14 +257,17 @@ function create_and_attach_volume() { local volume="" for i in $(eval echo "{0..$max_attempts}") ; do - # If only tags on resource are prefixed with `aws:` they will all be deleted and cause a parameter validation on - # TagSpecifications[0].Tags[0] error to be thrown since the payload is determined to be a string, not dictionary. - if [ ! -z $instance_tags ]; then - local tag_specification="ResourceType=volume,Tags=[$instance_tags,{Key=source-instance,Value=$instance_id},{Key=amazon-ebs-autoscale-creation-time,Value=$timestamp}]" - else: - local tag_specification="ResourceType=volume,Tags=[{Key=source-instance,Value=$instance_id},{Key=amazon-ebs-autoscale-creation-time,Value=$timestamp}]" - fi - + + # 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}]" + + # 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 + # around $tag_specification causes a parsing error due to the space in the $timestamp value (added to $tag_specification above). local volume=$(\ aws ec2 create-volume \ --region $region \ From 80ffce0f9b5d00dc49061e07220fc4e658b6740a Mon Sep 17 00:00:00 2001 From: Mark Whelan <7407040+MrMarkW@users.noreply.github.com> Date: Tue, 24 May 2022 09:40:08 -0500 Subject: [PATCH 16/25] feat: Add support for gp3 iops, throughput --- README.md | 23 +++++++++++++- bin/create-ebs-volume | 33 +++++++++++++------- bin/ebs-autoscale | 19 ++++++------ config/ebs-autoscale.json | 16 +++++----- install.sh | 63 +++++++++++++++++++++++++++++++++++++-- 5 files changed, 124 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index c5c94e7..8099942 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ aws ec2 run-instances --image-id ami-5253c32d \ --iam-instance-profile Name=MyInstanceProfileWithProperPermissions ``` -that installs required packages and runs the initialization script. By default this creates a mount point of `/scratch` on a encrypted 100GB EBS volume. To change the mount point, edit the [cloud-init script](templates/cloud-init-userdata.yaml) file and supply additional options to the install script to suit your specific needs. Install options are shown below. +that installs required packages and runs the initialization script. By default this creates a mount point of `/scratch` on a encrypted 200GB gp3 EBS volume. To change the mount point, edit the [cloud-init script](templates/cloud-init-userdata.yaml) file and supply additional options to the install script to suit your specific needs. Install options are shown below. ```text Install Amazon EBS Autoscale @@ -70,6 +70,27 @@ Options -t, --volume-type VOLUMETYPE EBS volume type to use. (Default: gp3) + --volume-iops VOLUMEIOPS + Volume IOPS for gp3, io1, io2 (default: 3000) + + --volume-throughput VOLUMETHOUGHPUT + Volume throughput for gp3 (default: 125) + + --min-ebs-volume-size SIZE_GB + Mimimum size in GB of new volumes created by the instance. + (Default: 150) + + --max-ebs-volume-size SIZE_GB + Maximum size in GB of new volumes created by the instance. + (Default: 1500) + + --max-total-created-size SIZE_GB + Maximum total size in GB of all volumes created by the instance. + (Default: 8000) + + --max-attached-volumes N + Maximum number of attached volumes. (Default: 16) + ``` ## A note on the IAM Instance Profile diff --git a/bin/create-ebs-volume b/bin/create-ebs-volume index b599069..8f41936 100755 --- a/bin/create-ebs-volume +++ b/bin/create-ebs-volume @@ -44,13 +44,17 @@ Required Options - -t, --type Type of volume. (Default: gp2) + -t, --type Type of volume. (Default: config.volume.type) - -i, --iops IOPS for volume. Only valid if type=io1. (Default: 3000) + -i, --iops N + IOPS for volume. Only valid if type=io1, io2, gp3. (Default: config.volume.iops) + + --throughput N + The throughput for a volume, with a maximum of 1,000 MiB/s. (Default: config.volume.throughput) --not-encrypted Flag to make the volume un-encyrpted. Default is to create an encrypted volume - + --max-total-created-size SIZE_GB Maximum total size in GB of all volumes created by the instance. (Default: config.limits.max_logical_volume_size) @@ -78,8 +82,9 @@ function error() { TYPE=$(get_config_value .volume.type) IOPS=$(get_config_value .volume.iops) +THROUGHPUT=$(get_config_value .volume.throughput) ENCRYPTED=$(get_config_value .volume.encrypted) -MAX_TOTAL_EBS_SIZE=$(get_config_value .limits.max_logical_volume_size) +MAX_LOGICAL_VOLUME_SIZE=$(get_config_value .limits.max_logical_volume_size) MAX_ATTACHED_VOLUMES=$(get_config_value .limits.max_ebs_volume_count) MAX_CREATED_VOLUMES=$MAX_ATTACHED_VOLUMES @@ -99,10 +104,18 @@ while (( "$#" )); do IOPS=$2 shift 2 ;; + --throughput) + THROUGHPUT=$2 + shift 2 + ;; --not-encrypted) unset ENCRYPTED shift ;; + --max-total-created-size) + MAX_LOGICAL_VOLUME_SIZE=$2 + shift 2 + ;; --max-attached-volumes) MAX_ATTACHED_VOLUMES=$2 shift 2 @@ -111,10 +124,6 @@ while (( "$#" )); do MAX_CREATED_VOLUMES=$2 shift 2 ;; - --max-total-created-size) - MAX_TOTAL_EBS_SIZE=$2 - shift 2 - ;; -v|--verbose) VERBOSE=1 shift @@ -227,8 +236,8 @@ function create_and_attach_volume() { done # check how much EBS storage this instance has created - if [ "$total_created_size" -ge "$MAX_TOTAL_EBS_SIZE" ]; then - error "maximum total ebs volume size reached ($MAX_TOTAL_EBS_SIZE)" + if [ "$total_created_size" -ge "$MAX_LOGICAL_VOLUME_SIZE" ]; then + error "maximum total ebs volume size reached ($MAX_LOGICAL_VOLUME_SIZE)" fi # check how many volumes this instance has created @@ -251,7 +260,9 @@ function create_and_attach_volume() { # create the volume local tmpfile=$(mktemp /tmp/ebs-autoscale.create-volume.XXXXXXXXXX) local volume_opts="--size $SIZE --volume-type $TYPE" - if [ "$TYPE" == "io1" ]; then volume_opts="$volume_opts --iops $IOPS"; fi + local IOPS_TYPES = ['io1','io2','gp3'] + if [[ " ${IOPS_TYPES[*]} " =~ " ${TYPE} " ]]; then volume_opts="$volume_opts --iops $IOPS"; fi + if [ "$TYPE" == "gp3" ]; then volume_opts="$volume_opts --throughput $THROUGHPUT"; fi if [ "$ENCRYPTED" == "1" ]; then volume_opts="$volume_opts --encrypted"; fi local timestamp=$(date "+%F %T UTC%z") # YYYY-mm-dd HH:MM:SS UTC+0000 diff --git a/bin/ebs-autoscale b/bin/ebs-autoscale index 17494d2..5d91f6a 100755 --- a/bin/ebs-autoscale +++ b/bin/ebs-autoscale @@ -32,6 +32,7 @@ initialize +MIN_EBS_VOLUME_SIZE=$(get_config_value .limits.min_ebs_volume_size) MAX_EBS_VOLUME_SIZE=$(get_config_value .limits.max_ebs_volume_size) MAX_LOGICAL_VOLUME_SIZE=$(get_config_value .limits.max_logical_volume_size) MAX_EBS_VOLUME_COUNT=$(get_config_value .limits.max_ebs_volume_count) @@ -118,29 +119,29 @@ calc_new_size() { # new volume sizes increase as the number of attached volumes increase local num_devices=$1 #local num_devices=$(get_num_devices) - local new_size=150 + local new_size=$MIN_EBS_VOLUME_SIZE if [ "$num_devices" -ge "4" ] && [ "$num_devices" -le "6" ]; then - if [ "$MAX_EBS_VOLUME_SIZE" -ge "299" ]; then + if [ "$MAX_EBS_VOLUME_SIZE" -ge "299" ] && [ "$MIN_EBS_VOLUME_SIZE" -le "299" ]; then new_size=300 + elif [ "$MIN_EBS_VOLUME_SIZE" -ge "299" ] + new_size=$MIN_EBS_VOLUME_SIZE else new_size=$MAX_EBS_VOLUME_SIZE fi elif [ "$num_devices" -gt "6" ] && [ "$num_devices" -le "10" ]; then - if [ "$MAX_EBS_VOLUME_SIZE" -ge "999" ]; then + if [ "$MAX_EBS_VOLUME_SIZE" -ge "999" ] && [ "$MIN_EBS_VOLUME_SIZE" -le "999" ]; then new_size=1000 + elif [ "$MIN_EBS_VOLUME_SIZE" -ge "999" ] + new_size=$MIN_EBS_VOLUME_SIZE else new_size=$MAX_EBS_VOLUME_SIZE fi elif [ "$num_devices" -gt "10" ]; then - if [ "$MAX_EBS_VOLUME_SIZE" -ge "1499" ]; then - new_size=1500 - else - new_size=$MAX_EBS_VOLUME_SIZE - fi + new_size=$MAX_EBS_VOLUME_SIZE else if [ "$MAX_EBS_VOLUME_SIZE" -ge "149" ]; then - new_size=150 + new_size=$MIN_EBS_VOLUME_SIZE else new_size=$MAX_EBS_VOLUME_SIZE fi diff --git a/config/ebs-autoscale.json b/config/ebs-autoscale.json index 1e62c30..a522d2a 100644 --- a/config/ebs-autoscale.json +++ b/config/ebs-autoscale.json @@ -2,22 +2,24 @@ "mountpoint": "%%MOUNTPOINT%%", "filesystem": "%%FILESYSTEM%%", "lvm": { - "volume_group": "autoscale_vg", - "logical_volume": "autoscale_lv" + "volume_group": "autoscale_vg", + "logical_volume": "autoscale_lv" }, "volume": { "type": "%%VOLUMETYPE%%", - "iops": 3000, + "iops": "%%VOLUMEIOPS%%", + "throughput": "%%VOLUMETHOUGHPUT%%", "encrypted": 1 }, "detection_interval": 2, "limits": { - "max_ebs_volume_size": 1500, - "max_logical_volume_size": 8000, - "max_ebs_volume_count": 16 + "min_ebs_volume_size": "%%MINEBSVOLUMESIZE%%", + "max_ebs_volume_size": "%%MAXEBSVOLUMESIZE%%", + "max_logical_volume_size": "%%MAXLOGICALVOLUMESIZE%%", + "max_ebs_volume_count": "%%MAXATTACHEDVOLUMES%%" }, "logging": { "log_file": "/var/log/ebs-autoscale.log", "log_interval": 300 } -} +} \ No newline at end of file diff --git a/install.sh b/install.sh index 0674c33..6d88d98 100644 --- a/install.sh +++ b/install.sh @@ -56,7 +56,28 @@ Options -t, --volume-type VOLUMETYPE Volume type (default: gp3) - -s, --initial-size SIZE + --volume-iops VOLUMEIOPS + Volume IOPS for gp3, io1, io2 (default: 3000) + + --volume-throughput VOLUMETHOUGHPUT + Volume throughput for gp3 (default: 125) + + --min-ebs-volume-size SIZE_GB + Mimimum size in GB of new volumes created by the instance. + (Default: 150) + + --max-ebs-volume-size SIZE_GB + Maximum size in GB of new volumes created by the instance. + (Default: 1500) + + --max-total-created-size SIZE_GB + Maximum total size in GB of all volumes created by the instance. + (Default: 8000) + + --max-attached-volumes N + Maximum number of attached volumes. (Default: 16) + + -s, --initial-size SIZE_GB Initial size of the volume in GB. (Default: 200) Only used if --initial-device is NOT specified. @@ -64,8 +85,16 @@ EOF ) MOUNTPOINT=/scratch +# defaults to set into ebs-autoscale.json SIZE=200 VOLUMETYPE=gp3 +VOLUMEIOPS=3000 +VOLUMETHOUGHPUT=125 +MIN_EBS_VOLUME_SIZE=150 +MAX_EBS_VOLUME_SIZE=1500 +MAX_LOGICAL_VOLUME_SIZE=8000 +MAX_ATTACHED_VOLUMES=16 + DEVICE="" FILE_SYSTEM=btrfs BASEDIR=$(dirname $0) @@ -87,6 +116,30 @@ while (( "$#" )); do VOLUMETYPE=$2 shift 2 ;; + --volume-iops) + VOLUMEIOPS=$2 + shift 2 + ;; + --volume-throughput) + VOLUMETHOUGHPUT=$2 + shift 2 + ;; + --min-ebs-volume-size) + MIN_EBS_VOLUME_SIZE=$2 + shift 2 + ;; + --max-ebs-volume-size) + MAX_EBS_VOLUME_SIZE=$2 + shift 2 + ;; + --max-total-created-size) + MAX_LOGICAL_VOLUME_SIZE=$2 + shift 2 + ;; + --max-attached-volumes) + MAX_ATTACHED_VOLUMES=$2 + shift 2 + ;; -d|--initial-device) DEVICE=$2 shift 2 @@ -150,7 +203,13 @@ cp ${BASEDIR}/config/ebs-autoscale.logrotate /etc/logrotate.d/ebs-autoscale 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}#" \ > /etc/ebs-autoscale.json ## Create filesystem @@ -163,7 +222,7 @@ fi # If a device is not given, or if the device is not valid if [ -z "${DEVICE}" ] || [ ! -b "${DEVICE}" ]; then - DEVICE=$(create-ebs-volume --size $SIZE --type $VOLUMETYPE) + DEVICE=$(create-ebs-volume --size $SIZE) fi # create and mount the BTRFS filesystem From 2bed4bd7e8da24598cf2f56b80675fd7debe455f Mon Sep 17 00:00:00 2001 From: BenCodeOcean <109240650+BenCodeOcean@users.noreply.github.com> Date: Wed, 31 Aug 2022 11:01:04 +0300 Subject: [PATCH 17/25] Metadata service compatible with imdsv2 --- shared/utils.sh | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/shared/utils.sh b/shared/utils.sh index 9560734..fdf315f 100644 --- a/shared/utils.sh +++ b/shared/utils.sh @@ -28,10 +28,18 @@ # IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. +function get_metadata() { + local key=$1 + local metdata_ip='169.254.169.254' + local token=$(curl -s -X PUT "http://$metdata_ip/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 60") + + echo `curl -s -H "X-aws-ec2-metadata-token: $token" http://$metdata_ip/latest/meta-data/$key` +} + function initialize() { - export AWS_AZ=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone/) + export AWS_AZ=$(get_metadata placement/availability-zone) export AWS_REGION=$(echo ${AWS_AZ} | sed -e 's/[a-z]$//') - export INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id) + export INSTANCE_ID=$(get_metadata placement/availability-zone/instance-id) export EBS_AUTOSCALE_CONFIG_FILE=/etc/ebs-autoscale.json } @@ -51,11 +59,6 @@ function get_config_value() { jq -r $filter $EBS_AUTOSCALE_CONFIG_FILE } -function get_metadata() { - local key=$1 - echo `curl -s http://169.254.169.254/latest/meta-data/$key` -} - function logthis() { echo "[`date`] $1" >> $(get_config_value .logging.log_file) } From db009b9d76eb7b77467540d9ec1c53635b65965a Mon Sep 17 00:00:00 2001 From: Bryan Lajoie Date: Mon, 1 Aug 2022 20:43:21 -0700 Subject: [PATCH 18/25] fix gp3 autoscale --- bin/create-ebs-volume | 2 +- install.sh | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/bin/create-ebs-volume b/bin/create-ebs-volume index 8f41936..db23f7f 100755 --- a/bin/create-ebs-volume +++ b/bin/create-ebs-volume @@ -260,7 +260,7 @@ function create_and_attach_volume() { # create the volume local tmpfile=$(mktemp /tmp/ebs-autoscale.create-volume.XXXXXXXXXX) local volume_opts="--size $SIZE --volume-type $TYPE" - local IOPS_TYPES = ['io1','io2','gp3'] + local IOPS_TYPES=( io1 io2 gp3 ) if [[ " ${IOPS_TYPES[*]} " =~ " ${TYPE} " ]]; then volume_opts="$volume_opts --iops $IOPS"; fi if [ "$TYPE" == "gp3" ]; then volume_opts="$volume_opts --throughput $THROUGHPUT"; fi if [ "$ENCRYPTED" == "1" ]; then volume_opts="$volume_opts --encrypted"; fi diff --git a/install.sh b/install.sh index 6d88d98..ce245a6 100644 --- a/install.sh +++ b/install.sh @@ -205,10 +205,10 @@ cat ${BASEDIR}/config/ebs-autoscale.json | \ 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#%%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}#" \ > /etc/ebs-autoscale.json @@ -222,7 +222,7 @@ fi # If a device is not given, or if the device is not valid if [ -z "${DEVICE}" ] || [ ! -b "${DEVICE}" ]; then - DEVICE=$(create-ebs-volume --size $SIZE) + DEVICE=$(create-ebs-volume --size $SIZE --type $VOLUMETYPE) fi # create and mount the BTRFS filesystem From 21c5074ba4e1eb73444d38b980e3e6876eed505b Mon Sep 17 00:00:00 2001 From: Bryan Lajoie Date: Fri, 9 Sep 2022 16:56:18 -0700 Subject: [PATCH 19/25] fix typo in bash, elif --- bin/ebs-autoscale | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/ebs-autoscale b/bin/ebs-autoscale index 5d91f6a..33edcb6 100755 --- a/bin/ebs-autoscale +++ b/bin/ebs-autoscale @@ -124,7 +124,7 @@ calc_new_size() { if [ "$num_devices" -ge "4" ] && [ "$num_devices" -le "6" ]; then if [ "$MAX_EBS_VOLUME_SIZE" -ge "299" ] && [ "$MIN_EBS_VOLUME_SIZE" -le "299" ]; then new_size=300 - elif [ "$MIN_EBS_VOLUME_SIZE" -ge "299" ] + elif [ "$MIN_EBS_VOLUME_SIZE" -ge "299" ]; then new_size=$MIN_EBS_VOLUME_SIZE else new_size=$MAX_EBS_VOLUME_SIZE @@ -132,7 +132,7 @@ calc_new_size() { elif [ "$num_devices" -gt "6" ] && [ "$num_devices" -le "10" ]; then if [ "$MAX_EBS_VOLUME_SIZE" -ge "999" ] && [ "$MIN_EBS_VOLUME_SIZE" -le "999" ]; then new_size=1000 - elif [ "$MIN_EBS_VOLUME_SIZE" -ge "999" ] + elif [ "$MIN_EBS_VOLUME_SIZE" -ge "999" ]; then new_size=$MIN_EBS_VOLUME_SIZE else new_size=$MAX_EBS_VOLUME_SIZE From 3d2a656c3e2cba7643b115602f17a283ddb5abb6 Mon Sep 17 00:00:00 2001 From: Bryan Lajoie Date: Wed, 14 Sep 2022 16:30:54 -0700 Subject: [PATCH 20/25] adding support for --initial-utilization-threshold, to limit scalining if desired --- bin/ebs-autoscale | 5 +++-- config/ebs-autoscale.json | 3 ++- install.sh | 11 ++++++++++- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/bin/ebs-autoscale b/bin/ebs-autoscale index 33edcb6..05325ee 100755 --- a/bin/ebs-autoscale +++ b/bin/ebs-autoscale @@ -36,6 +36,7 @@ MIN_EBS_VOLUME_SIZE=$(get_config_value .limits.min_ebs_volume_size) MAX_EBS_VOLUME_SIZE=$(get_config_value .limits.max_ebs_volume_size) MAX_LOGICAL_VOLUME_SIZE=$(get_config_value .limits.max_logical_volume_size) MAX_EBS_VOLUME_COUNT=$(get_config_value .limits.max_ebs_volume_count) +INITIAL_UTILIZATION_THRESHOLD=$(get_config_value .limits.initial_utilization_threshold) FILE_SYSTEM=$(get_config_value .filesystem) @@ -99,7 +100,7 @@ calc_threshold() { # as more ebs volumes are added, the threshold level increases local num_devices=$1 - local threshold=50 + local threshold=${INITIAL_UTILIZATION_THRESHOLD} if [ "$num_devices" -ge "4" ] && [ "$num_devices" -le "6" ]; then threshold=80 @@ -108,7 +109,7 @@ calc_threshold() { elif [ "$num_devices" -gt "10" ]; then threshold=90 else - threshold=50 + threshold=${INITIAL_UTILIZATION_THRESHOLD} fi echo ${threshold} diff --git a/config/ebs-autoscale.json b/config/ebs-autoscale.json index a522d2a..a8507de 100644 --- a/config/ebs-autoscale.json +++ b/config/ebs-autoscale.json @@ -16,7 +16,8 @@ "min_ebs_volume_size": "%%MINEBSVOLUMESIZE%%", "max_ebs_volume_size": "%%MAXEBSVOLUMESIZE%%", "max_logical_volume_size": "%%MAXLOGICALVOLUMESIZE%%", - "max_ebs_volume_count": "%%MAXATTACHEDVOLUMES%%" + "max_ebs_volume_count": "%%MAXATTACHEDVOLUMES%%", + "initial_utilization_threshold": "%%INITIALUTILIZATIONTHRESHOLD%%" }, "logging": { "log_file": "/var/log/ebs-autoscale.log", diff --git a/install.sh b/install.sh index ce245a6..c4e933c 100644 --- a/install.sh +++ b/install.sh @@ -77,6 +77,9 @@ Options --max-attached-volumes N Maximum number of attached volumes. (Default: 16) + --initial-utilization-threshold N + Initial disk utilization treshold for scale-up. (Default: 50) + -s, --initial-size SIZE_GB Initial size of the volume in GB. (Default: 200) Only used if --initial-device is NOT specified. @@ -94,6 +97,7 @@ MIN_EBS_VOLUME_SIZE=150 MAX_EBS_VOLUME_SIZE=1500 MAX_LOGICAL_VOLUME_SIZE=8000 MAX_ATTACHED_VOLUMES=16 +INITIAL_UTILIZATION_THRESHOLD=50 DEVICE="" FILE_SYSTEM=btrfs @@ -140,6 +144,10 @@ while (( "$#" )); do MAX_ATTACHED_VOLUMES=$2 shift 2 ;; + --initial-utilization-threshold) + INITIAL_UTILIZATION_THRESHOLD=$2 + shift 2 + ;; -d|--initial-device) DEVICE=$2 shift 2 @@ -209,7 +217,8 @@ cat ${BASEDIR}/config/ebs-autoscale.json | \ 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#%%MAXATTACHEDVOLUMES%%#${MAX_ATTACHED_VOLUMES}#" | \ + sed -e "s#%%INITIALUTILIZATIONTHRESHOLD%%#${INITIAL_UTILIZATION_THRESHOLD}#" \ > /etc/ebs-autoscale.json ## Create filesystem From 928dbb1087e4accee8e23e553fdbdba99e93a2eb Mon Sep 17 00:00:00 2001 From: Ofer-CO <107927697+Ofer-CO@users.noreply.github.com> Date: Mon, 24 Oct 2022 16:20:58 +0300 Subject: [PATCH 21/25] fix instance-id get-metadata --- shared/utils.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared/utils.sh b/shared/utils.sh index fdf315f..85f8a4e 100644 --- a/shared/utils.sh +++ b/shared/utils.sh @@ -39,7 +39,7 @@ function get_metadata() { function initialize() { export AWS_AZ=$(get_metadata placement/availability-zone) export AWS_REGION=$(echo ${AWS_AZ} | sed -e 's/[a-z]$//') - export INSTANCE_ID=$(get_metadata placement/availability-zone/instance-id) + export INSTANCE_ID=$(get_metadata instance-id) export EBS_AUTOSCALE_CONFIG_FILE=/etc/ebs-autoscale.json } From 5ea3967a23608c7ffe0ab6e404c0ec4187510651 Mon Sep 17 00:00:00 2001 From: BenCodeOcean <109240650+BenCodeOcean@users.noreply.github.com> Date: Mon, 31 Oct 2022 14:47:27 +0200 Subject: [PATCH 22/25] imdsv2 support added --- install.sh | 7 +++++++ shared/utils.sh | 10 +++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/install.sh b/install.sh index 0674c33..ad3f5c5 100644 --- a/install.sh +++ b/install.sh @@ -59,6 +59,9 @@ Options -s, --initial-size SIZE Initial size of the volume in GB. (Default: 200) Only used if --initial-device is NOT specified. + + -i, --imdsv2 + Enable imdsv2 for instance metadata API requests. EOF ) @@ -99,6 +102,10 @@ while (( "$#" )); do MOUNTPOINT=$2 shift 2 ;; + -i|--imdsv2) + IMDSV2="true" + shift 1 + ;; -h|--help) echo "$USAGE" exit diff --git a/shared/utils.sh b/shared/utils.sh index 85f8a4e..631ac81 100644 --- a/shared/utils.sh +++ b/shared/utils.sh @@ -30,10 +30,14 @@ function get_metadata() { local key=$1 - local metdata_ip='169.254.169.254' - local token=$(curl -s -X PUT "http://$metdata_ip/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 60") + local metadata_ip='169.254.169.254' + + if [ ! -z "$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"` + fi - echo `curl -s -H "X-aws-ec2-metadata-token: $token" http://$metdata_ip/latest/meta-data/$key` + echo `curl -s $token_wrapper http://$metadata_ip/latest/meta-data/$key` } function initialize() { From 06e1f1be002cbb9b6eddc40998d2b22a6c664ed2 Mon Sep 17 00:00:00 2001 From: BenCodeOcean <109240650+BenCodeOcean@users.noreply.github.com> Date: Mon, 31 Oct 2022 16:34:28 +0200 Subject: [PATCH 23/25] initialize call moved --- install.sh | 4 ++-- shared/utils.sh | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/install.sh b/install.sh index ad3f5c5..3d37a9e 100644 --- a/install.sh +++ b/install.sh @@ -76,8 +76,6 @@ BASEDIR=$(dirname $0) . ${BASEDIR}/shared/utils.sh -initialize - # parse options PARAMS="" while (( "$#" )); do @@ -126,6 +124,8 @@ done eval set -- "$PARAMS" +initialize + # for backwards compatibility evaluate positional parameters like previous 2.0.x and 2.1.x releases # this will be removed in the future if [ ! -z "$PARAMS" ]; then diff --git a/shared/utils.sh b/shared/utils.sh index 631ac81..d1a216e 100644 --- a/shared/utils.sh +++ b/shared/utils.sh @@ -34,7 +34,7 @@ function get_metadata() { if [ ! -z "$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"` + local token_wrapper='-H "X-aws-ec2-metadata-token: $token"' fi echo `curl -s $token_wrapper http://$metadata_ip/latest/meta-data/$key` From c922747d351152018205f09939b0a703f5f8f015 Mon Sep 17 00:00:00 2001 From: Ben Even Tsur <109240650+BenCodeOcean@users.noreply.github.com> Date: Wed, 9 Nov 2022 19:42:40 +0200 Subject: [PATCH 24/25] Update README.md for imdsv2 feature --- README.md | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 8099942..100cf81 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ that installs required packages and runs the initialization script. By default t ```text Install Amazon EBS Autoscale - install.sh [options] [[-m] ] + ./install.sh [options] [[-m] ] Options @@ -62,20 +62,16 @@ Options -m, --mountpoint MOUNTPOINT Mount point for autoscale volume (default: /scratch) - - -s, --initial-size SIZE - Initial size of the volume in GB. (Default: 200) - Only used if --initial-device is NOT specified. - + -t, --volume-type VOLUMETYPE - EBS volume type to use. (Default: gp3) + Volume type (default: gp3) --volume-iops VOLUMEIOPS Volume IOPS for gp3, io1, io2 (default: 3000) --volume-throughput VOLUMETHOUGHPUT Volume throughput for gp3 (default: 125) - + --min-ebs-volume-size SIZE_GB Mimimum size in GB of new volumes created by the instance. (Default: 150) @@ -91,6 +87,15 @@ Options --max-attached-volumes N Maximum number of attached volumes. (Default: 16) + --initial-utilization-threshold N + Initial disk utilization treshold for scale-up. (Default: 50) + + -s, --initial-size SIZE_GB + Initial size of the volume in GB. (Default: 200) + Only used if --initial-device is NOT specified. + + -i, --imdsv2 + Enable imdsv2 for instance metadata API requests. ``` ## A note on the IAM Instance Profile From 5aca17e7977e252965a09af1a0ab379d818a02ac Mon Sep 17 00:00:00 2001 From: Ben Even Tsur <109240650+BenCodeOcean@users.noreply.github.com> Date: Wed, 9 Nov 2022 19:52:22 +0200 Subject: [PATCH 25/25] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 100cf81..59699c3 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ that installs required packages and runs the initialization script. By default t ```text Install Amazon EBS Autoscale - ./install.sh [options] [[-m] ] + install.sh [options] [[-m] ] Options