简体   繁体   中英

Read from Secrets Manager in AWS Lambda function

I am trying to use the Node.js sample code that AWS Secrets Manager provides to read a secret value, and am putting this code inside a Lambda function. However, I can't seem to get into the function that handles the response from getting the secret value.

The Lambda role has AdministratorAccess permissions to rule out it being a permissions issue.

Lambda Code:

exports.handler = async (event) => {

// Load the AWS SDK
var AWS = require('aws-sdk'),
    region = "us-east-1",
    secretName = "/my-secrets/level1/level2",
    secret,
    decodedBinarySecret;

var client = new AWS.SecretsManager({
    region: region
});

console.log('above')

client.getSecretValue({SecretId: secretName}, function(err, data) {

    console.log('in')

    if (err) {
       throw err;
    }
    else {
        if ('SecretString' in data) {
            secret = data.SecretString;
        } else {
            let buff = new Buffer(data.SecretBinary, 'base64');
            decodedBinarySecret = buff.toString('ascii');
        }
    }

    console.log(secret)
});

console.log('below')

};

OUTPUT

2020-03-05T18:51:54.547Z    a3101875-a1f4-4b6f-ac62-3c2f93f5941f    INFO    above
2020-03-05T18:51:54.947Z    a3101875-a1f4-4b6f-ac62-3c2f93f5941f    INFO    below

Because the secret exists, I would expect to see "in" and the secret lines in the output...what is preventing it from getting inside that function?

Change your call to be a promise:

const data = await client.getSecretValue({SecretId: secretName}).promise();

The problem you are running into is that the lambda is ending execution before your callback is executed. AWS Lambda Function Handler in Node.js

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM