Create a lambda layer on EC2 (advanced)

  1. Started with Amazon Linux on EC2 instance running t4g.micro in eu-west-1.
  2. aws configure (and enter in your credentials)

Prepare Machine

```code bash sudo yum update -y sudo su cd /home/ssm-user sudo yum install gcc bzip2-devel ncurses-devel gdbm-devel xz-devel sqlite-devel openssl-devel tk-devel uuid-devel readline-devel zlib-devel libffi-devel

## Install Python 3.9

```code bash
wget https://www.python.org/ftp/python/3.9.0/Python-3.9.0.tar.xz
tar -xJf Python-3.9.0.tar.xz
cd Python-3.9.0
./configure --enable-optimizations
make altinstall
export PATH=$PATH:/usr/local/bin

Setting up the environment

-needs to be updated with creation of virtual environment-

```code bash cd /home/ssm-user python3 -m venv tutorial-env source tutorial-env/bin/activate

## install software for layer

```code bash
cd tutorial-env
mkdir my-lambda-layer && 
cd my-lambda-layer
mkdir -p aws-layer/python/lib/python3.9/site-packages
pip3 install beautifulsoup4 --target aws-layer/python/lib/python3.9/site-packages

This concludes the PIP-installation now we need to create the zip file.

Create zip-file

```code bash cd aws-layer zip -r9 lambda-layer.zip .

Don't forget to include the "." at the end.

After zipping the packages it will have the name lambda-layer.zip

## Upload the zip file to a lambda layer using AWS CLI.

To create the lambda layer automatically using the AWS CLI you can use the command below:

``` code bash
aws lambda publish-layer-version \
    --layer-name Data-Preprocessing \
    --description "My Python layer" \
    --zip-file fileb://lambda-layer.zip \
    --compatible-runtimes python3.9

clean the complete shizzle

code bash cd .. && cd .. && cd .. && rm -rf tutorial-env

OR------------------

  1. aws s3 cp pill.zip s3:///pill.zip
  2. In the Lambda console, I then created a Lambda Layer using the pill.zip from the s3 bucket and installed it.
  3. I then added the layer to the Python 3.7 Lambda function.
  4. I then verified that the following code worked successfully

from PIL import Image

def lambda_handler(event, context): # TODO implement return { 'statusCode': 200, 'body': json.dumps('Hello from Lambda!') }