Starts instances and waits for them to be in a running state. :return: The response to the start request.
(self)
| 189 | |
| 190 | # snippet-start:[python.example_code.ec2.StartInstances] |
| 191 | def start(self) -> Optional[Dict[str, Any]]: |
| 192 | """ |
| 193 | Starts instances and waits for them to be in a running state. |
| 194 | |
| 195 | :return: The response to the start request. |
| 196 | """ |
| 197 | if not self.instances: |
| 198 | logger.info("No instances to start.") |
| 199 | return None |
| 200 | |
| 201 | instance_ids = [instance["InstanceId"] for instance in self.instances] |
| 202 | try: |
| 203 | start_response = self.ec2_client.start_instances(InstanceIds=instance_ids) |
| 204 | waiter = self.ec2_client.get_waiter("instance_running") |
| 205 | waiter.wait(InstanceIds=instance_ids) |
| 206 | return start_response |
| 207 | except ClientError as err: |
| 208 | logger.error( |
| 209 | f"Failed to start instance(s): {','.join(map(str, instance_ids))}" |
| 210 | ) |
| 211 | error_code = err.response["Error"]["Code"] |
| 212 | if error_code == "IncorrectInstanceState": |
| 213 | logger.error( |
| 214 | "Couldn't start instance(s) because they are in an incorrect state. " |
| 215 | "Ensure the instances are in a stopped state before starting them." |
| 216 | ) |
| 217 | raise |
| 218 | |
| 219 | # snippet-end:[python.example_code.ec2.StartInstances] |
| 220 |
no test coverage detected