Search code examples
amazon-web-servicesaws-lambdaamazon-rdsaltostra

Where is my database password in Altostra project?


I’ve created a project in Altostra. I've created a Lambda function in the vscode extension and connected it to an RDS instance. The password is apparently auto-generated and “accessible to any lambda”, but where do I access it?

enter image description here


Solution

  • When you connect lambda to the database resource, you will have connection details provided for you into lambda's environment variable. enter image description here

    Here you have DB_SECRET_RDS02, representing the "secret" you need. Then, in the Lambda code, you will have access to the variable through process.env

    const aws = require('aws-sdk')
    
    // retrieving host and port
    const [host, port] = process.env.DB_RDS02.split(':')
    // retrieving secret from SecretManager
    const secretManager = new aws.SecretsManager()
    const secret = await secretManager.getSecretValue({
      SecretId: process.env.DB_SECRET_RDS02
    }).promise()
    // retrieving data from secret
    const { username, password } = JSON.parse(secret.SecretString)