Skip to content
This repository was archived by the owner on Jan 2, 2019. It is now read-only.

Commit ab8aab3

Browse files
author
Mohammed AbuAisha
committed
CY-893: Detach eni from lambda function
1 parent 8316cdc commit ab8aab3

File tree

7 files changed

+75
-5
lines changed

7 files changed

+75
-5
lines changed

CHANGELOG.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
2.8.1:
2+
- Fix issue with removing elastic network interface created by lambda invoke function
3+
2.8.0:
4+
- Upgrade boto3 library version to 1.9.57
5+
- Upgrade botocore library version to 1.12.57
6+
- Update all plugin examples to use the latest version of the plugin
7+
- Fix bugs for plugin
18
2.7.1:
29
- Remove tagging for EIP as it is not supported.
310
- Add use_ipv6_ip property to compute.

cloudify_awssdk/ec2/resources/eni.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ def status(self):
6767
return None
6868
return props['Status']
6969

70+
def list_network_interfaces(self, filters=None):
71+
params = dict()
72+
if filters:
73+
params['Filters'] = filters
74+
75+
resources = self.client.describe_network_interfaces(**params)
76+
return resources.get(NETWORKINTERFACES) if resources else []
77+
7078
def create(self, params):
7179
"""
7280
Create a new AWS EC2 NetworkInterface.

cloudify_awssdk/iam/resources/role.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def delete(ctx, iface, resource_config, **_):
150150
for policy in ctx.instance.runtime_properties['policies']:
151151
payload = dict()
152152
payload['PolicyArn'] = policy
153-
iface.attach_policy(payload)
153+
iface.detach_policy(payload)
154154

155155
iface.delete(resource_config)
156156

cloudify_awssdk/lambda_serverless/resources/function.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ def create(ctx, iface, resource_config, **_):
158158
utils.update_resource_arn(
159159
ctx.instance, create_response['FunctionArn'])
160160

161+
# Save vpc_config to be used later on when remove eni created by invoke
162+
# function
163+
if vpc_config and create_response.get('VpcConfig'):
164+
ctx.instance.runtime_properties['vpc_config'] =\
165+
create_response['VpcConfig']
166+
161167

162168
@decorators.aws_resource(LambdaFunction, RESOURCE_TYPE,
163169
ignore_properties=True)

cloudify_awssdk/lambda_serverless/resources/invoke.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
# Cloudify
2121
from cloudify_awssdk.common import decorators, utils
2222
from cloudify_awssdk.lambda_serverless.resources.function import LambdaFunction
23+
from cloudify_awssdk.ec2.resources import eni
2324

2425
RESOURCE_TYPE = 'Lambda Function Invocation'
2526

@@ -49,4 +50,52 @@ def attach_to(ctx, resource_config, **_):
4950
@decorators.aws_relationship(resource_type=RESOURCE_TYPE)
5051
def detach_from(ctx, resource_config, **_):
5152
'''Detaches an Lambda Invoke from something else'''
52-
pass
53+
props = ctx.target.instance.runtime_properties
54+
function_name = props.get('aws_resource_id')
55+
vpc_config = props.get('vpc_config')
56+
57+
# Check to see if the invoked function is placed in vpc or not so that we
58+
# can remove the eni created by invoke method
59+
if vpc_config:
60+
eni_instance = eni.EC2NetworkInterface(
61+
ctx_node=ctx.target.node,
62+
logger=ctx.logger
63+
)
64+
65+
eni_filter = [
66+
{
67+
'Name': 'vpc-id',
68+
'Values': [vpc_config['VpcId']]
69+
},
70+
{
71+
'Name': 'group-id',
72+
'Values': [
73+
group_id for group_id in
74+
vpc_config['SecurityGroupIds']
75+
]
76+
},
77+
{
78+
'Name': 'description',
79+
'Values': ['AWS Lambda VPC ENI:*']
80+
},
81+
{
82+
'Name': 'requester-id',
83+
'Values': ['*:{0}*'.format(function_name)]
84+
}
85+
]
86+
87+
eni_interfaces = eni_instance.list_network_interfaces(eni_filter)
88+
if eni_interfaces:
89+
eni_interface = eni_interfaces[0]
90+
eni_id = eni_interface['NetworkInterfaceId']
91+
attachment = eni_interface.get('Attachment')
92+
if attachment:
93+
eni_attachment_id = attachment.get('AttachmentId')
94+
params = {
95+
eni.ATTACHMENT_ID: eni_attachment_id
96+
}
97+
eni_instance.detach(params)
98+
99+
params = dict()
100+
params[eni.NETWORKINTERFACE_ID] = eni_id
101+
eni_instance.delete(params)

plugin.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ plugins:
22

33
awssdk:
44
executor: central_deployment_agent
5-
source: https://github.com/cloudify-incubator/cloudify-awssdk-plugin/archive/2.8.0.zip
5+
source: https://github.com/cloudify-incubator/cloudify-awssdk-plugin/archive/2.8.1.zip
66
package_name: cloudify-awssdk-plugin
7-
package_version: '2.8.0'
7+
package_version: '2.8.1'
88

99
data_types:
1010

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
setup(
2222
name='cloudify-awssdk-plugin',
23-
version='2.8.0',
23+
version='2.8.1',
2424
license='LICENSE',
2525
packages=find_packages(exclude=['tests*']),
2626
description='A Cloudify plugin for AWS',

0 commit comments

Comments
 (0)