You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用API或Node.js SDK v3获取AWS各服务支持区域?求替代方案

用AWS Node.js SDK v3获取各服务支持区域的通用方案

在AWS SDK v3后期版本移除了服务类内置的区域列表方法后,以下两种通用方案可以解决这个问题:

方案一:利用SSM Parameter Store公共参数(推荐)

AWS官方在SSM参数存储中维护了全量服务的支持区域数据,参数路径格式为/aws/service/global-infrastructure/services/<服务代码>/regions,返回值是包含所有支持区域的JSON数组。

代码示例

import { SSMClient, GetParameterCommand } from "@aws-sdk/client-ssm";

// 初始化SSM客户端(公共参数全局可用,指定任意有效区域即可)
const ssmClient = new SSMClient({ region: "us-east-1" });

/**
 * 获取指定AWS服务的支持区域
 * @param {string} serviceCode - AWS服务代码,例如"s3"、"ec2"、"lambda"
 * @returns {Promise<string[]>} 支持区域列表
 */
async function getServiceRegions(serviceCode) {
  const params = {
    Name: `/aws/service/global-infrastructure/services/${serviceCode}/regions`,
    WithDecryption: false
  };

  try {
    const command = new GetParameterCommand(params);
    const response = await ssmClient.send(command);
    return JSON.parse(response.Parameter.Value);
  } catch (error) {
    console.error(`获取${serviceCode}区域失败:`, error);
    throw error;
  }
}

// 使用示例
getServiceRegions("lambda")
  .then(regions => console.log("Lambda支持区域:", regions))
  .catch(err => console.error(err));

注意事项

  • 需要为IAM实体授予ssm:GetParameter权限,权限范围可指定为/aws/service/global-infrastructure/services/*
  • 服务代码需与AWS官方定义一致,例如S3对应s3、CloudFormation对应cloudformation,可通过AWS官方文档查询对应服务代码

方案二:通过Service Quotas API补充验证

此方法仅能获取服务的默认关联区域,无法返回全量支持区域,适合快速验证服务是否在某区域可用:

代码示例

import { ServiceQuotasClient, ListServicesCommand } from "@aws-sdk/client-service-quotas";

const serviceQuotasClient = new ServiceQuotasClient({ region: "us-east-1" });

async function getServiceDefaultRegion(serviceCode) {
  try {
    const listServicesCommand = new ListServicesCommand({ MaxResults: 100 });
    const servicesResponse = await serviceQuotasClient.send(listServicesCommand);
    const targetService = servicesResponse.Services?.find(s => s.ServiceCode === serviceCode);
    return targetService?.Region || null;
  } catch (error) {
    console.error(`通过Service Quotas获取区域失败:`, error);
    throw error;
  }
}

// 使用示例
getServiceDefaultRegion("s3")
  .then(region => console.log("S3默认关联区域:", region))
  .catch(err => console.error(err));

内容的提问来源于stack exchange,提问作者Aditya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 09:52:33