AWS Lambda NodeJS,无法建立外部连接 [英] AWS Lambda NodeJS, can't make external connection

查看:53
本文介绍了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('https://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('https://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("https://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('https://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,无法建立外部连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆