1
0

More than 1 year has passed since last update.

TwitterAPIをServerlessを使ってLambda + Pythonで動かしてAPIGatewayで呼び出せるようにしてみた

Posted at

前提

Node.jsで動かしてみてしっくりこなかったのでPython3でTwitterAPIを動かして見たいと思います。

Node.jsの記事
https://qiita.com/tama0571046/items/2bd9c94bbe3474243bea

2023/03/03追記
TwitterAPIが有料化したため利用が難しくなりましたが、サーバーレス自体は利用できると思いますので参考にしてください。

dockerのインストール

後述するserverless-python-requirementsで必要になります
こちらのサイトからインストールできます
https://www.docker.com/

AWSクレデンシャルの設定

アクセスキーとシークレットキーはAWSのユーザから取得してください

aws configure
> aws_access_key_id: ***
> aws_secret_access_key: ***
> region: ap-northeast-1
> output: json

serverlessとプラグインのインストール

npm install -g serverless
npm install --save serverless-python-requirements
npm install --save serverless-dotenv-plugin
npm install --save serverless-offline

プロジェクトを作成

sls create -t aws-python3 -p twitter_server_python3
cd twitter_server_python3

serverless.ymlを修正

twitter_server_python3/serverless.yml
service: twitter-server-python3

custom:
  dotenv:
    basePath: ./
  stage: ${env:STAGE}
  region: ${env:REGION}
  pythonRequirements:
    dockerizePip: true

provider:
  name: aws
  runtime: python3.7
  stage: ${self:custom.stage}
  region: ${self:custom.region}
  apiName: ${self:service}-${self:provider.stage}

plugins:
  - serverless-python-requirements
  - serverless-dotenv-plugin
  - serverless-offline

functions:
  hello:
    handler: handler.hello
    name: ${self:service}-hello-${self:provider.stage}
    events:
      - http:
          path: /hello
          method: get
          integration: lambda
  get_follow:
    handler: handler.get_follow
    name: ${self:service}-get_follow-${self:provider.stage}
    events:
      - http:
          path: /get_follow
          method: get
          integration: lambda
  get_list:
    handler: handler.get_list
    name: ${self:service}-get_list-${self:provider.stage}
    events:
      - http:
          path: /get_list
          method: get
          integration: lambda

.envファイルを追加

プロジェクト直下に**.env**ファイルを作成

vim .env
twitter_server_python3/.env
STAGE=dev
REGION=ap-northeast-1

CONSUMER_KEY=***
CONSUMER_SECRET=***
ACCESS_TOKEN=***
ACCESS_TOKEN_SECRET=***

requirements.txtを追加

プロジェクト直下にrequirements.txtファイルを作成

vim requirements.txt
twitter_server_python3/requirements.txt
tweepy==3.8.0

ローカル環境での実行

仮想環境を作成して仮想環境内に入る

python3.7 -m venv .venv
source .venv/bin/activate

仮想環境内でモジュールをインストール

.venv
pip install -r requirements.txt

ローカル環境で実行

.venv
sls invoke local --function hello

仮想環境から出る

deactivate

Lambda

デプロイ

sls deploy -v

実行

sls invoke -f hello --log

デプロイした内容を削除

sls remove

API Gateway

AWSにログインしてLambdaの今回作成した関数を開く
デザイナーにAPIGatewayがあるはずなので、開くとAPI エンドポイントからURLが確認できます

curl https://*****.execute-api.ap-northeast-1.amazonaws.com/dev/get_follow

POSTがaxiosからうまく行かない

APIGateway

アクションからCORSの有効化をクリック

POSTメソッドから統合リクエストをクリック
マッピングテンプレートをテンプレートが定義されていない場合(推奨)をクリック
application/jsonのリンクをクリックしてテンプレートの生成をメソッドリクエストのパススルー
を選択して保存

axios

pulugin/axios.jsを追加して下記を記述

plugin/axios.js
export default ({ $axios }) => {
  $axios.defaults.headers.common = {}
}

nuxt.config.jsに追記

nuxt.config.js
plugins: [
  ...
  "@/plugins/axios"
],

index.vueでaxiosを読み込み
bodyをJSON.stringfyにするのを忘れない

index.vue
import axios from 'axios'

export default {
  components: { axios },
  ...
  methods: {
    async onSave() {
      await axios.post(postListMembersURL, JSON.stringify({}))
    }
  }

ブラウザ

CORSの設定を追加

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0