I deployed an app in Heroku and AWS EC2. I also created a AWS S3 for my storage for uploading images and videos. Suddenly Heroku has emailed me that my application was affected by a recent vulnerability and my Access key for AWS S3 was stolen (Probably because of a bot that mine access key, also my repository is private so I don't know how this happened), I'm glad I saw it early and I was able to delete my keys and generate a new one. Basically my code for uploading images is this.
Access key from a .json file in my code.
{
"accessKeyId": "xxxxxxxxxxxxx",
"secretAccessKey": "xxxxxxxxxxxxxxxxxxxx",
"region": "us-east-2"
}
My Middleware
const multer = require('multer');
const AWS = require('aws-sdk');
const multerS3 = require('multer-s3');
AWS.config.loadFromPath('./resources/AwsS3Key.json');
var s3 = new AWS.S3();
const s3Storage = multerS3({
s3 : s3,
bucket : 'entrenami-app-bucket',
acl : 'public-read',
key : function (req, file, callback) {
callback(null, file.originalname);
}
});
module.exports.s3Upload = multer({ storage: s3Storage });
I then attached my middleware to my express router for uploading image or videos.
It was written in AWS documentation that I shouldn't put my access key with my source code. So I added it to gitignore. Now how would I access my access key when I deployed my app in AWS EC2 or Heroku? I have no Idea how to access my key using ENV variable, Please help me, I've been reading the documentation but I can't wrap my mind around it. Thank you in advance.