Question: Regardless of the format of encryption, what is the best way to store an encryption key on an AWS EC2 server?

I am storing encrypted information in a MySQL database and my cryptography key is stored in code. Data is encoded/decoded using this key when saving/retrieving from the database. Obviously, I do not want anyone to have access to this key. If the machine is compromised, so is the source (dang but that's life) and perhaps the MySql database too. Since the source is available, so is the key and the attacker can decrypt the data.

What is the best way to avoid storing the key on the server? I assume it would be storing it on a different server but that seems to pass the ball around a little and not solve the actual problem.

Another question could be 'should I even be using an AWS EC2 server' and how secure are one of these things if all patches are applied.

Does anyone have any insight on this sort of situation? I am a pretty good programmer but not the best versed in security.

Answer: 
If an attacker is able to perform a man in the middle attack (and he can perform it, if the wifi connection is insecure), then it's not secure:  

If I take your example: When the UPS courier scans the package, the attacker can drop the message with hash but keep the hash for later. The module will deny the UPS courier the access and he will probably simply go away. Now attacker can go to the house and send the message with the hash to unlock the door.

But this is only one obvious vulnerability of your system, that should show you why **should not roll your own crypto**.  

(I recommend you to read this answer: <https://security.stackexchange.com/a/18198/91783> )

If TLS/SSL is not possible for your system, I think you should use a well-tested implementation of an existing authentication protocol. The code is like a password the token that authenticates the user on the server.

Take a look at Wikpedia for a list of protocols:
<https://en.wikipedia.org/wiki/Authentication_protocol>
I do not notice anywhere in your description that you are encrypting your traffic. Are you sending your hash in the clear?
If so, I am thinking, what would prevent an eavesdropper from intercepting the hash and reuse it?

Another question is, how do you attempt to keep your list of allowable passwords which "keep changing"?