简体   繁体   English

跨区域调用AWS服务

[英]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). 我正在尝试通过俄亥俄州(us-east-2)上的lambda函数(Node.js 8.10)使用位于圣保罗(sa-east-1)的SNS。 This is the first time I try to use a AWS service located in another region. 这是我第一次尝试使用另一个地区的AWS服务。 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. 我为lambda函数设置了一个非常高的超时时间:5分钟。

I also have tested the same code on a lambda function located in São Paulo(sa-east-1), and it works. 我还在位于圣保罗(sa-east-1)的lambda函数上测试了相同的代码,并且可以正常工作。

I have been receiving the following error on my client: "Request failed with status code 504" "Endpoint request timed out" 我在客户端上收到以下错误:“请求失败,状态代码为504”“端点请求超时”

Question: How can I use SNS in another AWS region correctly? 问:如何在另一个AWS区域中正确使用SNS?

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): 例如,我使用以下模式将通知从us-east-1发送到东京(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? 您是否需要在VPC中运行Lambda? That's the only complication I can think of at the moment. 这是我目前能想到的唯一并发症。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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