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
Last revision Both sides next revision
linux_wiki:lambda_python_function [2019/07/31 21:57]
billdozor [File Conversion Example: Packaging]
linux_wiki:lambda_python_function [2019/08/09 22:20]
billdozor [File Conversion Example: 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 45: 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 57: Line 80:
  
 ---- ----
 +
  
 ====== Lambda: Create Function ====== ====== Lambda: Create Function ======
Line 120: Line 144:
     * **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>zip -r convertImage.zip convertImage</code>+  * 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   * Upload to AWS Lambda
  • linux_wiki/lambda_python_function.txt
  • Last modified: 2019/08/09 22:29
  • by billdozor