跨区域调用 AWS 服务
Invoke AWS Service across regions
我正在尝试通过位于俄亥俄州 (us-east-2) 的 lambda 函数 (Node.js 8.10) 使用位于圣保罗 (sa-east-1) 的 SNS。这是我第一次尝试使用位于另一个区域的 AWS 服务。到目前为止,这就是我正在做的事情:
//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
...
}
}
我为我的 lambda 函数设置了一个非常高的超时:5 分钟。
我还在位于圣保罗 (sa-east-1) 的 lambda 函数上测试了相同的代码,它有效。
我的客户端收到以下错误:
"Request failed with status code 504"
"Endpoint request timed out"
问题:如何在其他AWS区域正确使用SNS?
除了设置区域之外,您不需要进行任何特殊设置。
例如,我使用以下模式从 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
}
没有设置端点等。您需要在 VPC 中 运行 您的 lambda 吗?这是我目前唯一能想到的并发症。
我正在尝试通过位于俄亥俄州 (us-east-2) 的 lambda 函数 (Node.js 8.10) 使用位于圣保罗 (sa-east-1) 的 SNS。这是我第一次尝试使用位于另一个区域的 AWS 服务。到目前为止,这就是我正在做的事情:
//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
...
}
}
我为我的 lambda 函数设置了一个非常高的超时:5 分钟。
我还在位于圣保罗 (sa-east-1) 的 lambda 函数上测试了相同的代码,它有效。
我的客户端收到以下错误: "Request failed with status code 504" "Endpoint request timed out"
问题:如何在其他AWS区域正确使用SNS?
除了设置区域之外,您不需要进行任何特殊设置。
例如,我使用以下模式从 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
}
没有设置端点等。您需要在 VPC 中 运行 您的 lambda 吗?这是我目前唯一能想到的并发症。