I accidentally change the permission to
chmod -R 777 /var and now I cannot access the EC2 instance how do I fix this?
Loosing access to an Amazon EC2 instance is a common encounter - how to approach this situation when you don't know the reason yet is addressed in Eric Hammond's excellent summary Solving: "I can't connect to my server on Amazon EC2". However, since you already now what caused this, you can apply his respective instructions Fixing Files on the Root EBS Volume of an EC2 Instance right away, which boils down to:
[Note: this only applies to instances with Amazon EBS based Root Device Storage, because only these can be stopped without loosing the root volume; put another way, take care not to terminate your instance instead ;)]
Please see Eric's instructions for details on how to do this from the command line; obviously you can achieve all steps up to the SSH access via the AWS Management Console as well, removing the need to install the Amazon EC2 API Tools, in case they aren't readily available already.