Skip to content

Commit 0f46a24

Browse files
committed
updating zone method
1 parent 34aec98 commit 0f46a24

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ WORKDIR /usr/src/app
77
RUN apt-get update && apt-get -y install iproute2 curl jq libgmp3-dev ruby-dev build-essential sqlite libsqlite3-dev python3 python3-pip && \
88
gem install bundler:1.17.3 && \
99
bundle install && \
10-
pip3 install awscli && \
10+
pip3 install awscli netaddr && \
1111
apt-get autoremove -y --purge && \
1212
apt-get remove -y --auto-remove --purge ruby-dev libgmp3-dev build-essential libsqlite3-dev && \
1313
apt-get clean && \

startup-cdk.sh

+6-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ IP=$(ip route show |grep -o src.* |cut -f2 -d" ")
77
if [[ ${IP} == "" ]]; then
88
IP=$(hostname -i)
99
fi
10-
1110
SUBNET=$(echo ${IP} | cut -f1 -d.)
1211
NETWORK=$(echo ${IP} | cut -f3 -d.)
1312

@@ -80,19 +79,18 @@ fi
8079
# Still no luck? Perhaps we're running fargate!
8180
if [[ -z ${zone} ]]; then
8281
export AWS_DEFAULT_REGION=$REGION
83-
ip_addr=$(curl -m2 -s ${ECS_CONTAINER_METADATA_URI} | jq '.Networks[].IPv4Addresses[]')
84-
declare -a subnets=( $(aws ec2 describe-subnets | jq .Subnets[].CidrBlock| sed ':a;N;$!ba;s/\n/ /g') )
82+
ip_addr=$(curl -m2 -s ${ECS_CONTAINER_METADATA_URI} | jq -r '.Networks[].IPv4Addresses[]')
83+
declare -a subnets=( $(aws ec2 describe-subnets | jq -r .Subnets[].CidrBlock| sed ':a;N;$!ba;s/\n/ /g') )
8584
for sub in "${subnets[@]}"; do
86-
if $(ruby -e "puts(IPAddr.new($sub.to_s).include? $ip_addr.to_s)") == 'true'; then
87-
details=$(aws ec2 describe-subnets | jq -r ".Subnets[] | select(.CidrBlock==$sub)")
88-
if [[ $(echo $details | jq .Tags) != "null" ]]; then
89-
zone=$(echo $details | jq -r 'select(.Tags[].Value=="ecsworkshop") | .AvailabilityZone' | sort -u | grep -o .$)
90-
fi
85+
ip_match=$(echo -e "from netaddr import IPNetwork, IPAddress\nif IPAddress('$ip_addr') in IPNetwork('$sub'):\n print('true')" | python3)
86+
if [[ $ip_match == "true" ]];then
87+
zone=$(aws ec2 describe-subnets | jq -r --arg sub "$sub" '.Subnets[] | select(.CidrBlock==$sub) | .AvailabilityZone' | grep -o .$)
9188
fi
9289
done
9390
fi
9491

9592
export CODE_HASH="$(cat code_hash.txt)"
93+
export IP
9694
export AZ="${IP} in AZ-${zone}"
9795

9896
# exec bundle exec thin start

0 commit comments

Comments
 (0)