Ultimately, I’d like to have an extra feature in my app if the app is running on AWS EC2.
How do I check and set a variable to indicate if it is on AWS or not? I found this thread to do the check, but upon startup how do I set a variable across the app like a boolean? Something like:
let checkAWS;
metadata.isEC2().then(function (onEC2) {
checkAWS = true;
console.log("EC2: " + onEC2);
});
let app = express();
app.locals.isAWS = checkAWS;
console.log(checkAWS);
Every time, I always get the same output:
undefined
EC2: true
I am using the isAWS
variable in my .ejs
file to decide on that functionality.
2
Answers
For future reference in case anyone else is looking, this is what worked for me in order to be able to tell if you are running on AWS.
metadata.isEC2()
is asynchronous so its value is available some time later when the.then()
handler runs. Meanwhile, you’re trying to use the value ofcheckAWS
BEFORE that.then()
handler has run.Since you want access to that value before you start your Express sever, you could only start it inside the
.then()
handler like this:If you’re trying to export
app
for other modules to use, you will have to take a slightly different approach. We’d have to see your broader code context to know what to recommend for that. But, basically the idea here is that you shouldn’t start your server until you have the asynchronously retrieved isAWS result.Also, you should know that with that
metadata.isEC2()
call you’re using, that looks for a known endpoint in an EC2 instance to detect EC2. If that endpoint does not exist, then this will take 500ms to timeout and hit the.catch()
branch above. If it is an EC2 instance, it will be quick.Note, it might be simpler to just check for the presence of some environment variables that are automatically set by the AWS environment such as
AWS_REGION
orAWS_EXECUTION_ENV
. Those can be checked for synchronously.