[Piyopiyokai # 1] Let's play with Lambda: Creating a Python script

Handslab Piyopiyokai # 1 https://handslab.doorkeeper.jp/events/42327 This is the material.

Prerequisites

--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

Script creation

__ 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

Working directory preparation

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

Installation of external modules

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'

Creating lamba_function.py

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
    

Zip

Execute the following command in the working directory

command


zip -r myfunc ./

that's all

Recommended Posts

[Piyopiyokai # 1] Let's play with Lambda: Creating a Python script
[Piyopiyokai # 1] Let's play with Lambda: Creating a Lambda function
[Piyopiyokai # 1] Let's play with Lambda: Get a Twitter account
[Let's play with Python] Make a household account book
Let's make a GUI with python.
Let's play with Excel with Python [Beginner]
Write a batch script with Python3.5 ~
Let's create a script that registers with Ideone.com in Python.
Creating a simple PowerPoint file with Python
Let's make a shiritori game with Python
Let's create a free group with Python
Play with 2016-Python
Let's make a voice slowly with Python
Let's make a web framework with Python! (1)
Let's make a Twitter Bot with Python!
Let's make a web framework with Python! (2)
Play with Lambda layer (python) for about 5 minutes
Procedure for creating a LineBot made with Python
A memo when creating a python environment with miniconda
Commands for creating a python3 environment with virtualenv
A python lambda expression ...
[Grasshopper] When creating a data tree on Python script
Let's make a simple game with Python 3 and iPhone
Problems when creating a csv-json conversion tool with python
[For play] Let's make Yubaba a LINE Bot (Python)
Create a Layer for AWS Lambda Python with Docker
[Super easy] Let's make a LINE BOT with Python.
[Python] Road to a snake charmer (5) Play with Matplotlib
Let's play with 4D 4th
Let's play with Amedas data-Part 1
Operate TwitterBot with Lambda, Python
POST json with Python3 script
Let's run Excel with Python
Creating an egg with python
Let's play with Amedas data-Part 4
Let's try a shell script
[Python] Play with Discord's Webhook.
Make a fortune with Python
Let's write python with cinema4d.
Play RocketChat with API / Python
Let's play with Amedas data-Part 3
Let's play with Amedas data-Part 2
Create a directory with python
Let's make a websocket client with Python. (Access token authentication)
I replaced the Windows PowerShell cookbook with a python script.
Quickly take a query string with API Gateway-> Lambda (Python)
Let's create a PRML diagram with Python, Numpy and matplotlib.
Creating a Python script that supports the e-Stat API (ver.2)
[Let's play with Python] Image processing to monochrome and dots
Creating a GUI as easily as possible with python [tkinter edition]
I wrote a script to create a Twitter Bot development environment quickly with AWS Lambda + Python 2.7
[Python] What is a with statement?
Solve ABC163 A ~ C with Python
Operate a receipt printer with python
A python graphing manual with Matplotlib.
Play with a turtle with turtle graphics (Part 1)
Creating a decision tree with scikit-learn
Creating a Flask server with Docker
Face detection with Lambda (Python) + Rekognition
Solve ABC166 A ~ D with Python
[Python 3.8 ~] How to define a recursive function smartly with a lambda expression