use*_*144 3 amazon-sqs amazon-web-services node.js aws-cloudformation aws-lambda
看起来非常简单,但很难找到很好的例子.
因此,任务如下:AWS lambda向AWS-SQS发送一些消息.
AWS lambda代码包含以下行:
var QUEUE_URL = 'https://sqs.us-west-2.amazonaws.com/ID/QUEUE_NAME';",
Run Code Online (Sandbox Code Playgroud)
为了摆脱这个代码,有两种选择:
基于这一事实,周期性触发器(lambda)将每天工作多次,最好指定此依赖性duting部署.
通常,它看起来像是直接的任务和云编队脚本创建:
"Resources": {
"LF2HNR1": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Description": "This is lambda trigger",
"Handler": "index.myHandler",
"Runtime": "nodejs",
"Timeout": "300",
Run Code Online (Sandbox Code Playgroud)
并且还指定了依赖于SQS的依赖:
"DependsOn": [
"SQSQ562D4"
]
},
"SQSQ562D4": {
"Type": "AWS::SQS::Queue",
"Properties": {},
}
Run Code Online (Sandbox Code Playgroud)
Hovewer如何以编程方式获取lambda代码中的SQS url,这不是直截了当的任务:
exports.handler = function(event, context) {
var params = {
MessageBody: JSON.stringify(event),
var QUEUE_URL = ????
Run Code Online (Sandbox Code Playgroud)
主要的复杂性是正确使用CloudFormaion API来获取SQS URL.
为了做到这一点,我使用了以下主要由此API驱动的代码:
var queueURL;
cloudFormation.describeStackResource(cloudFormationParams, function(err, data) {
if (err){
console.log(err, err.stack); // an error occurred
}
else {
var queueURL =data.StackResourceDetail.PhysicalResourceId;
var params = {
MessageBody: JSON.stringify(event),
QueueUrl: queueURL
};
sqs.sendMessage(params, function(err,data){
if(err) {
context.done('error', "ERROR Put SQS"); // ERROR with message
}else{
console.log('data:',data.MessageId);
context.done(null,''); // SUCCESS
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1052 次 |
| 最近记录: |