从 Azure Cloud Function 调用 CosmosDB 服务器



我正在开发一个Azure云函数(在节点js上运行(,它应该从我的Azure Cosmos DB for MongoDB API帐户返回文档集合。

当我在本地生成和启动函数时,一切正常,但在将其部署到 Azure 时失败。这是错误:MongoNetwork错误: 无法在第一次连接时连接到服务器 [++++.mongo.cosmos.azure.com:++++] ...

我是CosmosDB和Azure云函数的新手,所以我正在努力找到问题。我在门户中查看了防火墙和虚拟网络设置,并尝试了连接字符串的不同变体。

由于它似乎在本地工作,因此我认为它可能是门户中的配置设置。
有人可以帮助我吗?

1.设置连接
我使用了门户提供的主连接字符串。

import * as mongoClient from 'mongodb';
import { cosmosConnectionStrings } from './credentials';
import { Context } from '@azure/functions';
// The MongoDB Node.js 3.0 driver requires encoding special characters in the Cosmos DB password. 
const config = {
url: cosmosConnectionStrings.primary_connection_string_v1,
dbName: "****"
};
export async function createConnection(context: Context): Promise<any> {
let db: mongoClient.Db;
let connection: any;
try {
connection = await mongoClient.connect(config.url, {
useNewUrlParser: true,
ssl: true
}); 
context.log('Do we have a connection? ', connection.isConnected());
if (connection.isConnected()) {
db = connection.db(config.dbName);
context.log('Connected to: ', db.databaseName);
}
} catch (error) {
context.log(error);
context.log('Something went wrong');
}
return {
connection,
db
};
}



2. 主函数 执行查询并返回集合的主函数

const httpTrigger: AzureFunction = async function (context: Context, req: HttpRequest): Promise<void> {
context.log('Get all projects function processed a request.');
try {
const { db, connection } = await createConnection(context);
if (db) {
const projects = db.collection('projects')
const res = await projects.find({})
const body = await res.toArray()
context.log('Response projects: ', body);
connection.close()
context.res = {
status: 200,
body
}
} else {
context.res = {
status: 400,
body: 'Could not connect to database'
}; 
}
} catch (error) {
context.log(error); 
context.res = {
status: 400,
body: 'Internal server error'
}; 
}  
};

我再次查看了防火墙和专用网络设置,并阅读了有关配置 IP 防火墙的官方文档。默认情况下,本地机器的当前IP地址会添加到IP白名单中。这就是该函数在本地工作的原因。

根据文档,我尝试了下面描述的所有选项。他们都为我工作。但是,仍然不清楚为什么我必须手动执行操作才能使其工作。我也不确定哪个选项是最好的。

  • 将"允许从以下位置访问所有网络"设置为"所有网络
    "(包括互联网(都可以访问数据库(显然不建议这样做(

  • 将云函数项目的入站和出方向 IP 地址添加到白名单
    如果 IP 地址随时间变化,这可能会具有挑战性。如果您在消费计划中,这可能会发生。

  • 如果从不
  • 访问 Azure Cosmos DB 帐户的服务,请检查"例外"部分中的"接受来自公共 Azure 数据中心内的连接"选项

    提供静态 IP(例如,Azure 流分析和 Azure 函数(,您仍然可以使用 IP 防火墙来限制访问。您可以 通过选择 接受来自 Azure 数据中心内的连接选项。

    This option configures the firewall to allow all requests from Azure, including requests from the subscriptions of other customers deployed in Azure. The list of IPs allowed by this option is wide, so it limits the effectiveness of a firewall policy. Use this option only if your requests don’t originate from static IPs or subnets in virtual networks. Choosing this option automatically allows access from the Azure portal because the Azure portal is deployed in Azure.

最新更新