lab77
Lab 77 diagram

100 days in Cloud

Lab 77

Install Python module for Lambda using Cloud9


Services Covered

  • lambdaLambda
  • cloud9Cloud9

Lab description

For a Lambda function to work with the request library it needs to be uploaded as a separate module. To do that a new Cloud9 environment will be started and in it, a pip commands executed in a terminal. Then modules will be packed and upload to Lambda function.

  • Create a Lambda function
  • Create Cloud9 environment
  • Download packages using Terminal and pip
  • Upload libraries in to Lambda

Lab date

15-10-2021


Prerequisites

  • AWS account

Lab steps

  1. Create a new Lambda function with Python som runtime.
    import json
     import requests
     def lambda_handler(event, context):    
    r = requests.get('https://github.com')     
    print(r.status_code)   
     print(r.headers['Date'])    
    print(r.headers['server'])    
    return {        
    'statusCode': 200,        
    'body': json.dumps('success')    }
    

    If you try and test it will respond with error message because request package is not part of Lambda Python standard library.

  2. Go to Cloud9 and create a new environment. Choose direct access with EC2 instance. This will take some time:
    cloud9
  3. Import the Lambda function to Cloud9. On the left side click on then choose your Lambda and install_request_module then download.
  4. Install request library in Terminal:
    77
  5. Run those commands:
    pip install --target ~/environment/<<LAMBDA NAME>>/ requests
    Then to zip the library run:
    zip -r ../my-deployment-package.zip *
    Download that zip file
  6. Back in Lambda upload that zip package and test your function:

     

    lambda
  7. Delete your environment in Cloud9.

Lab files and Github link


Acknowledgements

Tags: