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

如何通过AWS CLI查询API资源关联的VPC_LINK信息

解决方法

你已经通过aws apigateway get-integration拿到的connectionId就是VPC Link的ID,以下是批量/编程式获取对应VPC Link名称及详情的方法:

直接用返回的connectionId作为参数调用get-vpc-link命令,即可获取该VPC Link的名称和完整配置:

aws apigateway get-vpc-link --vpc-link-id xcvbn

返回结果中的name字段就是VPC Link的名称,同时会包含VPC配置、状态等其他信息。

2. 批量匹配VPC Link信息

如果需要批量处理大量API资源的集成,可以先拉取全量VPC Link列表建立映射,再和集成信息自动匹配:

步骤1:导出所有VPC Link列表

# 获取当前账号下所有VPC Link并保存为JSON文件
aws apigateway get-vpc-links --output json > vpc-links.json

步骤2:用脚本批量遍历API资源与集成

以Bash+jq为例,批量处理单个API的所有资源及关联VPC Link:

REST_API_ID="123abcxyz"

# 获取当前API的所有资源ID
RESOURCE_IDS=$(aws apigateway get-resources --rest-api-id $REST_API_ID --output json | jq -r '.items[].id')

for RESOURCE_ID in $RESOURCE_IDS; do
    # 获取该资源的所有HTTP方法
    HTTP_METHODS=$(aws apigateway get-methods --rest-api-id $REST_API_ID --resource-id $RESOURCE_ID --output json | jq -r '.[].httpMethod')
    
    for METHOD in $HTTP_METHODS; do
        # 获取集成信息
        INTEGRATION=$(aws apigateway get-integration --rest-api-id $REST_API_ID --resource-id $RESOURCE_ID --http-method $METHOD --output json)
        CONNECTION_TYPE=$(echo $INTEGRATION | jq -r '.connectionType')
        
        if [ "$CONNECTION_TYPE" = "VPC_LINK" ]; then
            CONNECTION_ID=$(echo $INTEGRATION | jq -r '.connectionId')
            # 从预存的VPC Link列表中匹配名称
            VPC_LINK_NAME=$(jq -r --arg id "$CONNECTION_ID" '.items[] | select(.id == $id) | .name' vpc-links.json)
            
            echo "API资源ID: $RESOURCE_ID, 请求方法: $METHOD, VPC Link ID: $CONNECTION_ID, VPC Link名称: $VPC_LINK_NAME"
        fi
    done
done

3. 用AWS SDK实现跨API批量处理

如果需要遍历账号下所有API的资源集成,可以用Python的boto3 SDK编写脚本:

import boto3

# 初始化API Gateway客户端
client = boto3.client('apigateway')

# 建立VPC Link ID到名称的映射字典
vpc_links = client.get_vpc_links()['items']
vpc_link_map = {link['id']: link['name'] for link in vpc_links}

# 遍历账号下所有API
rest_apis = client.get_rest_apis()['items']
for api in rest_apis:
    api_id = api['id']
    print(f"=== 处理API: {api['name']} ({api_id}) ===")
    
    # 获取当前API的所有资源
    resources = client.get_resources(restApiId=api_id)['items']
    for resource in resources:
        resource_id = resource['id']
        
        # 获取资源的所有请求方法
        methods = client.get_methods(restApiId=api_id, resourceId=resource_id)
        for method in methods:
            http_method = method['httpMethod']
            
            # 获取集成信息(跳过无集成的方法)
            try:
                integration = client.get_integration(
                    restApiId=api_id,
                    resourceId=resource_id,
                    httpMethod=http_method
                )
            except client.exceptions.NotFoundException:
                continue
            
            if integration.get('connectionType') == 'VPC_LINK':
                connection_id = integration['connectionId']
                vpc_link_name = vpc_link_map.get(connection_id, '未找到对应VPC Link')
                print(f"资源ID: {resource_id}, 方法: {http_method}, VPC Link ID: {connection_id}, 名称: {vpc_link_name}")

以上方法均可实现全自动化批量获取,无需手动操作控制台。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 22:12:40