AWS Lambda NodeJS,无法建立外部连接

编程入门 行业动态 更新时间:2024-10-09 23:19:09
本文介绍了AWS Lambda NodeJS,无法建立外部连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

因此,我正在尝试做一个基本的事情:从我的AWS Lambda脚本连接到外部REST-API.该API托管假期列表.

So, I'm trying to do a basic thing: Connect to an external REST-API from my AWS Lambda script. This API hosts a list of holidays.

但是,每当我尝试执行代码时,它就会超时(达到最大lambda执行时间).

But, whenever I try to execute the code it just times out (max lambda execution time reached).

因此,我创建了此包装器函数,该函数能够处理4种不同的方式来执行 GET 请求,但是它们全部执行相同的操作.

So I created this wrapper function, that is capable of handling 4 different ways of doing GET requests, but all of them perform the same.

const request = require('request') const https = require('https') const axios = require('axios') const superagent = require('superagent') let test = (type = "") => { return new Promise((resolve, reject) => { debug("Fetching with: " + type) const d = new Date() if(type == "superagent"){ superagent.get('holidayapi.pl/v1/holidays?country=DK&year=' + d.getFullYear()) .query({ country: 'DK', year: '2019' }) .end((err, res) => { if (err) { console.log(err) reject(err) } else { console.log(res) resolve(res) } }) } else if(type == "axios"){ axios.get('holidayapi.pl/v1/holidays?country=DK&year=' + d.getFullYear()) .then(response => { debug(response) resolve(response) }) .catch(error => { console.log(error) reject(error) }) } else if(type == "https"){ const req = https.get("holidayapi.pl/v1/holidays?country=DK&year=" + d.getFullYear(), (resp) => { let data = '' // A chunk of data has been recieved. resp.on('data', (chunk) => { data += chunk }) // The whole response has been received. Print out the result. resp.on('end', () => { console.log(JSON.parse(data).explanation) resolve([]) }) }) .on('error', (e) => { debug(e) reject(e.message) }) req.end() } else if(type == "request"){ request('holidayapi.pl/v1/holidays?country=DK&year=' + d.getFullYear(), { json: true }, (err, res, body) => { debug(err) debug(res) debug(body) if (err) reject(err) else resolve(body.holidays) }) } else { reject("Mangler type") } }) } exports.connect_test = (event, context, callback) => { test(event.pathParameters.type) .then((rsp) => { callback(null, JSON.stringify(rsp, null, 2)) }) .catch((err) => { callback(null, JSON.stringify(err, null, 2)) }) }

debug 函数是console.log的映射,用于检查NODE_ENV是否为"dev".

The debug function is a map to console.log, that checks if the NODE_ENV is "dev".

推荐答案

  • 我想默认的VPC可以连接互联网,因此,如果它是默认的,它就可以正常工作(只需将lambda的默认超时时间增加到合理的水平即可).
  • 如果它位于VPC内,则需要为该VPC配置NAT网关/NAT实例,以使其具有Internet连接或与具有Internet访问权限的另一个VPC配对(在VPC lambda内部需要具有适当的角色和子网).
  • 这可能会帮助您 aws文档

更多推荐

AWS Lambda NodeJS,无法建立外部连接

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

发布评论

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

>www.elefans.com

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