Handslab Piyopiyokai # 1 https://handslab.doorkeeper.jp/events/42327 This is the material.
--I have an AWS account --You have permission to operate AWS Lambda with this account --Has a Twitter API key and access token --Pip command can be used in the terminal
__ Work with the client using a text editor. __
--Create a Python script "lambda_function.py". --Prepare the module to be used in the working directory --Zip the working directory
Prepare a working directory under the user directory. Execute the following command in the terminal
command
cd ~
mkdir piyopiyo2
Verification
ls | grep piyopiyo2
result
piyopiyo2
Go to your working directory and work.
command
cd piyopiyo2
Verification
pwd
result
/Users/****/piyopiyo2
Install the external module you want to use in your working directory. This time we will use tweepy as a module Reference: Reference
command
pip install tweepy -t ./
Verification
ls
result
examples requests_oauthlib-0.6.1.dist-info
oauthlib six-1.10.0.dist-info
oauthlib-1.0.3-py2.7.egg-info six.py
requests six.pyc
requests-2.9.1.dist-info tweepy
requests_oauthlib tweepy-3.5.0.dist-info
Replace with the key obtained during the''xxxxxxxxxxxxxxxxxxx'
Create a Python script. Create a script with the name lamba_function.py in the current directory (the directory where you installed the module with pip).
A quick summary of the basics of Python Reference: Summary of studying Python to use AWS Lambda
lamba_function.py
import json
import tweepy
CONSUMER_KEY = 'xxxxxxxxxxxxxxxxxxxx'
CONSUMER_SECRET = 'xxxxxxxxxxxxxxxxxxxx'
ACCESS_TOKEN = 'xxxxxxxxxxxxxxxxxxxx'
ACCESS_TOKEN_SECRET = 'xxxxxxxxxxxxxxxxxxxx'
def lambda_handler(event, context):
client = tweepy_client()
post_message = 'Hands-Lab piyopiyo kai'
post_twitter(client, post_message)
def post_twitter(client, message):
API = client
text = message
req = API.update_status(text)
return req
def tweepy_client():
consumer_key = CONSUMER_KEY
consumer_secret = CONSUMER_SECRET
access_token = ACCESS_TOKEN
access_token_secret = ACCESS_TOKEN_SECRET
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
return api
Execute the following command in the working directory
command
zip -r myfunc ./
that's all
Recommended Posts