AWS Lambda和SQL Server集成

编程入门 行业动态 更新时间:2024-10-23 03:30:56
本文介绍了AWS Lambda和SQL Server集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的应用程序使用MS Sql Server.现在,在了解了AWS Lambda之后,我想将我的应用程序转移到AWS Lambda以具有无服务器架构.

My application uses MS Sql Server. Now after knowing about AWS Lambda, I want to shift my application to AWS Lambda to have server-less architecture.

但是正如我提到的,应用程序使用Sql Server.因此,我不确定AWS Lambda是否支持与Sql Server的连接.

But as I mentioned, application uses Sql Server. So I am not sure if AWS Lambda supports connection with Sql Server.

任何评论/链接都会有所帮助.

Any comments/ links will be helpful.

推荐答案

下面是一些示例示例,其中带有注释以从Lambda连接到MS SQL Server数据库(假设使用NodeJS作为您的语言).

Here is some example boilerplate with comments to connect to an MS SQL Server database from Lambda (Assuming using NodeJS as your language).

const sql = require('mssql'); exports.handler = async (event, context, callback) => { let lookupValue = event.lookupValue; // Take DB Config from environment variables set in Lambda config const config = { user: process.env.DB_USERNAME, password: process.env.DB_PASSWORD, server: process.env.DB_SERVER, database: process.env.DB_DATABASE, options: { encrypt: true // Use this if you're on Windows Azure } } try { // Open DB Connection let pool = await sql.connect(config) // Query Database let result = await pool.request() .input('lookupValue', sql.Int, lookupValue) .query('select * from exampleTable where id = @lookupValue'); // Close DB Connection pool.close(); // The results of our query console.log("Results:", result.recordset); // Use callback if you need to return values from your lambda function. // Callback takes (error, response?) as params. callback(null, result.recordset); } catch (err) { // Error running our SQL Query console.error("ERROR: Exception thrown running SQL", err); } sql.on('error', err => console.error(err, "ERROR: Error raised in MSSQL utility")); }

注意:您需要将运行mssql所需的node_modules上传到函数中.我发现最简单的方法是将整个文件夹(您的主要[通常为index.js]函数文件以及package.json和您的node_modules文件夹)压缩在一起,然后使用aws-cli上传:

Note: You will need to upload to your function the node_modules required to run mssql. The easiest way I've found to do this is to zip up the whole folder (your main [usually index.js] function file, along with package.json and your node_modules folder), and then upload it using the aws-cli:

aws lambda update-function-code --function-name your-function-name-here --zip-file your-zipped-project-directory.zip

最后,确保您的数据库能够接受来自AWS Lambda函数的连接.最好的方法是结合使用AWS的VPC,NAT和Elastic IP设置-此示例在此博客文章中有所描述: medium/@matthewleak/aws-lambda-functions-with-a-static-ip-89a3ada0b471

Finally, make sure that your database is able to accept connections from the AWS Lambda function. The best way to do this is to use a combination of AWS's VPC, NAT, and Elastic IP settings - and example of which is described in this blog post: medium/@matthewleak/aws-lambda-functions-with-a-static-ip-89a3ada0b471

更多推荐

AWS Lambda和SQL Server集成

本文发布于:2023-11-24 04:25:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623961.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Lambda   AWS   Server   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!