I am new to AWS Lambda functions, and I am trying to retrieve secrets to use to connect to RDS database.
Here is my current code:
var aws = require("aws-sdk");
exports.handler = async (event) => {
console.log("version = " + aws.VERSION)
var client = new aws.SecretsManager({
version: '2017-10-17',
region: 'eu-west-2' // Your region
});
var secret, decodedBinarySecret;
await client.getSecretValue({
SecretId: 'mysecretid'
}, function(err, data) {
if (err) {
console.log("I am here 2");
try {
/* code */
if (err.code === 'DecryptionFailureException')
// Secrets Manager can't decrypt the protected secret text using the provided KMS key.
// Deal with the exception here, and/or rethrow at your discretion.
throw err;
else if (err.code === 'InternalServiceErrorException')
// An error occurred on the server side.
// Deal with the exception here, and/or rethrow at your discretion.
throw err;
else if (err.code === 'InvalidParameterException')
// You provided an invalid value for a parameter.
// Deal with the exception here, and/or rethrow at your discretion.
throw err;
else if (err.code === 'InvalidRequestException')
// You provided a parameter value that is not valid for the current state of the resource.
// Deal with the exception here, and/or rethrow at your discretion.
throw err;
else if (err.code === 'ResourceNotFoundException')
// We can't find the resource that you asked for.
// Deal with the exception here, and/or rethrow at your discretion.
throw err;
}
catch (e) {
console.log(e);
console.log(JSON.stringify(e));
}
}
else {
console.log("I am here 1");
// Decrypts secret using the associated KMS CMK.
// Depending on whether the secret is a string or binary, one of these fields will be populated.
if ('SecretString' in data) {
secret = data.SecretString;
}
else {
let buff = new Buffer(data.SecretBinary, 'base64');
decodedBinarySecret = buff.toString('ascii');
}
} // Your code goes here.
console.log("I am here 3");
console.log(secret);
});
};
I have attached the correct roles and permissions to the function and also the console log of the aws version is version = 2.1083.0
But the other console logs do not output anything indicating the code is not being hit, wondering what I am doing wrong?