// Copyright Abridged, Inc. 2021,2024. All Rights Reserved. // Node module: @collabland/aws // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT import {isMain} from '@collabland/common'; import {getIAMRoleArn} from '../aws-clients/index.js'; import {setAwsCredentialsToEnv, STSServiceClient} from '../index.js'; async function main() { let service = new STSServiceClient(); const id = await service.getCallerIdentity(); console.log('Caller identity: %O', id); const role = await service.assumeRole( getIAMRoleArn('collabland-api-server-qa-ec2-role'), ); console.log('Assumed role: %O', role); const credentials = role.Credentials!; const config = { accessKeyId: credentials.AccessKeyId!, secretAccessKey: credentials.SecretAccessKey!, expiration: credentials.Expiration, sessionToken: credentials.SessionToken, }; // Set up AWS credentials as environment variables setAwsCredentialsToEnv(config); // Create a new instance of STSServiceClient to pick up the environment // variable changes service = new STSServiceClient(); const newId = await service.getCallerIdentity(); console.log('New caller identity: %O', newId); } if (isMain(import.meta.url)) { await main(); }