[PYTHON] Regularly post to Twitter using AWS lambda!

Overview

I thought it would be convenient if I could post regularly on Twitter using AWS Lambda, so I summarized it. スクリーンショット 2020-10-16 23.34.23.png

Creation procedure

Creation flow

  1. Build an environment with Docker
  2. Twitter post from Python

Details

1. Build Lambda environment with Docker

I used Docker to use non-standard libraries with lambada. To use docker with lambda, it is convenient to use lambci / docker-lambda. This article was helpful. [Reproduction of local development environment with AWS Lambda x Python](https://qiita.com/i35_267/items/15b981acae8cf7c763ae#-docker-lambda%E3%82%92%E8%A7%A6%E3%81% A3% E3% 81% A6% E3% 81% BF% E3% 82% 8B)

Dockerfile


FROM lambci/lambda:build-python3.8
ENV AWS_DEFAULT_REGION ap-northeast-1

ADD . .

CMD pip3 install -r requirements.txt -t /var/task && \
  zip -9 deploy_package.zip lambda_function.py && \
  zip -r9 deploy_package.zip *

I'm not sure about the options for zip -9.

requirements.txt


twitter

2. Twitter post

You can easily tweet using the library here sixohsix / twitter. Articles that I used as a reference ・ Post to twitter easily with Python3

lambda_funcyion.py


import twitter

def lambda_handler(event, context):
    
    auth = twitter.OAuth(
      token="token", 
      token_secret="Token secret", 
      consumer_key="Key", 
      consumer_secret="secret"
    )
    context = 'I did my best today too!'
    t = twitter.Twitter(auth=auth)
    t.statuses.update(status=context)
   
    

3. Local debugging

It is troublesome to give it to lambda one by one and check it, so check it locally.

$ docker run -v "$PWD":/var/task lambci/lambda:python3.8 lambda_function.lambda_handler [event]

You should be posting with this.

4. Deploy

First, put it in a zip file.

$ docker build -t mylambda .
$ docker run -v "$PWD":/var/task mylambda

Next, create a lambda function from the AWS console.

Upload zip file

Register environment variables

It's OK if you can register the time of regular execution!

reference -Be careful about the time difference when using cron expressions in Amazon CloudWatch EventsOfficial document rule schedule formula

Impressions ・ Future ...

・ I want to incorporate it into other flows.

Recommended Posts

Regularly post to Twitter using AWS lambda!
Post to Twitter using Python
[Lambda] [Python] Post to Twitter from Lambda!
I tried to delete bad tweets regularly with AWS Lambda + Twitter API
How to set layer on Lambda using AWS SAM
I tried to get an AMI using AWS Lambda
Post to your account using the API on Twitter
Web scraping using AWS lambda
Post images of Papillon regularly on Python + AWS Lambda + Slack
Summary if using AWS Lambda (Python)
Tweet WakaTime Summary using AWS Lambda
Using Lambda with AWS Amplify with Go
Easily post to twitter with Python 3
[AWS] Using ini files with Lambda [Python]
Connect to s3 with AWS Lambda Python
Summary of how to write AWS Lambda
Tweet in Chama Slack Bot ~ How to make a Slack Bot using AWS Lambda ~
[Introduction to AWS] The first Lambda is Transcribe ♪
Serverless scraping using selenium with [AWS Lambda] -Part 1-
Feel free to turn Python using the library into an AWS Lambda function
Post youtube soaring title to twitter with python3
[AWS / Lambda] How to load Python external library
Summary of studying Python to use AWS Lambda
I tried to create an environment to check regularly using Selenium with AWS Fargate
I tried to use Twitter Scraper on AWS Lambda and it didn't work.
A note that connects to Lambda via AWS API Gateway (HTTP API) to process POST data
A little trick to know when writing a Twilio application using Python on AWS Lambda
Terraform configured to launch AWS Lambda from Amazon SQS
Check types_map when using mimetypes on AWS Lambda (Python)
Amazon SNS → AWS Lambda → Slack → AWS Chatbot to execute AWS commands
Try to delete tweets in bulk using Twitter API
I want to AWS Lambda with Python on Mac!
Tweet from AWS Lambda
Search Twitter using Python
Example of using lambda
I wrote a script to create a Twitter Bot development environment quickly with AWS Lambda + Python 2.7
Try AWS Lambda Destinations
[LINE Notify API, AWS] Regularly send buzzing tweets to group LINE
[AWS] Try adding Python library to Layer with SAM + Lambda (Python)
Addictive points when downloading files using boto on AWS Lambda
Output CloudWatch Logs to S3 with AWS Lambda (Pythyon ver)
Create API with Python, lambda, API Gateway quickly using AWS SAM
[Python] Regularly export from CloudWatch Logs to S3 with Lambda
Tweet Now Playing to Twitter using the Spotify API. [Python]
Deploy external modules (numpy, scipy, requests, etc.) to AWS Lambda
A quick explanation from creating AWS Lambda Layers to linking
[Python] Allow pip3 packages to be imported on AWS Lambda
Send a request from AWS Lambda to Amazon Elasticsearch Service
[End of 2020] A memo to start using AWS CLI (Version 2)