Skip to content

Commit

Permalink
Ability to use EC2 private IP address (#353)
Browse files Browse the repository at this point in the history
* Ability to use EC2 private IP address

* Black linting
  • Loading branch information
pwerth committed May 30, 2022
1 parent e31b17c commit 704f667
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions dask_cloudprovider/aws/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def __init__(
iam_instance_profile=None,
instance_tags: None,
volume_tags: None,
use_private_ip: False,
**kwargs,
):
super().__init__(*args, **kwargs)
Expand All @@ -76,6 +77,7 @@ def __init__(
self.iam_instance_profile = iam_instance_profile
self.instance_tags = instance_tags
self.volume_tags = volume_tags
self.use_private_ip = use_private_ip

async def create_vm(self):
"""
Expand Down Expand Up @@ -122,9 +124,11 @@ async def create_vm(self):
"InstanceInitiatedShutdownBehavior": "terminate",
"NetworkInterfaces": [
{
"AssociatePublicIpAddress": True,
"AssociatePublicIpAddress": False
if self.use_private_ip
else True,
"DeleteOnTermination": True,
"Description": "public",
"Description": "private" if self.use_private_ip else "public",
"DeviceIndex": 0,
"Groups": self.security_groups,
"SubnetId": self.subnet_id,
Expand Down Expand Up @@ -166,13 +170,16 @@ async def create_vm(self):
f"Created instance {self.instance['InstanceId']} as {self.name}"
)

address_type = "Private" if self.use_private_ip else "Public"
ip_address_key = f"{address_type}IpAddress"

timeout = Timeout(
300,
f"Failed Public IP for instance {self.instance['InstanceId']}",
f"Failed {address_type} IP for instance {self.instance['InstanceId']}",
)
while (
"PublicIpAddress" not in self.instance
or self.instance["PublicIpAddress"] is None
ip_address_key not in self.instance
or self.instance[ip_address_key] is None
) and timeout.run():
backoff = 0.1
await asyncio.sleep(
Expand All @@ -187,7 +194,7 @@ async def create_vm(self):
except botocore.exceptions.ClientError as e:
timeout.set_exception(e)
backoff = backoff * 2
return self.instance["PublicIpAddress"]
return self.instance[ip_address_key]

async def destroy_vm(self):
async with self.cluster.boto_session.create_client(
Expand Down Expand Up @@ -323,6 +330,10 @@ class EC2Cluster(VMCluster):
volume_tags: dict, optional
Tags to be applied to all EBS volumes upon creation. By default, includes
"createdBy": "dask-cloudprovider"
use_private_ip: bool (optional)
Whether to use a private IP (if True) or public IP (if False).
Default ``False``.
Notes
-----
Expand Down Expand Up @@ -430,6 +441,7 @@ def __init__(
debug=False,
instance_tags=None,
volume_tags=None,
use_private_ip=False,
**kwargs,
):
self.boto_session = get_session()
Expand Down Expand Up @@ -488,6 +500,8 @@ def __init__(
volume_tags = volume_tags if volume_tags is not None else {}
self.volume_tags = {**volume_tags, **self.config.get("volume_tags")}

self._use_private_ip = use_private_ip

self.options = {
"cluster": self,
"config": self.config,
Expand All @@ -506,6 +520,7 @@ def __init__(
"iam_instance_profile": self.iam_instance_profile,
"instance_tags": self.instance_tags,
"volume_tags": self.volume_tags,
"use_private_ip": self._use_private_ip,
}
self.scheduler_options = {**self.options}
self.worker_options = {**self.options}
Expand Down

0 comments on commit 704f667

Please sign in to comment.