Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision Last revision Both sides next revision | ||
linux_wiki:lambda_python_function [2019/07/30 22:54] billdozor created |
linux_wiki:lambda_python_function [2019/08/09 22:20] billdozor [File Conversion Example: Pre-Reqs] |
||
---|---|---|---|
Line 14: | Line 14: | ||
In order to create a new Lambda function, there are some pre-reqs: | In order to create a new Lambda function, there are some pre-reqs: | ||
* Additional resources created (such as a S3 bucket if the lambda function will access a bucket) | * Additional resources created (such as a S3 bucket if the lambda function will access a bucket) | ||
- | * An IAM role with an attached | + | * An IAM role with one or more attached |
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: Lambda, Next: | + | * Choose a service that will use the role: Lambda |
- | * Create a new policy to allow object put/gets in your bucket. Example:< | + | * Click 'Next: |
+ | * **Required**: | ||
+ | * Also, Create a new policy to allow object put/gets in your bucket. Example:< | ||
" | " | ||
" | " | ||
Line 37: | Line 40: | ||
" | " | ||
" | " | ||
- | " | + | " |
+ | " | ||
], | ], | ||
" | " | ||
Line 43: | Line 47: | ||
{ | { | ||
" | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | ] | ||
+ | }</ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== 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 ' | ||
+ | * **Required**: | ||
+ | * Also, Create a new policy to allow basic EC2 list, | ||
+ | " | ||
+ | " | ||
+ | { | ||
+ | " | ||
" | " | ||
" | " | ||
- | "s3:ListAllMyBuckets", | + | "ec2:DescribeInstances", |
- | "s3:ListBucket", | + | "ec2:StartInstances", |
- | "s3:HeadBucket" | + | "ec2:StopInstances" |
], | ], | ||
" | " | ||
Line 53: | Line 78: | ||
] | ] | ||
}</ | }</ | ||
- | * **Required**: | ||
---- | ---- | ||
+ | |||
====== 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: | * Permissions: | ||
Line 75: | Line 100: | ||
====== Lambda: Configure Function ====== | ====== Lambda: Configure Function ====== | ||
+ | |||
+ | After initial creation, you are brought to the Lambda function configuration page, with the designer at the top. | ||
+ | |||
+ | The designer allows you to add triggers (things that tell your function to start) and inspect what resources your function has access to. | ||
+ | |||
+ | ===== File Conversion Example: Configure ===== | ||
+ | |||
+ | * Click 'Add trigger' | ||
+ | * Select S3 from the dropdown | ||
+ | * Bucket: Select your bucket that you will upload files to | ||
+ | * Event type: Have it trigger on 'All object create events' | ||
+ | * Prefix: Optionally, type a prefix if you only want to convert files in certain prefixes/ | ||
+ | * Suffix: Enter ' | ||
+ | * Ensure ' | ||
+ | * Click " | ||
+ | * 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 ====== | ||
+ | |||
+ | Packaging and uploading a lambda function. | ||
+ | |||
+ | ===== File Conversion Example: Packaging ===== | ||
+ | |||
+ | * Create a directory named after the lambda function< | ||
+ | |||
+ | * Install required packages into that directory< | ||
+ | |||
+ | * Create the lambda function file< | ||
+ | |||
+ | import json | ||
+ | |||
+ | def lambda_handler(event, | ||
+ | # TODO implement | ||
+ | return { | ||
+ | ' | ||
+ | ' | ||
+ | }</ | ||
+ | * **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:// | ||
+ | |||
+ | * Package up the directory into a zip<code bash>cd convertImage | ||
+ | zip -r convertImage.zip .</ | ||
+ | * **Important**: | ||
+ | |||
+ | * Upload to AWS Lambda | ||
+ | * CLI Method< | ||
+ | * Web Console Method | ||
+ | * Login to your AWS console | ||
+ | * Services > Compute > Lambda | ||
+ | * Click " | ||
+ | * Under " | ||
+ | * Click the dropdown and select " | ||
+ | * Under " | ||
+ | * Browse to your file and select it | ||
+ | * In the top right, click " | ||
---- | ---- | ||