====== Orig layer gen ======
to check and edit.....
# more layer-create.py
import json
# import urllib.request
import os
import shutil
import sys
import subprocess
import zipfile
import boto3
import botocore
from os import listdir
from os.path import isfile, join
def lambda_handler(event, context):
# https://stackoverflow.com/questions/58648739/how-to-check-if-python-package-is-latest-version-programmatically?noredirect=1&lq=1
# https://www.digitalocean.com/community/tutorials/how-to-use-subprocess-to-run-external-programs-in-python-3
# https://www.dataquest.io/blog/python-subprocess/
name = 'boto3'
# The -m flag makes sure that you are using the pip that's tied to the active Python executable.
latest_version = str(subprocess.run([sys.executable, '-m', 'pip', 'install', '{}==random'.format(name)], capture_output=True, text=True))
latest_version = latest_version[latest_version.find('(from versions:')+15:]
latest_version = latest_version[:latest_version.find(')')]
latest_version = latest_version.replace(' ','').split(',')[-1]
print("version", latest_version)
# install_path = '/tmp/' + latest_version
install_path = os.path.join('/tmp/', latest_version)
os.mkdir(install_path)
os.mkdir('/tmp/boto3')
installresponse = subprocess.run(["pip", "install", "boto3", "-t", "/tmp/boto3"], capture_output=True, text=True)
print("Install stdout", installresponse.stdout)
print("Install stderr", installresponse.stderr)
print("Install response", installresponse)
print("ZIP ------------")
shutil.make_archive("/tmp/boto3_" + latest_version, 'zip', "/tmp/boto3")
print("EndZIP ")
lsresponse = subprocess.run(["ls", "-l", "/tmp"], capture_output=True, text=True)
print("ls stdout", lsresponse.stdout)
#print("ls stderr", lsresponse.stderr)
#print("ls response:- ", lsresponse)
print("listdir")
print(listdir('/tmp'))
zipfilename = "boto3_" + latest_version + ".zip"
zipfilepath = os.path.join('/tmp/', zipfilename)
print("zipfile name:- ", zipfilename, zipfilepath)
# Replace period with underscore because period is not allowed in lambda layer names.
latest_version = latest_version.replace(".", "_")
print("version", latest_version)
lambda_client = boto3.client('lambda')
response = lambda_client.publish_layer_version(
CompatibleRuntimes=[
'python3.6',
'python3.7',
'python3.8',
'python3.9',
],
Content={
'ZipFile': zipfilepath,
},
Description='Lambda Layer for S3 Additional Checksum.',
LayerName='boto3-' + latest_version,
LicenseInfo='MIT',
)
print(response)
#run function with dummy data
lambda_handler(1,2)
# more lambda_layer_inLambda/lambda.py
import json
# import urllib.request
import os
import sys
import subprocess
#from pip._internal import main
#main(['index versions', '-I', '-q', 'boto3', '--target', '/tmp/', '--no-cache-dir', '--disable-pip-version-check'])
def lambda_handler(event, context):
name = 'boto3'
latest_version = str(subprocess.run([sys.executable, '-m', 'pip', 'install', '{}==random'.format(name)], capture_output=True, text=True))
latest_version = latest_version[latest_version.find('(from versions:')+15:]
latest_version = latest_version[:latest_version.find(')')]
latest_version = latest_version.replace(' ','').split(',')[-1]
print("version", latest_version)