简体   繁体   中英

Invoke AWS Service across regions

I am trying to use SNS located in São Paulo (sa-east-1) from a lambda function (Node.js 8.10) on Ohio (us-east-2). This is the first time I try to use a AWS service located in another region. So far, this is what I am doing:

//init aws resources
const AWS = require('aws-sdk');
const sns = new AWS.SNS({apiVersion: '2010-03-31', region: 'sa-east-1'});

//promisefy AWS.SNS.createPlatformEndpoint method
snsCreatePlatformEndpoint = params => new Promise(
  (resolve, reject)=>{
    sns.createPlatformEndpoint(params, function(error, data){
      if (error) { reject(error); }
      else { resolve(data); }
    });
  }
);

exports.handler = (awsEvent, context, callback) => {
  //parse stuff in here
  ...

  HandleToken(token, callback);
};

async function HandleToken(token, callback){
  try{
    let params = {
      PlatformApplicationArn: process.env.PlatAppArn, 
      Token: token,
    };
    console.log('params:', params); // this prints as expected
    let {EndpointArn} = await snsCreatePlatformEndpoint(params);
    console.log('It should pass through here'); // it is not printed
    //returns a success response
    ...
  } catch (error) {
    //returns an error response
    ...
  }
}

I have set a really high timeout for my lambda function: 5mins.

I also have tested the same code on a lambda function located in São Paulo(sa-east-1), and it works.

I have been receiving the following error on my client: "Request failed with status code 504" "Endpoint request timed out"

Question: How can I use SNS in another AWS region correctly?

You shouldn't need to do any special setup beyond setting the region.

Eg, I use the following pattern to send notifications from us-east-1 to Tokyo (ap-northeast-1):

// this lambda runs in us-east-1

let AWS = require("aws-sdk");
AWS.config.update({ region: "ap-northeast-1" }); // asia-pacific region

exports.handler = async (event, context) => {
    var params = {
      Message: 'my payload',
      TopicArn: 'arn:aws:sns:ap-northeast-1:xxxxxx:tokyoSNS'
    };

    let SNS = new AWS.SNS({apiVersion: '2010-03-31'});
    var data = await SNS.publish(params).promise();

    // check if successful then return    
}

No endpoints, etc., was setup. Are you required to run your lambda in a VPC? That's the only complication I can think of at the moment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM