0
votes

I have created a simple cloud formation template that holds together a "message splitter" for one SQS queue, with the messages being distributed into 4 other queues.

The relevant parts of the Cloudformation template are

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

As per the template, I want to embed the arn of the target queue into the inline Lambda nodejs code.

The Cloudformation template runs successfully, however the queue splitting does not work. When I look at the created lambda function, it appears as if the GetAtt call has not been resolved:

enter image description here

I'm looking for a way to resolve/execute the GetAtt call inside the inlined Lambda code.

1

1 Answers

3
votes

Try !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