def lambda_handler()

in cdk/nodegroup/lambda/attach-multus-eni.py [0:0]


def lambda_handler(event, context):
    instance_id = event['detail']['EC2InstanceId']
    LifecycleHookName=event['detail']['LifecycleHookName']
    AutoScalingGroupName=event['detail']['AutoScalingGroupName']
    secgroup_ids = []
    subnet_ids = []

    count = 0
    while True:
      try:
        subnet_ids.append(os.environ['SubnetId' + str(count + 1)])
        secgroup_ids.append(os.environ['SecGroupId' + str(count + 1)])
        count = count + 1
      except KeyError:
        break

    if event["detail-type"] == "EC2 Instance-launch Lifecycle Action":
        index = 1
        for x in subnet_ids:
            interface_id = create_interface(x,secgroup_ids[index - 1])
            attachment = attach_interface(interface_id,instance_id,index)
            index = index+1
            if not interface_id:
                complete_lifecycle_action_failure(LifecycleHookName,AutoScalingGroupName,instance_id)
                return
            elif not attachment:
                complete_lifecycle_action_failure(LifecycleHookName,AutoScalingGroupName,instance_id)
                delete_interface(interface_id)
                return
        #complete_lifecycle_action_success(LifecycleHookName,AutoScalingGroupName,instance_id)

    if event["detail-type"] == "EC2 Instance-terminate Lifecycle Action":
        interface_ids = []
        attachment_ids = []