Metal Wing
Metal Wing

Reputation: 1175

node.js Lambda only runs once (all subsequent attempts fail)

My lambda attempts to do the following: Given a particular object in S3: unzip it on the fly, and parse some text (UUID) out of it.

My issue is that it only works once. Here's my code

const aws = require('aws-sdk');
const gzip = require('zlib').createGunzip();
const s3 = new aws.S3({region: 'us-east-1', apiVersion: '2006-03-01'});

exports.handler = async (event, context) => {
    const bucket = event.bucket;
    let key = event.key;

    let uuid = '';
    try {
        uuid = await getUuidFromFile(bucket, key);
    }
    catch (err) { console.log("Error!!", err) }

    return {
        statusCode: 200,
        body: JSON.stringify(uuid || {message: `Failed to retrieve UUID for ${deviceName}`})
    }

};

function getUuidFromFile(bucket, key) {
    let string = '';
    let uuid;

    return new Promise((resolve, reject) => {
        s3.getObject({Bucket: bucket, Key: key})
            .createReadStream()
            .pipe(gzip)
            .on('data', (data) => {
                string += data.toString();
            })
            .on('end', () => {
                uuid = parseUUID(string.substring(0, 150));
                console.log("UUID", uuid);
                resolve(uuid);
            })
            .on('err', (err) => {
                reject(err);
            });
    });
}

function parseUUID(data) {
    // do some actual parsing on the data and get the uuid
    return "uuid";
}

When I deploy my lambda and run it for a particular key - it works: finds the file, unzips it, parses it, and returns UUID. Any subsequent runs for the SAME key return absolutely nothing.

Upvotes: 1

Views: 243

Answers (1)

Metal Wing
Metal Wing

Reputation: 1175

I think figured it out.

Before:

const gzip = require('zlib').createGunzip();
...
s3.getObject({Bucket: bucket, Key: key})
        .createReadStream()
        .pipe(gzip)

After:

const zlib = require('zlib');
...
s3.getObject({Bucket: bucket, Key: key})
        .createReadStream()
        .pipe(zlib.createGunzip())

That's all! Now it works perfectly every time. I suspect this has to do with Lambda execution environment, perhaps? Either way, having createGunzip() declared at the top, rather every time I call the function, was the issue.

Upvotes: 1

Related Questions