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
linux_wiki:lambda_python_function [2019/07/31 20:14]
billdozor [Lambda: Pre-Reqs]
linux_wiki:lambda_python_function [2019/08/09 22:29] (current)
billdozor [File Conversion Example: Configure]
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 28: Line 29:
 Create an IAM role Create an IAM role
   * Services > Security > IAM > Roles > Create Role   * Services > Security > IAM > Roles > Create Role
-    * Choose a service that will use the role: LambdaNext:Permissions +    * Choose a service that will use the role: Lambda 
-    * Create a new policy to allow object put/gets in your bucket. Example:<code json>{+    * Click 'Next:Permissions' 
 +    * **Required**: Attach the AWS managed policy "AWSLambdaBasicExecutionRole" to your new role in order to log your Lambda function to cloudwatch logs. 
 +    * Also, Create a new policy to allow object put/gets in your bucket. Example:<code json>{
     "Version": "2012-10-17",     "Version": "2012-10-17",
     "Statement": [     "Statement": [
Line 37: 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 43: Line 47:
         {         {
             "Sid": "VisualEditor1",             "Sid": "VisualEditor1",
 +            "Effect": "Allow",
 +            "Action": "s3:HeadBucket",
 +            "Resource": "*"
 +        }
 +    ]
 +}</code>
 +
 +----
 +
 +===== Stop/Start EC2 Example: Pre-Reqs =====
 +
 +Create an IAM role
 +  * Services > Security > IAM > Roles > Create Role
 +    * Choose a service that will use the role: Lambda
 +    * Click 'Next:Permissions'
 +    * **Required**: Attach the AWS managed policy "AWSLambdaBasicExecutionRole" to your new role in order to log your Lambda function to cloudwatch logs.
 +    * Also, Create a new policy to allow basic EC2 list,stop,start. Example:<code json>{
 +    "Version": "2012-10-17",
 +    "Statement": [
 +        {
 +            "Sid": "VisualEditor0",
             "Effect": "Allow",             "Effect": "Allow",
             "Action": [             "Action": [
-                "s3:ListAllMyBuckets", +                "ec2:DescribeInstances", 
-                "s3:ListBucket", +                "ec2:StartInstances", 
-                "s3:HeadBucket"+                "ec2:StopInstances"
             ],             ],
             "Resource": "*"             "Resource": "*"
Line 53: Line 78:
     ]     ]
 }</code> }</code>
-  * **Required**: Additionally, attach the AWS managed policy "AWSLambdaBasicExecutionRole" to your new role in order to log your Lambda function to cloudwatch logs. 
  
 ---- ----
 +
  
 ====== Lambda: Create Function ====== ====== Lambda: Create Function ======
Line 64: Line 89:
     * Select Author from scratch     * Select Author from scratch
     * Basic Information     * Basic Information
-      * Function name: mySpecialFunction (must be unique, no spaces)+      * Function name (example): mySpecialFunction (must be unique, no spaces)
       * Runtime: Select the latest Python (Python 3.7 at the time of this page creation)       * Runtime: Select the latest Python (Python 3.7 at the time of this page creation)
       * Permissions: Expand 'Choose or create an execution role'       * Permissions: Expand 'Choose or create an execution role'
Line 96: Line 121:
  
 ---- ----
 +
 +===== Stop/Start EC2 Example: Configure =====
 +
 +  * Click 'Add trigger'
 +    * Select CloudWatch Events from the dropdown
 +    * Rule: Create a new rule
 +    * Rule name: <any name>
 +    * Description: <any description>
 +    * Rule type: Select 'Schedule expression'
 +    * Schedule expression: 00 05 * * ? *
 +      * Expressions are in UTC: Example is every day at midnight (CDT)
 +      * Minute Hour DayOfMonth Month DayOfWeek Year
 +        * [[https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html|More info here]]
 +    * Ensure 'Enable trigger' is checked
 +    * Click "Add"
 +  * Select the Lambda function in the middle of the designer
 +    * The function can now be edited inline or you can package up a Python function and upload it.
 +      * Edit code inline: Use when the function is simple and does not require any dependencies that you would need a package installed for.
 +      * Upload a zip file: When you develop the function outside of the AWS console and/or need dependencies installed.
 +
 +----
 +
  
 ====== Lambda: Package and Upload Function ====== ====== Lambda: Package and Upload Function ======
  
-FIXME - TODO+Packaging and uploading a lambda function. 
 + 
 +===== File Conversion Example: Packaging ===== 
 + 
 +  * Create a directory named after the lambda function<code bash>mkdir convertImage</code> 
 + 
 +  * Install required packages into that directory<code bash>pip3 install --target convertImage/ Pillow</code> 
 + 
 +  * Create the lambda function file<code bash>vim convertImage/lambda_function.py 
 + 
 +import json 
 + 
 +def lambda_handler(event, context): 
 +    # TODO implement 
 +    return { 
 +        'statusCode': 200, 
 +        'body': json.dumps('Hello from Lambda!'
 +    }</code> 
 +    * **Notes** 
 +      * 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/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. 
 + 
 +  * Upload to AWS Lambda 
 +    * CLI Method<code bash>aws lambda update-function-code --function-name convertImage --zip-file fileb://convertImage.zip</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.1564618444.txt.gz
  • Last modified: 2019/07/31 20:14
  • by billdozor