首页 > 解决方案 > 如何在 typescript 中获取我的个人资料的 aws 区域

问题描述

我在 .aws/config 中为特定的 aws 配置文件配置了我的 aws 区域。
是否可以在我的 node.js 服务中获取设置区域(例如 us-west-1)的值?
是否有我可以读取的环境变量?

标签: node.jstypescriptamazon-web-services

解决方案


您可以运行aws configure get region以获取默认配置文件的区域。

如果您添加--profile,您将获得该特定配置文件的区域aws configure get region --profile otherprofile

您可以使用exec从 nodejs 运行命令。

const { exec } = require('child_process')

exec('aws configure get region', (err, stdout, stderr) => {
 const region = stdout

 console.log(region)
})

但我认为最好将该区域保存为环境变量并从那里读取。


推荐阅读