Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jftuga/79cfdd8fbc9c7ffd713f121739126274 to your computer and use it in GitHub Desktop.
Save jftuga/79cfdd8fbc9c7ffd713f121739126274 to your computer and use it in GitHub Desktop.
creating aws lambda layers for python with third party libraries

creating aws lambda layers for python and third party libraries

python versions

  • Python 3.9: use Fedora 34
  • Python 3.10: use Fedora 35

example

# 'python' will be the top-level folder and underneath it, modules will be installed
pip install -t python paramiko
zip -9r paramiko-py39.zip python

lambda usage

import sys
# sys.path.append("/opt/third_party")

try:
    import paramiko
except ModuleNotFoundError as err:
    print(f"{err}")
    sys.exit()

client = paramiko.SSHClient()

cloudformation

  • /opt/python is in the sys.path
  • the paramiko-py39.zip mentioned above, will be unzipped into /opt/python
import sys
# sys.path.append("/opt/third_party")
try:
import netifaces
except ModuleNotFoundError as err:
print(f"{err}")
sys.exit()
def lambda_handler(event, context):
real_interfaces = [x for x in netifaces.interfaces() if x != "lo"]
print(f"{real_interfaces=}")
for i in real_interfaces:
for j in netifaces.ifaddresses(i):
print(i, netifaces.ifaddresses(i)[j])
print()
print(f"{netifaces.gateways()=}")
return {'statusCode': 200, 'body': "ok 1"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment