aws-sdk-js-v3: ec2-metadata-service errors in up to date AWS EKS cluster using Pod Identity
Checkboxes for prior research
- I’ve gone through Developer Guide and API reference
- I’ve checked AWS Forums and StackOverflow.
- I’ve searched for previous similar issues and didn’t find any solution.
Describe the bug
Using latest version of https://www.npmjs.com/package/@aws-sdk/ec2-metadata-service seems to not work out of the box with NodeJS v18 in an AWS EKS kubernetes cluster running pod with service account associated and valid policy attached.
Regression Issue
- Select this option if this issue appears to be a regression.
SDK version number
@aws-sdk/ec2-metadata-service@3.693.0
Which JavaScript Runtime is this issue in?
Node.js
Details of the browser/Node.js/ReactNative version
node v18.20.4
Reproduction Steps
# testing the Pod with latest awscli
root@nodetest:/tmp# /usr/local/bin/aws --version
aws-cli/2.21.2 Python/3.12.6 Linux/6.1.112-124.190.amzn2023.x86_64 exe/x86_64.debian.12
root@nodetest:/tmp# /usr/local/bin/aws sts get-caller-identity
{
"UserId": "xxxxx:eks-app-dev-nodetest-23833230-c77c-4398-95a9-c03cc43bf1a7",
"Account": "xxxxx",
"Arn": "arn:aws:sts::xxxxx:assumed-role/eks-app-dev-app-multimediaworker/eks-app-dev-nodetest-23833230-c77c-4398-95a9-c03cc43bf1a7"
}
root@nodetest:/tmp# /usr/local/bin/aws secretsmanager get-secret-value --secret-id xxxxx --output text > output # Works too
Trying getting metadata info via JS module:
root@nodetest:/tmp# npm install @aws-sdk/ec2-metadata-service
added 19 packages, and audited 20 packages in 2s
found 0 vulnerabilities
root@nodetest:/tmp# cat test.js
const main = async () => {
const { MetadataService } = require("@aws-sdk/ec2-metadata-service");
const metadataService = new MetadataService({});
const metadata = await metadataService.request("/latest/meta-data/", {});
console.log(metadata);
}
main();
root@nodetest:/tmp# node test.js
/tmp/node_modules/@aws-sdk/ec2-metadata-service/dist-cjs/index.js:112
throw new Error(`Error making request to the metadata service: ${error}`);
^
Error: Error making request to the metadata service: Error: Request failed with status code 401
at _MetadataService.request (/tmp/node_modules/@aws-sdk/ec2-metadata-service/dist-cjs/index.js:112:13)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async main (/tmp/test.js:5:22)
Node.js v18.20.4
Observed Behavior
Error when trying to fetch metadata
Expected Behavior
Metadata fetched correctly
Possible Solution
No response
Additional Information/Context
No response
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 19 (8 by maintainers)
@RanVaknin I can confirm you https://www.npmjs.com/package/@aws-sdk/ec2-metadata-service works as expected once the above patching to the Amazon Linux 2023 instance has been applied 🎉
Hang on, it’s me who has to thank you for the help so far! Take your time, I’ll check emails and answer asap if you have any related question. Enjoy your weekend