Real‐Time‐Example‐upload‐image‐convert‐Thumnel - amresh087/newronaRepos GitHub Wiki

sls -->select project python starter --> now enter project name --> open it in vs code

if we external API mean jar or library then we need to create layer.

So, in serverless framework then first generate dependeny

   sls plugin install -n serverless-python-requirements

When you run above command then it will generate all dependency in package.json and lock-package.json

SO, Now we need to add dependency in servrless.yml weneed to follow (https://www.serverless.com/plugins/serverless-python-requirements)

     service: final-python-thumnail

     frameworkVersion: '3'

     provider:
       name: aws
       runtime: python3.9
       region: us-east-1
       profile: serverless-admin
       stage: dev
       timeout: 10
       memorySize: 128
       environment:
         THUMBNAIL_SIZE: 128
         REGION_NAME: ${self:provider.region}

       iam:
         role:
           statements:
             - Effect: 'Allow'
               Resource: '*'
               Action: 's3:*'

     functions:
       s3_thumbnail_genrator:
         handler: handler.s3_thumbnail_genrator

     plugins:
       - serverless-python-requirements
     custom:
       pythonRequirements:
         dockerizePip: true
         layer:
           name: python-numpy
           description: "Layer which contains numpy library"
           compatibleRuntimes:
             - python3.9

Now,we need to create requirements.txt and added layer name like below

     numpy==1.21.0

Note: Here we are using dockerizePip: true mean local docker will running stage otherwise it will not work

Without docker deployment

serverless.yml

    service: final-python-thumnail
    frameworkVersion: '3'
    provider:
      name: aws
      runtime: python3.9
      region: us-east-1
      profile: serverless-admin
      stage: dev
      timeout: 10
      memorySize: 128
      environment:
        THUMBNAIL_SIZE: 128
        REGION_NAME: ${self:provider.region}

      iam:
        role:
          statements:
            - Effect: 'Allow'
              Resource: '*'
              Action: 's3:*'

    functions:
      s3_thumbnail_genrator:
        handler: handler.s3_thumbnail_genrator
        layers:
          - { Ref: MyLayerLambdaLayer } 

    layers:
      MyLayer:
        path: layer
        compatibleRuntimes:
          - python3.9

    #plugins:
    #  - serverless-python-requirements
    #custom:
    #  pythonRequirements:
    #    dockerizePip: true
    #    layer:
    #      name: python-numpy
    #      description: "Layer which contains numpy library"
    #      compatibleRuntimes:
    #        - python3.9

Now, we need to install dependency in our folder

    pip install "boto3==1.20.1"  -t layer/python
    pip install "pillow==10.3.0"  -t layer/python

AWC CLI approach: we need to use aws cli then create dependency

    pip install -t .  

Now, we need to compress all files in zip

Now we need to run below command

     aws lambda create-function --function-name final-python-thumnail-dev-s3_thumbnail_genrator  --zip-file fileb://final-python-thumnail.zip --handler s3_thumbnail_genrator.handler.s3_thumbnail_genrator  --runtime python3.9  --role arn:aws:iam::699782546312:role/final-python-thumnail-dev-us-east-1-lambdaRole