question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ec2 models get_zone_by_name KeyError when creating a Batch client

See original GitHub issue

A pytest-based github action that depends on moto has started failing due to a traceback in ec2 models. The version of moto installed was:

Collecting moto[all]
Downloading moto-2.2.18-py2.py3-none-any.whl (1.5 MB)

The full traceback was:

______________________ ERROR at setup of test_blackboard _______________________

aws_credentials = None

    @pytest.fixture(scope="function")
    def batch_client(aws_credentials):
>       with mock_batch():

tests/conftest.py:52: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/moto/__init__.py:17: in f
    module = importlib.import_module(module_name, "moto")
/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/moto/batch/__init__.py:1: in <module>
    from .models import batch_backends
/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/moto/batch/models.py:14: in <module>
    from moto.ec2 import ec2_backends
/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/moto/ec2/__init__.py:1: in <module>
    from .models import ec2_backends
/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/moto/ec2/models.py:8729: in <module>
    region.name: EC2Backend(region.name) for region in RegionsAndZonesBackend.regions
/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/moto/ec2/models.py:8729: in <dictcomp>
    region.name: EC2Backend(region.name) for region in RegionsAndZonesBackend.regions
/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/moto/ec2/models.py:8624: in __init__
    super().__init__()
/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/moto/ec2/models.py:1141: in __init__
    super().__init__()
/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/moto/ec2/models.py:1374: in __init__
    super().__init__()
/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/moto/ec2/models.py:1390: in __init__
    super().__init__()
/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/moto/ec2/models.py:1513: in __init__
    super().__init__()
/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/moto/ec2/models.py:3470: in __init__
    super().__init__()
/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/moto/ec2/models.py:1743: in __init__
    self._load_amis()
/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/moto/ec2/models.py:1752: in _load_amis
    self.amis[ami_id] = Ami(self, **ami)
/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/moto/ec2/models.py:1701: in __init__
    volume = self.ec2_backend.create_volume(size=15, zone_name=region_name)
/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/moto/ec2/models.py:3486: in create_volume
    zone = self.get_zone_by_name(zone_name)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <moto.ec2.models.EC2Backend object at 0x7f5d679f9710>
name = 'us-east-1a'

    def get_zone_by_name(self, name):
>       for zone in self.zones[self.region_name]:
E       KeyError: 'ap-southeast-3'

/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/moto/ec2/models.py:2209: KeyError

The github action in question is here: https://github.com/spacetelescope/calcloud/runs/4512515777?check_suite_focus=true

And the code that’s being executed when it fails seems to be this:

@pytest.fixture(scope="function")
def batch_client(aws_credentials):
    with mock_batch():
        yield boto3.client("batch", region_name="us-east-1")

The last run one week ago with the same source code worked fine, using moto 2.2.17

Collecting moto[all]
  Downloading moto-2.2.17-py2.py3-none-any.whl (1.5 MB)

https://github.com/spacetelescope/calcloud/runs/4434257100?check_suite_focus=true

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:11
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

11reactions
bblommerscommented, Dec 14, 2021

Thanks for raising this everyone - this is now fixed as of Moto >= 2.2.19.

2reactions
foxmulder900commented, Dec 13, 2021

This change did the trick for me: https://github.com/spulec/moto/pull/4688

I just deduced what those values should be based on what they are for ap-southeast-1 & ap-southeast-2, so it would be good for someone to verify them a little more closely.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Better understanding why im getting a Key error - Stack Overflow
Currently I can get a response if I exclude "RoleName". Why am I seeing a Key error when running the above? AWS Collective....
Read more >
What is KeyError in Python? Dictionary and Handling Them
This article will provide you with a detailed and comprehensive knowledge of how to resolve KeyError in Python in Dictionary.
Read more >
How to fix Python KeyError Exceptions in simple steps?
In simple terms, when you see a KeyError, it denotes that the key you were looking for could not be found.
Read more >
Getting key error when training object detection model
Hi! I am getting the error while training my object detection model. Epoch 1/1 Mini-batch: 1/1457 Loss: 0.1182 Mini-batch: 101/1457 Loss: ...
Read more >
Running batch jobs at scale with EC2 Spot Instances - AWS
This will create a compute environment managed by AWS Batch that will be ... Select Spot as provisioning model, leave maximum price blank ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found