I am using AWS Java API RunInstance() to start a new EC2 instance from my custom AMI image. How do I pass environment variables to the new EC2 INSTANCE such as database url,
You can also use instance metadata retrieval as explained at https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html
From the above document, the following GET request would retrieve user data for an instance if you run it from within the instance:
GET http://169.254.169.254/latest/user-data
This way, user data can be retrieved dynamically even after the instance is already started and running.