General Information
Creating Lambda functions with Python for automated actions.
Checklist
In order to create a new Lambda function, there are some pre-reqs:
See below for Lambda examples.
Create a S3 Bucket
Create an IAM role
{ "Version": "2012-10-17", "Statement": [ { "Sid": "VisualEditor0", "Effect": "Allow", "Action": [ "s3:PutObject", "s3:GetObject", "s3:ListBucket" ], "Resource": "arn:aws:s3:::my-test-bucket/*" }, { "Sid": "VisualEditor1", "Effect": "Allow", "Action": "s3:HeadBucket", "Resource": "*" } ] }
Create an IAM role
{ "Version": "2012-10-17", "Statement": [ { "Sid": "VisualEditor0", "Effect": "Allow", "Action": [ "ec2:DescribeInstances", "ec2:StartInstances", "ec2:StopInstances" ], "Resource": "*" } ] }
After the pre-reqs are in place, the function can be created.
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.
Packaging and uploading a lambda function.
mkdir convertImage
pip3 install --target convertImage/ Pillow
vim convertImage/lambda_function.py import json def lambda_handler(event, context): # TODO implement return { 'statusCode': 200, 'body': json.dumps('Hello from Lambda!') }
cd convertImage zip -r convertImage.zip .
aws lambda update-function-code --function-name convertImage --zip-file fileb://convertImage.zip