Skip to content

Commit 08fdd4a

Browse files
systemd: build: fix service startup failure when using IMDSv2
PR fluent#9845 added a service dependency "sethostname.service" for Amazon Linux 2, which uses Amazon's Instance Metadata Service (IMDS) to query and set the hostname. This works for instances using the legacy IMDSv1, however it fails when admins have enforced the usage of IMDSv2. This patch is a modification of @cpandya-we comment/work on fluent#10186. Fixes fluent#10186 Signed-off-by: Charlton Stanley <charlton@charltonstanley.com>
1 parent e2cee6a commit 08fdd4a

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

init/az2-sethostname.in

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
[Unit]
2-
Description=Set Hostname Workaround coreos/bugs#1272
2+
Description=Set Hostname Workaround coreos/bugs#1272 with EC2 IMDSv2 support
33
Wants=network-online.target
44
After=network-online.target
55

66
[Service]
77
Type=oneshot
88
RemainAfterExit=yes
9-
ExecStart=/bin/sh -c "/usr/bin/hostnamectl set-hostname $(curl -s http://169.254.169.254/latest/meta-data/hostname)"
9+
10+
ExecStartPre=/bin/sh -c 'curl -sX PUT "http://169.254.169.254/latest/api/token" \
11+
-H "X-aws-ec2-metadata-token-ttl-seconds: 21600" > /run/imds_token'
12+
13+
ExecStartPre=/bin/sh -c 'curl -s "http://169.254.169.254/latest/meta-data/hostname" \
14+
-H "X-aws-ec2-metadata-token: $(cat /run/imds_token)" > /run/ec2_hostname'
15+
16+
ExecStart=/bin/sh -c '/usr/bin/hostnamectl set-hostname "$(cat /run/ec2_hostname)"'
17+
18+
ExecStartPost=/bin/sh -c 'rm -f /run/imds_token /run/ec2_hostname'
1019

1120
[Install]
12-
WantedBy=multi-user.target
21+
WantedBy=multi-user.target

0 commit comments

Comments
 (0)