async function processMessageFromQueue()

in source/services/queue-consumer/index.ts [204:246]


async function processMessageFromQueue(msg: AWS.SQS.Message, msgReceiptHandle: string): Promise<AWS.SSM.StartAutomationExecutionResult> {
    if (!msg.Body) {
        throw (new Error('SQS Message did not contain a Body'));
    }

    let parsedMsgBody = null;

    try {
        parsedMsgBody = JSON.parse(msg.Body);
    } catch (err) {
        throw (new Error('Unable to parse the SQS Message Body'));
    }

    if (!parsedMsgBody.AutomationDocumentName) {
        // Without the AutomationDocumentName here, we don't know what automation document to execute in SSM
        throw (new Error('SQS Message Body is missing AutomationDocumentName'));
    }

    const ssmParams: AWS.SSM.StartAutomationExecutionRequest = {
        DocumentName: parsedMsgBody.AutomationDocumentName,
        Parameters: {
            SQSMsgBody: [JSON.stringify(parsedMsgBody)],
            SQSMsgReceiptHandle: [msgReceiptHandle]
        }
    };

    // Set parameters that are specific to a given action
    parsedMsgBody.TaskParameters.forEach((taskParameter: any) => {
        if (taskParameter.name !== 'SQSMsgBody' && taskParameter.name !== 'SQSMsgReceiptHandle') {
            if (taskParameter.value && taskParameter.value.trim() !== '') {
                ssmParams.Parameters[taskParameter.name] = [taskParameter.value];
            }
        }
    });

    try {
        const data = await ssm.startAutomationExecution(ssmParams).promise();
        return data;
    } catch (err) {
        logger.error('Error while starting an automation document in SSM');
        throw (err);
    }
}