Search code examples
amazon-web-servicesaws-lambdaaws-step-functionsaws-sdk-js

Why doesn't my Lambda function start an execution on an AWS State Machine?


I have set up an AWS Step Function state machine that chains 3 lambda functions and runs perfectly fine when I manually create an execution. I created a fourth lambda function to take input from API Gateway, pass what I want to the Step Function and start a new execution.

I believe I've followed all of the advice I could find online for this, but no matter what I've tried, the Lambda function always seems to time out (whether invoked from API Gateway or tested on it's own), and I get no errors in CloudWatch to speak of, even though I've moved console.log lines to just about every point in the function. It appears as though the stepfunctions.StartExecution line is never actually called, but the function also never returns either (whether the callback it is inside the stepfunction block or not), so I'm left beating my head against the desk.

Here is the Lambda function:

'use strict';
const AWS = require('aws-sdk');
const moment = require('moment');

exports.postArchive = (event, context, callback) => {
    const parsedInput = JSON.parse(event.body);
    const stepInput = JSON.stringify({ "thingId": parsedInput.thingId });
    const rightNow = moment().format('YYYYMMDD-hhmmss');
    const params = {
        stateMachineArn: 'arn:aws:states:us-east-1:ACCOUNT_ID:stateMachine:create-archive',
        input: stepInput,
        name: `ArchiveAt${rightNow}`
    };
    console.log(JSON.stringify(params));
    const stepfunctions = new AWS.StepFunctions({apiVersion: '2016-11-23'});

    stepfunctions.startExecution(params, function (err, data) {
        if (err) {
            console.log(err, err.stack);
            callback(err, null);
            return;
        }
        const response = {
            statusCode: 200,
            headers: {
                "Content-Type": "application/json"
            },
            body: JSON.stringify({
                message: "State Machine started successfully.",
                result: data
            })
        };
        callback(null, response);
     });
};

Also, the Lambda IAM role has full access to AWS Step Functions, so I don't believe there is a problem there.


Solution

  • There was apparently an issue with one or all of the VPC/Subnets/Security Group combo I was using for the Lambda function to start execution on a state machine.

    There isn't any documentation out there on this that I could find, so I might be wrong or missing something, but this is what worked for me. It's probably okay since I can still run all the functions that actually interact with a database inside a private subnet and chain them in Step Functions.