def sync_lambda_function()

in lib/lambda_util.py [0:0]


def sync_lambda_function(function_file, bucket_name):
  # Get the file name without the path
  function_name = os.path.basename(function_file)
  
  # The zip file will have the same name, but with a .zip extension
  if function_file.endswith('.py'): object_key = function_name.replace(".py",".zip")
  
  # Get the last modified date of the file (seconds since the Epoch)
  file_mtime = os.path.getmtime(function_file)

  # Check attributes of object on S3
  try:
    header = s3.head_object(Bucket=bucket_name, Key=object_key)
    object_mtime = header['LastModified'].timestamp()
  
    # If the local file is older than the one on S3, simply return the existing key and version
    if file_mtime <= object_mtime:
      return object_key, header['VersionId']
  except ClientError as e:
    if e.response['Error']['Code'] == "404": None # If the file isn't found then proceed with the upload
    else:
      raise
  
  # Create a zip file from the function
  zipfile.ZipFile(object_key, mode='w').write(function_file, arcname=function_name)
  
  try:
    __upload_file(object_key, bucket_name)
    return object_key, s3.head_object(Bucket=bucket_name, Key=object_key)['VersionId']
  except ClientError as e: raise
  finally:
    os.remove(object_key) # Delete the temporary zip file