Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
linux_wiki:lambda_python_function [2019/07/31 21:56] billdozor [File Conversion Example: Packaging] |
linux_wiki:lambda_python_function [2019/08/09 22:29] 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 39: | Line 40: | ||
" | " | ||
" | " | ||
- | " | + | " |
+ | " | ||
], | ], | ||
" | " | ||
Line 45: | 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 57: | Line 80: | ||
---- | ---- | ||
+ | |||
====== Lambda: Create Function ====== | ====== Lambda: Create Function ====== | ||
Line 97: | 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: | ||
+ | * Rule type: Select ' | ||
+ | * Schedule expression: 00 05 * * ? * | ||
+ | * Expressions are in UTC: Example is every day at midnight (CDT) | ||
+ | * Minute Hour DayOfMonth Month DayOfWeek Year | ||
+ | * [[https:// | ||
+ | * 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 ====== | ====== Lambda: Package and Upload Function ====== | ||
Line 120: | Line 166: | ||
* **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:// | + | * 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**: | ||
- | * Package up the directory into a zip<code bash>cd .. | ||
- | zip -r convertImage.zip convertImage</ | ||
* Upload to AWS Lambda | * Upload to AWS Lambda | ||
* CLI Method< | * CLI Method< |