Skip to content

Commit 626d29d

Browse files
orensharoniOren Sharoni
andauthored
fix: ec2 check to support IMDSv1 and IMDSv2 + moving env vars to conf file fixes #206
* Add support for IMDV2 in the ec2 startup script (thank you @orensharoni) * Fix a typo in the ec2 startup script (thank you @HighOnMikey) --------- Co-authored-by: Oren Sharoni <[email protected]>
1 parent c687663 commit 626d29d

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

standalone_ubuntu_oss_install.sh

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@ required=("S3_BUCKET_NAME" "S3_SERVER" "S3_SERVER_PORT" "S3_SERVER_PROTO"
3636
if [ ! -z ${AWS_CONTAINER_CREDENTIALS_RELATIVE_URI+x} ]; then
3737
echo "Running inside an ECS task, using container credentials"
3838
uses_iam_creds=1
39+
elif TOKEN=$(curl -X PUT --silent --fail --connect-timeout 2 --max-time 2 "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600") && \
40+
curl -H "X-aws-ec2-metadata-token: $TOKEN" --output /dev/null --silent --head --fail --connect-timeout 2 --max-time 5 "http://169.254.169.254"; then
41+
echo "Running inside an EC2 instance, using IMDSv2 for credentials"
42+
uses_iam_creds=1
3943
elif curl --output /dev/null --silent --head --fail --connect-timeout 2 "http://169.254.169.254"; then
40-
echo "Running inside an EC2 instance, using IMDS for credentials"
44+
echo "Running inside an EC2 instance, using IMDSv1 for credentials"
4145
uses_iam_creds=1
4246
else
4347
required+=("AWS_ACCESS_KEY_ID" "AWS_SECRET_ACCESS_KEY")
@@ -312,12 +316,12 @@ EOF
312316
# Only include these env vars if we are not using a instance profile credential
313317
# to obtain S3 permissions.
314318
if [ $uses_iam_creds -eq 0 ]; then
315-
cat >> "/etc/nginx/environment" << EOF
319+
cat >> "/etc/nginx/nginx.conf" << EOF
316320
env AWS_ACCESS_KEY_ID;
317321
env AWS_SECRET_ACCESS_KEY;
318322
EOF
319323
if [[ -v AWS_SESSION_TOKEN ]]; then
320-
cat >> "/etc/nginx/environment" << EOF
324+
cat >> "/etc/nginx/nginx.conf" << EOF
321325
env AWS_SESSION_TOKEN;
322326
EOF
323327
fi

0 commit comments

Comments
 (0)