-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.py
More file actions
61 lines (47 loc) · 1.75 KB
/
index.py
File metadata and controls
61 lines (47 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""
Custom Resource for extracting the ipv6 address of an EC2 instance.
This feature is currently not supported natively by cloudformation.
An open feature request can be found here:
https://github.com/aws-cloudformation/cloudformation-coverage-roadmap/issues/916
This custom resource will check the network interface of the given instance and output
the associated ipv6 address.
Parameters:
* InstanceId: a single InstanceId
Return:
Attributes:
- Ipv6Address: The ipv6 address of the given instance
"""
import os
from cfn_custom_resource import CloudFormationCustomResource
try:
from _metadata import CUSTOM_RESOURCE_NAME
except ImportError:
CUSTOM_RESOURCE_NAME = 'dummy'
REGION = os.environ['AWS_REGION']
class InstanceIpv6Address(CloudFormationCustomResource):
"""
ec2.InstanceIpv6Address
Properties:
InstanceId: str: The instance id to retrieve the ipv6 address from
"""
RESOURCE_TYPE_SPEC = CUSTOM_RESOURCE_NAME
def validate(self):
self.instance_id = self.resource_properties['InstanceId']
return self.instance_id.startswith("i-")
def create(self):
ec2_client = self.get_boto3_client('ec2')
print(f"Retrieving network interface info for '{self.instance_id}'")
resp = ec2_client.describe_network_interfaces(
Filters=[{'Name':'attachment.instance-id', 'Values':[self.instance_id] }]
)
address = resp.get('NetworkInterfaces')[0]['Ipv6Address']
print(f"Found address '{address}' associated with instance '{self.instance_id}'")
return {
'Ipv6Address': address
}
def update(self):
return self.create()
def delete(self):
# Nothing to delete
pass
handler = InstanceIpv6Address.get_handler()