AWS Java SDK - automatically detect the region

When the app is deployed in multiple regions in AWS, its useful to detect the region automatically without specifying the region by using a property/environment variable ourself.


We can detect the region by using the AWS SDK:

    Regions.getCurrentRegion(); //returns Regions enum

Or by using:

    EC2MetadataUtils.getEC2InstanceRegion(); //returns region String

Or:

    System.getenv("AWS_REGION")