Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to expose ec2 instance ipv6 address #64

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions custom_resources/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@

from .LambdaBackedCustomResource import LambdaBackedCustomResource

class InstanceIpv6Address(LambdaBackedCustomResource):
props = {
'InstanceId': (string_types, True), # e.g. i-1234567890abcdef0
}

@classmethod
def _lambda_policy(cls):
return {
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": [
"ec2:DescribeNetworkInterfaces",
],
"Resource": "*",
}],
}

class FindAmi(LambdaBackedCustomResource):
props = {
Expand Down
61 changes: 61 additions & 0 deletions lambda_code/ec2/InstanceIpv6Address/index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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()
1 change: 1 addition & 0 deletions lambda_code/ec2/InstanceIpv6Address/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
git+https://github.com/iRobotCorporation/cfn-custom-resource#egg=cfn-custom-resource