gpt4 book ai didi

aws-lambda - 在内联 Lambda Nodejs 代码中使用 GetAtt

转载 作者:行者123 更新时间:2023-12-03 07:29:35 24 4
gpt4 key购买 nike

我创建了一个简单的云形成模板,它将一个 SQS 队列的“消息拆分器”组合在一起,并将消息分发到其他 4 个队列中。

Cloudformation 模板的相关部分是

ProdSqsDistributor:
Type: 'AWS::Lambda::Function'
Properties:
Code:
ZipFile: |
var AWS = require('aws-sdk');
var sqs = new AWS.SQS({region : 'us-east-1'});
var sns = new AWS.SNS({region : 'us-east-1'});

exports.handler = async (event) => {
//console.log('Received event:', JSON.stringify(event, null, 2));
const promises = event.Records.map( async message => {
var messageId = message.messageId;
var body = message.body;
console.log('SQS message %s: %j', messageId, body);
if (body.indexOf('App\\\\Jobs\\\\CNC\\\\ConvertFile') !== -1){
console.log('cnc1 matched');

await sqs.sendMessage({MessageBody: body, QueueUrl: '!GetAtt ProdCnc.Arn'})


...

ProdCnc:
Type: 'AWS::SQS::Queue'
Properties:
DelaySeconds: 0
VisibilityTimeout: 120
ReceiveMessageWaitTimeSeconds: 20

根据模板,我想将目标队列的 arn 嵌入到内联 Lambda Nodejs 代码中。

Cloudformation 模板运行成功,但队列拆分不起作用。当我查看创建的 lambda 函数时,似乎 GetAtt 调用尚未解析:

enter image description here

我正在寻找一种在内联 Lambda 代码中解析/执行 GetAtt 调用的方法。

最佳答案

尝试 !Sub:

ProdSqsDistributor:
Type: 'AWS::Lambda::Function'
Properties:
Code:
ZipFile: !Sub
- |
var AWS = require('aws-sdk');
var sqs = new AWS.SQS({region : 'us-east-1'});
var sns = new AWS.SNS({region : 'us-east-1'});

exports.handler = async (event) => {
//console.log('Rcvd event:', JSON.stringify(event, null, 2));
const promises = event.Records.map( async message => {
var messageId = message.messageId;
var body = message.body;
console.log('SQS message %s: %j', messageId, body);
if (body.indexOf('App\\\\Jobs\\\\CNC\\\\ConvertFile') !== -1){
console.log('cnc1 matched');

await sqs.sendMessage({MessageBody: body, QueueUrl: '${ProdCncArb}'})
- ProdCncArb: !GetAtt ProdCnc.Arn

关于aws-lambda - 在内联 Lambda Nodejs 代码中使用 GetAtt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57138111/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com