Skip to content

Commit

Permalink
Fix logging for Lambda functions
Browse files Browse the repository at this point in the history
  • Loading branch information
vchintal committed Oct 20, 2023
1 parent eb2395e commit 038396a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
9 changes: 5 additions & 4 deletions patterns/privatelink-access/lambdas/create_eni.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def __str__(self):
return '%s >>> %s' % (self.message, json.dumps(self.kwargs))

_ = StructuredMessage # optional, to improve readability
logging.basicConfig(level=logging.DEBUG, format='%(message)s')

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

def handler(event, context):
# Only modify on CreateNetworkInterface events
Expand All @@ -27,14 +27,15 @@ def handler(event, context):

# Add the extracted private IP address of the ENI as an IP target in the target group
try:
logger.info('IP address %s is identified as belonging to one of the cluster endpoint ENIs', ip)
response = ELBV2_CLIENT.register_targets(
TargetGroupArn = TARGET_GROUP_ARN,
Targets=[{
'Id': ip,
'Port': 443
}]
)
logging.info(_(response))
logger.info(_(response))
except Exception as e:
logging.error(_(e))
logger.error(_(e))
raise(e)
16 changes: 8 additions & 8 deletions patterns/privatelink-access/lambdas/delete_eni.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def __str__(self):
return '%s >>> %s' % (self.message, json.dumps(self.kwargs))

_ = StructuredMessage # optional, to improve readability
logging.basicConfig(level=logging.DEBUG, format='%(message)s')

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

def handler(event, context):

Expand All @@ -32,7 +32,7 @@ def handler(event, context):
)['TargetHealthDescriptions']

if not targetHealthDescriptions:
logging.info("Did not find any TargetHealthDescriptions, quitting!")
logger.info("Did not find any TargetHealthDescriptions, quitting!")
return

# Iterate over the list of TargetHealthDescriptions and extract the list of
Expand All @@ -54,7 +54,7 @@ def handler(event, context):
)['NetworkInterfaces']

if not networkInterfaces:
logging.info("Did not find any EKS API ENIs to compare with, quitting!")
logger.info("Did not find any EKS API ENIs to compare with, quitting!")
return

for networkInterface in networkInterfaces:
Expand All @@ -71,17 +71,17 @@ def handler(event, context):
unhealthyTargetsToDeregister.append(unhealthyTarget)

if not unhealthyTargetsToDeregister:
logging.info("There are no unhealthy targets to deregister, quitting!")
logger.info("There are no unhealthy targets to deregister, quitting!")
return

logging.info("Targets are to be deregistered: %s", unhealthyTargetsToDeregister)
logger.info("Targets to be deregistered are: %s", unhealthyTargetsToDeregister)

try:
response = ELBV2_CLIENT.deregister_targets(
TargetGroupArn = TARGET_GROUP_ARN,
Targets=unhealthyTargetsToDeregister
)
logging.info(_(response))
logger.info(_(response))
except Exception as e:
logging.error(_(e))
logger.error(_(e))
raise(e)

0 comments on commit 038396a

Please sign in to comment.