Get an instance by instance id. The special name 'mine' resolves to a unique running owned instance, if there is one; otherwise the name is assumed to be an instance id.
(name: str)
| 646 | |
| 647 | |
| 648 | def get_instance(name: str) -> Instance: |
| 649 | """ |
| 650 | Get an instance by instance id. The special name 'mine' resolves to a |
| 651 | unique running owned instance, if there is one; otherwise the name is |
| 652 | assumed to be an instance id. |
| 653 | """ |
| 654 | if name == "mine": |
| 655 | filters: list[FilterTypeDef] = [ |
| 656 | {"Name": "tag:LaunchedBy", "Values": [whoami()]}, |
| 657 | {"Name": "instance-state-name", "Values": ["pending", "running"]}, |
| 658 | ] |
| 659 | instances = [i for i in boto3.resource("ec2").instances.filter(Filters=filters)] |
| 660 | if not instances: |
| 661 | raise RuntimeError("can't understand 'mine': no owned instance?") |
| 662 | if len(instances) > 1: |
| 663 | raise RuntimeError( |
| 664 | f"can't understand 'mine': too many owned instances ({', '.join(i.id for i in instances)})" |
| 665 | ) |
| 666 | instance = instances[0] |
| 667 | say(f"understanding 'mine' as unique owned instance {instance.id}") |
| 668 | return instance |
| 669 | return boto3.resource("ec2").Instance(name) |
| 670 | |
| 671 | |
| 672 | def list_instances( |