Skip to content

Commit 7bd9672

Browse files
外部ライブラリを使用したLambda関数をデプロイ
1 parent ac2ee91 commit 7bd9672

5 files changed

Lines changed: 60 additions & 4 deletions

File tree

.github/workflows/deploy_lambda.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ permissions:
1414
contents: read
1515
defaults:
1616
run:
17-
working-directory: "hello_lambda"
17+
working-directory: "external_library_lambda"
1818
env:
1919
PYTHON_VERSION: 3.13
20-
function-name: hello_lambda_actions
20+
function-name: external_library_lambda
2121
jobs:
2222
main:
2323
runs-on: ubuntu-latest
@@ -42,7 +42,7 @@ jobs:
4242
uses: aws-actions/aws-lambda-deploy@v1.0.1
4343
with:
4444
function-name: ${{ env.function-name }}
45-
code-artifacts-dir: ./hello_lambda
45+
code-artifacts-dir: ./external_library_lambda
4646
handler: app.lambda_handler
4747
runtime: python${{ env.PYTHON_VERSION }}
4848
role: ${{ env.lambda-role-arn }}

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.env
2-
**/.env
2+
**/.env
3+
.venv

external_library_lambda/__init__.py

Whitespace-only changes.

external_library_lambda/app.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import json
2+
import requests
3+
4+
5+
def lambda_handler(event, context):
6+
"""
7+
PokeAPIを呼び出してポケモンの情報を取得するLambdaハンドラ関数
8+
"""
9+
pokemon_id = "25" # ピカチュウのID
10+
api_url = f"https://pokeapi.co/api/v2/pokemon/{pokemon_id}"
11+
12+
try:
13+
# PokeAPIにGETリクエストを送信
14+
response = requests.get(api_url)
15+
16+
# レスポンスが成功したかチェック (ステータスコード 200)
17+
response.raise_for_status()
18+
19+
# レスポンスのJSONをパース
20+
data = response.json()
21+
pokemon_name = data.get("name")
22+
23+
# 成功レスポンスを返す
24+
return {
25+
"statusCode": 200,
26+
"headers": {"Content-Type": "application/json"},
27+
"body": json.dumps(
28+
{
29+
"message": "Successfully fetched data from PokeAPI!",
30+
"pokemonName": pokemon_name,
31+
}
32+
),
33+
}
34+
35+
except requests.exceptions.RequestException as e:
36+
# API呼び出しでエラーが発生した場合
37+
print(f"Error: {e}")
38+
return {
39+
"statusCode": 500,
40+
"headers": {"Content-Type": "application/json"},
41+
"body": json.dumps({"message": "Failed to fetch data from PokeAPI."}),
42+
}
43+
44+
45+
# --- ローカルテスト用のコードを追加 ---
46+
if __name__ == "__main__":
47+
# Lambda関数を直接呼び出し、結果をコンソールに表示する
48+
# eventとcontext引数にはNoneや空の辞書{}を渡す
49+
print("--- Running Local Test ---")
50+
result = lambda_handler({}, None)
51+
52+
# 見やすいように整形して出力
53+
print(json.dumps(result, indent=2, ensure_ascii=False))
54+
print("--- End of Local Test ---")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
requests==2.31.0

0 commit comments

Comments
 (0)