4

Hello I need some help in determining aws region inside a glue job. I am trying to use boto3 client kms and when I do the following I get a Error NoRegionError: You must specify a region. kms = boto3.client('kms') Obviously it is asking me to set region_name when creating the client but I do not wish to hardcode the region

When running a glue job i do see a line in the logs which says Detected region us-east-2 but I am not sure on how I can fetch that value ?

sks27
  • 63
  • 2
  • 7
  • Have you tried [this](https://stackoverflow.com/questions/37514810/how-to-get-the-region-of-the-current-user-from-boto) answer already? Most AWS services set up the environment appropriately, to make sure this works. – Maurice Jun 20 '19 at 20:30
  • Thanks Maurice! Yes I tried the above link but that returns me None. – sks27 Jun 20 '19 at 21:18

2 Answers2

3

If you're running Pyspark / Python shell Glue job, try this:

import requests

r = requests.get("http://169.254.169.254/latest/dynamic/instance-identity/document")
response_json = r.json()
region = response_json.get('region')

print region
ya24
  • 490
  • 1
  • 4
  • 16
  • Wow, at first I thought there's no way this black magic works... but alas it's an AWS thing: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-identity-documents.html – aiguofer Jun 30 '20 at 05:36
  • You can also access it from the environment variable if you are on Glue 2.0+: https://stackoverflow.com/a/72467100/6580142 – David Liao Jun 01 '22 at 19:26
-1

AWS Glue is still not available in all the regions. You may refer this link

That's the reason you need to hardcode the region.

Coming to the "Detected region us-east-2" this might be becus of your aws CLI configuration

BigData-Guru
  • 1,161
  • 1
  • 15
  • 20
  • Sorry I did not follow the aws CLI configuration part. I am running the above code in AWS Glue as a Spark JOB not locally. – sks27 Jun 20 '19 at 21:22
  • While creating the Job, you must have selected the region.. I don't think it's required to set the region specifically in the program – BigData-Guru Jun 21 '19 at 09:20
  • If the job while executing creates an AWS client to access some other resource, the region needs to be specified. – Jari Turkia May 13 '20 at 06:55