linux_wiki:lambda_python_function

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
linux_wiki:lambda_python_function [2019/07/31 21:38]
billdozor [File Conversion Example: Packaging]
linux_wiki:lambda_python_function [2019/08/09 22:13]
billdozor [Lambda: Pre-Reqs]
Line 18: Line 18:
 See below for Lambda examples. See below for Lambda examples.
   * File Conversion Example - convert images from one format to another when created in a S3 bucket.   * File Conversion Example - convert images from one format to another when created in a S3 bucket.
 +  * Stop/Start EC2 Instances Example - stop/start ec2 instances with a certain tag key:value on a schedule.
  
  
Line 39: Line 40:
             "Action": [             "Action": [
                 "s3:PutObject",                 "s3:PutObject",
-                "s3:GetObject"+                "s3:GetObject", 
 +                "s3:ListBucket"
             ],             ],
             "Resource": "arn:aws:s3:::my-test-bucket/*"             "Resource": "arn:aws:s3:::my-test-bucket/*"
Line 46: Line 48:
             "Sid": "VisualEditor1",             "Sid": "VisualEditor1",
             "Effect": "Allow",             "Effect": "Allow",
-            "Action": +            "Action": "s3:HeadBucket",
-                "s3:ListAllMyBuckets", +
-                "s3:ListBucket", +
-                "s3:HeadBucket" +
-            ],+
             "Resource": "*"             "Resource": "*"
         }         }
Line 104: Line 102:
 ===== File Conversion Example: Packaging ===== ===== File Conversion Example: Packaging =====
  
-  * Create the directory and python3 virtual environment<code bash>mkdir convertImage +  * Create a directory named after the lambda function<code bash>mkdir convertImage</code>
-cd convertImage +
-python3 -m venv venv</code>+
  
-  * Activate the virtualenv and install required packages<code bash>source venv/bin/activate +  * Install required packages into that directory<code bash>pip3 install --target convertImage/ Pillow</code>
-pip install Pillow</code>+
  
-  * Create the lambda function file<code bash>vim lambda_function.py+  * Create the lambda function file<code bash>vim convertImage/lambda_function.py
  
 import json import json
Line 123: Line 118:
     * **Notes**     * **Notes**
       * The above is the default code that is created for you in a from scratch lambda function in the console.       * The above is the default code that is created for you in a from scratch lambda function in the console.
-      * For the File conversion example and others, see: https://gitlab.com/whowe/aws+      * For the File conversion example and others, see: https://gitlab.com/whowe/aws/tree/master/lambda/ 
 + 
 +  * Package up the directory into a zip<code bash>cd convertImage 
 +zip -r convertImage.zip .</code> 
 +    * **Important**: The archive that is created should NOT contain a parent directory, as this parent directory already exists in AWS Lambda for your function. The "lambda_function.py" needs to be at the root of your structure.
  
-  * Deactivate the virtualenv<code bash>deactivate</code> +  * Upload to AWS Lambda 
-  Package up the directory into a zip<code bash>cd .. +    * CLI Method<code bash>aws lambda update-function-code --function-name convertImage --zip-file fileb://convertImage.zip</code> 
-zip -r convertImage.zip convertImage</code>+    Web Console Method 
 +      * Login to your AWS console 
 +      * Services > Compute > Lambda 
 +      * Click "Functions > <your function name>" 
 +      * Under "Function code"Code entry type" 
 +        * Click the dropdown and select "Upload a .zip file" 
 +        * Under "Function package", click "Upload" 
 +        * Browse to your file and select it 
 +      * In the top right, click "Save"
  
 ---- ----
  
  • linux_wiki/lambda_python_function.txt
  • Last modified: 2019/08/09 22:29
  • by billdozor