Skip to content

Amazon Linux 2 - fluent-bit.service fails to start with error "Dependency failed for Fluent Bit." #10186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
charltonstanley opened this issue Apr 9, 2025 · 2 comments · May be fixed by #10221

Comments

@charltonstanley
Copy link

Bug Report

Describe the bug
#9845 was implemented to address a race condition, however this will fail for any instances that require the use of version 2 of AWS's Instance Metadata Service. IMDSv2 requires retrieving a token via a PUT and then passing that token when performing your GET.
See the documentation here: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/configuring-instance-metadata-service.html#instance-metadata-retrieval-examples

To Reproduce

  1. Install fluent-bit 4.0.0 on an Amazon Linux 2 instance that requires IMDSv2
  2. observe that the fluent-bit service won't even start.

Log output from the new setservice.service dependency that was added in #9845.

[ec2-user@REDACTED ~]$ sudo service sethostname status
Redirecting to /bin/systemctl status sethostname.service
● sethostname.service - Set Hostname Workaround coreos/bugs#1272
   Loaded: loaded (/usr/lib/systemd/system/sethostname.service; disabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Tue 2025-04-08 17:55:02 CDT; 1min 16s ago
  Process: 5306 ExecStart=/bin/sh -c /usr/bin/hostnamectl set-hostname $(curl -s http://169.254.169.254/latest/meta-data/hostname) (code=exited, status=1/FAILURE)
 Main PID: 5306 (code=exited, status=1/FAILURE)

Apr 08 17:55:02 REDACTED.ec2.internal systemd[1]: Starting Set Hostname Workaround coreos/bugs#1272...
Apr 08 17:55:02 REDACTED.ec2.internal sh[5306]: Invalid number of arguments.
Apr 08 17:55:02 REDACTED.ec2.internal systemd[1]: sethostname.service: main process exited, code=exited, status=1/FAILURE
Apr 08 17:55:02 REDACTED.ec2.internal systemd[1]: Failed to start Set Hostname Workaround coreos/bugs#1272.
Apr 08 17:55:02 REDACTED.ec2.internal systemd[1]: Unit sethostname.service entered failed state.
Apr 08 17:55:02 REDACTED.ec2.internal systemd[1]: sethostname.service failed.

Expected behavior
Fluent-bit service starts

Screenshots

Screenshot of relevant EC2 Instance setting
Image

Your Environment

  • Version used: 4.0.0
  • Configuration:
  • Environment name and version (e.g. Kubernetes? What version?):
  • Server type and version: n/a
  • Operating System and version: Amazon Linux 2
  • Filters and plugins: n/a

Additional context

We are unable to upgrade to v4.0.0 on all of the instances in our AWS organization. We are smaller organization, but I know that we can't be the only AWS customers that, for security reasons, require IMDSv2 to be used over IMDSv1.

Security benefits of IMDSv2: https://aws.amazon.com/blogs/security/defense-in-depth-open-firewalls-reverse-proxies-ssrf-vulnerabilities-ec2-instance-metadata-service/

@cpandya-we
Copy link

cpandya-we commented Apr 11, 2025

@charltonstanley
Replace your /lib/systemd/system/sethostname.service file with the contents below:

[Unit]
Description=Set Hostname Workaround coreos/bugs#1272 with EC2 IMDSv2 support
Wants=network-online.target
After=network-online.target

[Service]
Type=oneshot
RemainAfterExit=yes

ExecStartPre=/bin/bash -c 'curl -sX PUT "http://169.254.169.254/latest/api/token" \
  -H "X-aws-ec2-metadata-token-ttl-seconds: 21600" > /run/imds_token'

ExecStartPre=/bin/bash -c 'curl -s "http://169.254.169.254/latest/meta-data/hostname" \
  -H "X-aws-ec2-metadata-token: $(cat /run/imds_token)" > /run/ec2_hostname'

ExecStart=/usr/bin/hostnamectl set-hostname "$(cat /run/ec2_hostname)"

ExecStartPost=/bin/bash -c 'rm -f /run/imds_token /run/ec2_hostname'

[Install]
WantedBy=multi-user.target

Then run the below two commands to pickup the new sethostname service

sudo systemctl daemon-reload
sudo systemctl start sethostname.service

@charltonstanley
Copy link
Author

@cpandya-we Thanks for this. I had to change ExecStart from

ExecStart=/usr/bin/hostnamectl set-hostname "$(cat /run/ec2_hostname)"

to

ExecStart=/bin/bash -c '/usr/bin/hostnamectl set-hostname "$(cat /run/ec2_hostname)"'

In order for the "$(cat /run/ec2_hostname)" part of the command to work.

charltonstanley added a commit to PedigreeTechnologies/fluent-bit that referenced this issue Apr 15, 2025
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
charltonstanley added a commit to PedigreeTechnologies/fluent-bit that referenced this issue Apr 16, 2025
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 <[email protected]>
charltonstanley added a commit to PedigreeTechnologies/fluent-bit that referenced this issue Apr 16, 2025
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 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants