amazon-web-services - 无法从链中的任何提供商加载 AWS 凭证:WebIdentityTokenCredentialsProvider:无法找到指定的 Web 身份令牌文件
问题描述
我正在使用 aws eks fargate deployment.yaml 文件部署我的服务,我的服务将连接到 dynamodb,并且在加载 id 时会引发错误。即使我通过以下命令获得了 Web 令牌文件。
kubectl exec -n first-namespace first-deployment-fhghgj567kkk-257xq env | grep AWS
AWS_REGION=us-east-1
AWS_ROLE_ARN=arn:aws:iam::263011912432:role/firstRole
AWS_WEB_IDENTITY_TOKEN_FILE=/var/run/secrets/eks.amazonaws.com/serviceaccount/token
但它仍然会引发以下错误。
Caused by: com.amazonaws.SdkClientException: Unable to load AWS credentials from any provider in the chain:
[EnvironmentVariableCredentialsProvider:
Unable to load AWS credentials from environment variables (AWS_ACCESS_KEY_ID (or AWS_ACCESS_KEY)
and AWS_SECRET_KEY (or AWS_SECRET_ACCESS_KEY)),
SystemPropertiesCredentialsProvider: Unable to load AWS credentials from Java system properties (aws.accessKeyId and aws.secretKey),
WebIdentityTokenCredentialsProvider: Unable to locate specified web identity token file:
/var/run/secrets/eks.amazonaws.com/serviceaccount/token,
com.amazonaws.auth.profile.ProfileCredentialsProvider@7b58e085: profile file cannot be null,
com.amazonaws.auth.EC2ContainerCredentialsProviderWrapper@39cb0014: Failed to connect to service endpoint: ]
我 ssh 进入容器并检查令牌是否仍然存在。
有人可以帮忙吗?谢谢
解决方案
我有一个类似的问题,在我的情况下,我必须升级 java sdk 并添加一个服务帐户,我没有尝试自己调用 dynamo,但 s3 和 lambdas 工作正常。这是我遵循的步骤,这是文档链接:https ://docs.aws.amazon.com/eks/latest/userguide/enable-iam-roles-for-service-accounts.html
一、开启OIDC
eksctl utils associate-iam-oidc-provider --cluster cluster_name --approve
然后创建并应用您的服务帐户,注意该角色必须有权访问您要使用的任何服务(发电机):
apiVersion: v1
kind: ServiceAccount
metadata:
name: fargateserviceaccount
annotations:
eks.amazonaws.com/role-arn: arn:aws:iam::ACCOUNT:role/MY_ROLE_NAME
在您的 pod 上,定义服务帐户和环境变量:
spec:
serviceAccountName: <ServiceAccountName> (kubectl get sa)
containers:
- name: java-api
image: <AccountID>.dkr.ecr.us-east-1.amazonaws.com/<Repo>:<tag>
env:
- name: AWS_REGION
value: us-east-1
最后在您的代码中使用 WebIdentityTokenCredentialsProvider
// Create Credentials provider using ENV variables -> Service account is used to run Fargate
WebIdentityTokenCredentialsProvider awsCredentialProvider = WebIdentityTokenCredentialsProvider.builder()
.roleArn(System.getenv("AWS_ROLE_ARN"))
.roleSessionName(System.getenv("AWS_ROLE_SESSION_NAME"))
.webIdentityTokenFile(System.getenv("AWS_WEB_IDENTITY_TOKEN_FILE"))
.build();
// Build client using explicit credentials provider
AWSLambdaClientBuilder builder = AWSLambdaClientBuilder.standard()
.withRegion(REGION)
.withCredentials(awsCredentialProvider);
client = builder.build();
推荐阅读
- java - 警告:com.atomikos.icatch.imp.CoordinatorImp - 协调器 10.210.78.56.tm0196300003 状态为 COMMITTING 的本地启发式终止
- javascript - JQuery removeClass 方法在 firefox 上不起作用
- ruby-on-rails - 语法错误,意外的',',期待')'在纤细的轨道
- javascript - 从打字稿中的方法装饰器获取参数列表
- node.js - Alexa Cloud Service 与 Amazon Echo Dot/Alexa App 交互的方式/方法是什么?
- linux - Java ME / J2ME 使计算机硬重置
- c# - 在 VS Code 上使用 c# 代码获取所需输出的问题
- apache-nifi - 在 Apache NiFi 中使用 csv 的值查询数据库表
- c - 没有规则使 xo NASM 需要目标并生成 .o 文件作为输出。tthis 错误是什么意思 - 如何纠正它
- android - 如何截取相同布局的视频视图和文本视图