100 days in Cloud
Lab 77
Install Python module for Lambda using Cloud9
Services Covered
- Lambda
- Cloud9
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
- 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.
- Go to Cloud9 and create a new environment. Choose direct access with EC2 instance. This will take some time:
- Import the Lambda function to Cloud9. On the left side click on then choose your Lambda and install_request_module then download.
- Install request library in Terminal:
- Run those commands:
Then to zip the library run:pip install --target ~/environment/<<LAMBDA NAME>>/ requests
Download that zip filezip -r ../my-deployment-package.zip *
- Back in Lambda upload that zip package and test your function:
- Delete your environment in Cloud9.
Lab files and Github link
- lambda.py
- https://github.com/CloudedThings/100-Days-in-Cloud/tree/main/Labs/77-Install-Python-Modules-Using-Cloud9