1
async function getP(){
        var params = {
            Name: 'MY-NAME',
            WithDecryption: true
        };

        var request = await ssm.getParameter(params).promise();

        return request.Parameter.Value;          
    }

    async function getParam(){
        var resp = await getP()

        console.log(resp)
    }

    getParam()

This is the code inside my lambda function which is currently not working and I'm not sure why..

when I change it to:

    const x = getParam()
    console.log(x) // it says that this is pending

but I thought the async awaits would have resolved that, any ideas?

edited:

console.log('first') // never logged
const res = await ssm.getParameter(paramUsername).promise(); // paramUsername deffo exists in SSM
console.log(res, 'res') // never logged
console.log('second') // never logged
Red Baron
  • 7,181
  • 10
  • 39
  • 86
  • You need to `await getParam()`. The method is `async`, so if you just call it without awaiting you'll get back a promise. Either `await` it or resolve it using the usual promise method. – 404 Nov 13 '19 at 13:12
  • this didnt work either. I think `ssm.getParamater` is broken :/ – Red Baron Nov 13 '19 at 14:44
  • It's not because I use it all the time. You just need to await it properly. – 404 Nov 13 '19 at 14:45
  • As an example, if the lambda handler is `async handler() { const res = await ssm.getParameter(...).promise(); console.log(res); }`, that should get you your result, obviously with `...` replaced with the ssm params etc. – 404 Nov 13 '19 at 14:47
  • @404 well clearly something is broken badly here. my lambda is async and I have put this in: PLEASE CHECK EDITED QUESTION lambda is timing out, even when set to 90seconds – Red Baron Nov 14 '19 at 10:30
  • @404 I think this might be to do with NAT gateway and SSM – Red Baron Nov 14 '19 at 10:40

4 Answers4

1

Rough answer, you have two options which I need the output from either...

1)

function to(promise) {
    return promise.then((data) => {
        return [null, data]
    }).catch(err => [err])
}

// YOUR CODE AMENDED

console.log('first') // never logged
let [err, res] = await to(ssm.getParameter(paramUsername).promise()); 

if(err){
    console.log(err)
    return 
}

console.log(res, 'res') // never logged
console.log('second') // never logged

OR

2) Enclose that call in a try catch like so:

try {

console.log('first') // never logged
const res = await ssm.getParameter(paramUsername).promise(); // paramUsername deffo exists in SSM
console.log(res, 'res') // never logged
console.log('second') // never logged
} catch(e){
  console.log(e)
}

Let me know what the error is, I'm betting your lambda doesn't have permission to access SSM! Will update!

Mrk Fldig
  • 4,244
  • 5
  • 33
  • 64
0

Had the same issue - the only combo I found that worked was to do nothing after the awaits other than return the promise once resolved.

So if you changed your code to:

async function getP(){
    var params = {
        Name: 'MY-NAME',
        WithDecryption: true
    };

    // Do not do anything after the await, only the return
    var request = await ssm.getParameter(params).promise();
    return request.Parameter;          
}

async function getParam(){
    // Do not do anything after the await, only the return
    var resp = await getP()
    return resp.Value;
}

const val = getParam();
console.log(val);

It should work inside lambda. This seems very quirky - I found that running my version of the original code from command line, or in a debugger it worked fine. It was only inside a lambda container (on AWS or in docker) that it didn't resolve and simply dropped out - no error thrown to be caught.

There are a few threads related to this topic (see below) so I hope this helps.

Jens
  • 20,533
  • 11
  • 60
  • 86
JohnSk
  • 93
  • 1
  • 6
0

Use as below:

import AWS from "aws-sdk";

const ssm = new AWS.SSM()

const params = (name) => {
    return {
        Name: name,
        WithDecryption: true,
    };
};

export const getParameter = async (key) => (await ssm.getParameter(params(key)).promise()).Parameter.Value;
Jens
  • 20,533
  • 11
  • 60
  • 86
muzafako
  • 188
  • 2
  • 11
0

I had the same problem, and the solution is set a egress outbound in the lambda security group.

cigien
  • 57,834
  • 11
  • 73
  • 112