简体   繁体   English

使用凭据创建 AWS SSM 客户端

[英]Creating AWS SSM client with credentials

I'm trying to create a SSMClient in JavaScript/TypeScript.我正在尝试在 JavaScript/TypeScript 中创建一个 SSMClient。 I've found a ton of examples but nothing seems to work.我找到了很多例子,但似乎没有任何效果。 I'm trying to get a value from the SSM parameter store.我正在尝试从 SSM 参数存储中获取值。 Here is my latest:这是我的最新消息:

    const stsClient = new STSClient({ region: REGION });

    const params = {
      RoleArn: "arn:aws:iam::425112775363:policy/SSMFullAccessCognito",
      RoleSessionName: "session1",
      DurationSeconds: 900,
    };


    //Assume Role
    const data = await stsClient.send(new AssumeRoleCommand(params));
    const rolecreds = {
      accessKeyId: data.Credentials!.AccessKeyId,
      secretAccessKey: data.Credentials!.SecretAccessKey,
      sessionToken: data.Credentials!.SessionToken,
    };

    const ssmClient = new SSMClient({ region: REGION  });

    console.info(ssmClient);
    
    const cmd = new GetParameterCommand({ Name: 'test', WithDecryption: false });

    const result = await ssmClient.send(cmd);

    console.info(result);

With the above it says creds are missing, which they are.上面说的是信用缺失,它们确实存在。 I just can't anywhere to convert "rolecreds" to Somethng SSM wants.我无法在任何地方将“rolecreds”转换为 SSM 想要的东西。 I can assume the role fine and I get back valid creds.我可以很好地承担这个角色,并且我会取回有效的证书。

I've found 100 different ways from multiple sources but nothing works.我从多个来源找到了 100 种不同的方法,但没有任何效果。 I'm running AWSv3.我正在运行 AWSv3。

EDIT: I am using Amplify too.编辑:我也在使用 Amplify。

You don't seem to actually using roleCreds .您似乎并没有实际使用roleCreds If you look at the documentation for SSMClient , you'll see that it takes an optional credential object which you need to use in your situation: ( https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-ssm/interfaces/ssmclientconfig.html#credentials )如果您查看SSMClient的文档,您会看到它需要一个可选的凭证 object,您需要在您的情况下使用它:( https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/客户端 ssm/interfaces/ssmclientconfig.html#credentials

Assuming the SSMFullAccessCognito role has the correct permissions to access the required SSM parameter, what you have to do is to pass the rolecreds object to the SSMClient .假设SSMFullAccessCognito角色具有访问所需 SSM 参数的正确权限,您需要做的是将rolecreds object 传递给SSMClient You can do the following:您可以执行以下操作:

const data = await stsClient.send(new AssumeRoleCommand(params));
const rolecreds = {
    accessKeyId: data.Credentials!.AccessKeyId!,
    secretAccessKey: data.Credentials!.SecretAccessKey!,
    sessionToken: data.Credentials!.SessionToken!,
};

const ssmClient = new SSMClient({ region: REGION, credentials: rolecreds });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM