class_docstr: AWS Driver
class_docstr_tok: ["AWS","Driver"]
code: def spin_up_worker(
      self, resource_conf, worker_prefix, queue_name, task_id
  ):
      # user_data script will automatically run when the instance is started. it will install the required packages
      # for clearml-agent, configure it using environment variables and run clearml-agent on the required queue
      # Config reference: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2/client/run_instances.html
      user_data = self.gen_user_data(
          worker_prefix,
          queue_name,
          task_id,
          resource_conf.get("cpu_only", False),
      )
  
      ec2 = boto3.client("ec2", **self.creds())
      launch_specification = ConfigFactory.from_dict(
          {
              "ImageId": resource_conf["ami_id"],
              "Monitoring": {
                  "Enabled": bool(
                      resource_conf.get("enable_monitoring", False)
                  )
              },
              "InstanceType": resource_conf["instance_type"],
          }
      )
      # handle EBS volumes (existing or new)
      # Ref: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/block-device-mapping-concepts.html
      if resource_conf.get("ebs_snapshot_id") and resource_conf.get(
          "ebs_device_name"
      ):
          launch_specification["BlockDeviceMappings"] = [
              {
                  "DeviceName": resource_conf["ebs_device_name"],
                  "Ebs": {
                      "SnapshotId": resource_conf["ebs_snapshot_id"]
                  },
              }
          ]
      elif resource_conf.get("ebs_device_name"):
          launch_specification["BlockDeviceMappings"] = [
              {
                  "DeviceName": resource_conf["ebs_device_name"],
                  "Ebs": {
                      "VolumeSize": resource_conf.get(
                          "ebs_volume_size", 80
                      ),
                      "VolumeType": resource_conf.get(
                          "ebs_volume_type", "gp3"
                      ),
                  },
              }
          ]
  
      if resource_conf.get("subnet_id", None):
          launch_specification["SubnetId"] = resource_conf["subnet_id"]
      elif resource_conf.get("availability_zone", None):
          launch_specification["Placement"] = {
              "AvailabilityZone": resource_conf["availability_zone"]
          }
      else:
          raise Exception(
              "subnet_id or availability_zone must to be specified in the config"
          )
      if resource_conf.get("key_name", None):
          launch_specification["KeyName"] = resource_conf["key_name"]
      if resource_conf.get("security_group_ids", None):
          launch_specification["SecurityGroupIds"] = resource_conf[
              "security_group_ids"
          ]
      # Adding iam role - you can have Arn OR Name, not both, Arn getting priority
      if self.iam_arn:
          launch_specification["IamInstanceProfile"] = {
              "Arn": self.iam_arn,
          }
      elif self.iam_name:
          launch_specification["IamInstanceProfile"] = {
              "Name": self.iam_name
          }
  
      if resource_conf["is_spot"]:
          # Create a request for a spot instance in AWS
          encoded_user_data = base64.b64encode(
              user_data.encode("ascii")
          ).decode("ascii")
          launch_specification["UserData"] = encoded_user_data
          ConfigTree.merge_configs(
              launch_specification,
              resource_conf.get("extra_configurations", {}),
          )
  
          instances = ec2.request_spot_instances(
              LaunchSpecification=launch_specification
          )
  
          # Wait until spot request is fulfilled
          request_id = instances["SpotInstanceRequests"][0][
              "SpotInstanceRequestId"
          ]
          waiter = ec2.get_waiter("spot_instance_request_fulfilled")
          waiter.wait(SpotInstanceRequestIds=[request_id])
          # Get the instance object for later use
          response = ec2.describe_spot_instance_requests(
              SpotInstanceRequestIds=[request_id]
          )
          instance_id = response["SpotInstanceRequests"][0][
              "InstanceId"
          ]
  
      else:
          # Create a new EC2 instance
          launch_specification.update(
              MinCount=1,
              MaxCount=1,
              UserData=user_data,
              InstanceInitiatedShutdownBehavior="terminate",
          )
          ConfigTree.merge_configs(
              launch_specification,
              resource_conf.get("extra_configurations", {}),
          )
  
          instances = ec2.run_instances(**launch_specification)
  
          # Get the instance object for later use
          instance_id = instances["Instances"][0]["InstanceId"]
  
      instance = boto3.resource("ec2", **self.creds()).Instance(
          instance_id
      )
      if resource_conf.get("tags"):
          instance.create_tags(
              Resources=[instance_id],
              Tags=[
                  {"Key": key, "Value": val}
                  for key, val in parse_tags(resource_conf.get("tags"))
              ],
          )
      # Wait until instance is in running state
      instance.wait_until_running()
      return instance_id
  
file_path: clearml/clearml/automation/aws_driver.py
imports: import base64
  import attr
  from attr.validators import instance_of
  from ..None import Task
  from ..utilities.pyhocon import ConfigFactory, ConfigTree
  from .auto_scaler import CloudDriver
  from .cloud_driver import parse_tags
label_desc: Use image node_id 3 for calling the AWSDriver obj's underlying member method code with example usage: obj.spin_up_worker(resource_conf, worker_prefix, queue_name, task_id) and returns: instance_id