0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Lambda基礎

0
Posted at

ゴール

  1. Lambda + API Gatewayをコンソールで構築
  2. LambdaをPythonで実装
  3. API GatewayでGET/POSTを作る
  4. LambdaからDynamoDBを操作
  5. AWS SAMで同じ構成をコード化
  6. LambdaをDockerコンテナイメージでデプロイ
  7. CI/CD(GitHub Actionsなど)を構築

Lambda + API Gatewayをコンソールで構築

Lambda構築

  • 関数名:hello-api
  • ランタイム:Python 3.13 など
  • アーキテクチャ:arm64 でも x86_64 でもOK
  • 実行ロール:新しいロールを作成

Lambdaのコードを以下にします。

import json

def lambda_handler(event, context):

    return {

        "statusCode": 200,

        "headers": {

            "Content-Type": "application/json"

        },

        "body": json.dumps({

            "message": "Hello World"

        })

    }

テスト実行

テストタブから実行します。

とりあえず、以下で実行。
{}

実行結果
image.png

API Gateway構築

今回は HTTP API を使うのがおすすめです。
API Gatewayには大きく、

  • HTTP API
  • REST API

があります。
単純なLambda APIなら、まずは HTTP API で十分です。

Routeを設定する

GET /hello

というAPIにします。
つまり、

GET https://xxxxx.execute-api.ap-northeast-1.amazonaws.com/hello

/helloを前に作成したLambdaへ統合します。
image.png

curlで実行

 curl https://xxxxxxxxx.ap-northeast-1.amazonaws.com/hello 
{"message": "Hello World"}%        

成功!!

次回、 LambdaをPythonで実装
処理を持たせたLambdaで色々実行

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?