Skip to content

Commit

Permalink
Add support for EC2 tags on instance creation (#352)
Browse files Browse the repository at this point in the history
* Add support for EC2 instance tags

* Pass params via boto on instance creation

* Fix default values

* Use same default tags

* Fix typo

* Fix black formatting errors

* More format fixes

* black

* Use cloudprovider.yaml to set defaults

* Add cloudprovider.yaml

* Delete DEFAULT_TAGS
  • Loading branch information
pwerth committed May 27, 2022
1 parent b604581 commit e31b17c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
32 changes: 32 additions & 0 deletions dask_cloudprovider/aws/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
get_default_vpc,
get_vpc_subnets,
get_security_group,
dict_to_aws,
)
from dask_cloudprovider.utils.timeout import Timeout

Expand Down Expand Up @@ -50,6 +51,8 @@ def __init__(
filesystem_size=None,
key_name=None,
iam_instance_profile=None,
instance_tags: None,
volume_tags: None,
**kwargs,
):
super().__init__(*args, **kwargs)
Expand All @@ -71,6 +74,8 @@ def __init__(
self.filesystem_size = filesystem_size
self.key_name = key_name
self.iam_instance_profile = iam_instance_profile
self.instance_tags = instance_tags
self.volume_tags = volume_tags

async def create_vm(self):
"""
Expand Down Expand Up @@ -125,6 +130,16 @@ async def create_vm(self):
"SubnetId": self.subnet_id,
}
],
"TagSpecifications": [
{
"ResourceType": "instance",
"Tags": dict_to_aws(self.instance_tags, upper=True),
},
{
"ResourceType": "volume",
"Tags": dict_to_aws(self.volume_tags, upper=True),
},
],
}

if self.key_name:
Expand Down Expand Up @@ -302,6 +317,12 @@ class EC2Cluster(VMCluster):
be created automatically. Default is ``True``.
debug: bool, optional
More information will be printed when constructing clusters to enable debugging.
instance_tags: dict, optional
Tags to be applied to all EC2 instances upon creation. By default, includes
"createdBy": "dask-cloudprovider"
volume_tags: dict, optional
Tags to be applied to all EBS volumes upon creation. By default, includes
"createdBy": "dask-cloudprovider"
Notes
-----
Expand Down Expand Up @@ -407,6 +428,8 @@ def __init__(
iam_instance_profile=None,
docker_image=None,
debug=False,
instance_tags=None,
volume_tags=None,
**kwargs,
):
self.boto_session = get_session()
Expand Down Expand Up @@ -458,6 +481,13 @@ def __init__(
else self.config.get("iam_instance_profile")
)
self.debug = debug

instance_tags = instance_tags if instance_tags is not None else {}
self.instance_tags = {**instance_tags, **self.config.get("instance_tags")}

volume_tags = volume_tags if volume_tags is not None else {}
self.volume_tags = {**volume_tags, **self.config.get("volume_tags")}

self.options = {
"cluster": self,
"config": self.config,
Expand All @@ -474,6 +504,8 @@ def __init__(
"filesystem_size": self.filesystem_size,
"key_name": self.key_name,
"iam_instance_profile": self.iam_instance_profile,
"instance_tags": self.instance_tags,
"volume_tags": self.volume_tags,
}
self.scheduler_options = {**self.options}
self.worker_options = {**self.options}
Expand Down
4 changes: 4 additions & 0 deletions dask_cloudprovider/cloudprovider.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ cloudprovider:
iam_instance_profile: {} # Iam role to assign to instances
# Arn: 'string'
# Name: 'string'
instance_tags:
createdBy: dask-cloudprovider
volume_tags:
createdBy: dask-cloudprovider

azure:
location: null # The Azure location to launch your cluster
Expand Down

0 comments on commit e31b17c

Please sign in to comment.