Hey everyone! I'm having some trouble retrieving key-value pairs from a JSON file in my AWS Lambda function. I already have the same environment variables that are present in the new JSON file, but despite this, I'm still getting the values from the environment variables instead of the JSON. When I invoke the lambda function, it throws an error, but the script works perfectly when I run it locally. Here's a snippet of my code:
credentials = load_credentials(ENCRYPTION_KEY, file_path="encrypted_credentials.json")
print(credentials)
DB_CONFIG = {
"server": credentials.get("DB_SERVER_KC"),
"user": credentials.get("DB_USER"),
"password": credentials.get("DB_PASSWORD"),
"database": credentials.get("db_name")
}
I'm wondering what could be causing this issue. Any insights would be appreciated!
4 Answers
If you uploaded the credentials file directly to the Lambda environment, that could be your issue. Lambda functions don't have access to paths in the same way that scripts running in a local environment do. Also, bundling credentials in Lambda like this isn't exactly the safest move. You might want to rethink your approach.
Good question! But bundling credentials in your Lambda function isn't the best practice. Writing your own encryption can lead to security holes if you're not careful. If you’re still experimenting, at least consider consulting with a DevOps engineer to check your design.
It's hard to troubleshoot without knowing the exact error you're encountering. But if you're using environment variables with the same keys as your JSON file, that might explain why you're seeing those values instead. You could consider using AWS Secrets Manager for better management of your credentials, especially if you're working with databases like RDS.
This seems to be less of an AWS problem and more of a Python issue. Take a step back and clarify what you're trying to achieve here. If your goal is just to connect Lambda to a database, you might not need to handle credentials this way at all. IAM roles can simplify that process a lot.
Related Questions
How To: Running Codex CLI on Windows with Azure OpenAI
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically