Post an image tweet with Lambda when S3 event triggered

Cloud Managed Image

In my app, posting an image in Twitter is one of the important functions. It should have been automated and isolated from other functions in loose coupling architecture so I used lambda function to post a tweet with Tweepy library in Python. Also this function can be triggered when other functions upload the retrieved images from web in specific S3 bucket.

I refer to the tutorial document of S3 Using AWS Lambda with Amazon S3 and modfied the Python code and pack the all required code and libraries to achieve the purpose. In this article I’ll cover required resources in AWS and Python code that I uploaded in lambda function. I’ll explain also what IAM role and S3 I needed to create.

Versions

  • Local Python 3.7.3
  • Lambda Runtime 3.7
  • Tweepy 3.9.0 and dependent libraries

Resources

Lambda resources

  • A lambda function
  • An access policy applied for a lambda function that grants Amazon S3 permission to invoke a tweet post

IAM resources

  • An execution role that grants permissions that a lambda function needs through the permissions policy associated with this role

S3 resources

  • A source bucket that invokes a lambda function with a notification configuration

We will create Python code locally and zip it with Tweepy and dependent libraries, then move to AWS console to create the rest of components such as lambda function, S3, and IAM policy/role.

Python code

First create a folder or directory on your system locally, and prepare a file for Python code under the created folder or directory. I did it on my Linux system.

$ mkdir lambda_function
$ vim lambda_function.py

The Python code is straightforward. When S3 event is recognized, the lambda function will know S3 bucket name and key name, then save the uploaded file under /tmp temporarily.

At last, the lambda handler calls the function tweet_with_image and pass the path of the image /tmp directory in lambda environment. The function tweet_with_image uses Tweepy library to post a tweet with the image in Twitter.

Once you write or copy this code in file, please save the file locally. Next we need to install Tweepy and related libraries and pack the files as a zip file. Unless you develop mid to large size package of Lambda, you can include all required codes and files in a single zip file and upload to lambda function. Here’s the commands I ran on my Linux console.

$ pip install tweepy -t .
$ chmod -R 755 ./*
$ zip -r lambda_function.zip .

$ ls -al | grep lambda
-rwxr-xr-x  1 user user    1440 Oct 1 16:54 lambda_function.py
-rw-rw-r--  1 user user 1435763 Oct 1 17:34 lambda_function.zip

Prepare a lambda function

The Python code is ready now, we need a place to upload this code as a lambda function. You can log in the AWS console and create a lambda function under Lambda service.

Function name and Runtime are mandatory in Basic information, in this sample I chose Runtime Python 3.7 as below. Function name is up to you.

Permissions, this is an IAM role you apply for the Lambda function. I took the policy template Amazon S3 object read-only permissions and attach it to newly created role. This is called an execution role for lambda.

Function code has the default lambda_function code once your lambda function is created, please click on Actions then choose either Upload a zip file and have your local zip in the AWS console. Once you saved your zip file, your local code is uploaded in the AWS console lambda_function.py pane. The lambda function is now ready to use.

Prepare S3 resource

We need to prepare S3 resource, and add a trigger in lambda function by the created S3 bucket to invoke a notification. Please note that the created lambda function sees the prefix in the bucket, so not only bucket but a folder will be also needed for the lambda function to be triggered.

You can use either the AWS CLI or console, I created one bucket with default policy and created “test” folder under the bucket. If an image is uploaded in this folder, the lambda function can be invoked and the saved image would be tweeted.

Back to the lambda function, and we click on “Add trigger” to specify what event can invoke this lambda function. For my case, an event should be “putting a png image in test folder of the created bucket”.

Complete the Trigger configuration and click on “Add” button at the bottom.

At last, we need to set up environment variable for tweeting. Twitter authentication requires 4 keys, consumer key, consumer secret, access token and access secret. You must set up twitter app in twitter developer page and publish your application’s token.

It’s all done!! You can save your image or use your program to store an image in the bucket (under test folder). An event is triggered and invoke the created lambda function. It downloads the image from S3 and tweet with the image automatically in Twitter. Sweet.

Leave a Reply

Your email address will not be published. Required fields are marked *