mingxingwang
mingxingwang

Reputation: 339

Can not call external api in aws lambda function

I was going to call third party api in the aws lambda function with request. But it shows timeout error. I set the lambda function time 30s and when I call that api by using postman it send response(error) with in 5 seconds. I think the request or axios can't send the request to the external api or can't receive the response. Can anyone help me? My code:

  request.post({url:url2, json:true, body:body, headers: {
    'ACCESS_TIMESTAMP': timestamp,
    'Content-Type': 'application/json'
  } }, (error, response, body) => {
  console.log("------------------I am here----------------------")
  callback(null, error)
})

When I use the axios the code is like below:

let jsonbody = JSON.stringify(body)
axios.post(url2, jsonbody,  {headers: {
      'ACCESS_TIMESTAMP': timestamp,
      'Content-Type': 'application/json'
    }})
.then(res => {
  callback(null, res.data)
})
.catch(err => {
  callback(null, err)
})

I want to get the result of res or err but they don't act.

Upvotes: 2

Views: 2833

Answers (1)

Lamanus
Lamanus

Reputation: 13551

When you locate the Lambda into your VPC with the private subnet, then you have to set the NAT gateway into your private subnet. If you didn't connect the NAT, Lambda cannot connect to the public internet and can communicate with the private IPs only.

Upvotes: 5

Related Questions