AWS SAM Hello World後の関数作成に関して

スポンサードサーチ

はじめに

下記記事にて、Hello Worldまで行いました。
この記事の内容は、チュートリアル: Hello World アプリケーションのデプロイ にも書かれています。

Hello Worldまで試した後。
さて、新しく関数を作成するか。。。
で?どうやるんだ?っとなったのでメモ

GET関数の作成

ディレクトリの作成

hello_worldを基に作成した方が早そうなので、ディレクトリごとコピーします

$ cp -r hello_world get_sample

Functionの記載

次にtemplete.yamlのResource部分に作成するFunctionの内容を追記します。
基本はHelloWorldFunction と同じです。

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: hello_world/
      Handler: app.lambda_handler
      Runtime: python3.9
      Architectures:
        - x86_64
      Events:
        HelloWorld:
          Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
          Properties:
            Path: /hello
            Method: get
+ GetSampleFunction:
+  Type: AWS::Serverless::Function
+  Properties:
+    CodeUri: get_sample/
+    Handler: app.lambda_handler
+     Runtime: python3.9
+    Architectures:
+      - x86_64
+    Events:
+     GetSample:
+        Type: Api
+        Properties:
+          Path: /get-sample
+          Method: get
  • Function名は任意
  • Properties > CodeUri: 該当するディレクトリ
  • Properties > Events > {Function名} > Properties > Path: エンドポイント
  • Properties > Events > {Function名} > Properties > Method: リクエストメソッド (POSTの場合は post になる)

実装

ここでは、get_sample > app.py でのレスポンス内容をhello world から get sampleに変更してみます

return {
    "statusCode": 200,
    "body": json.dumps({
        "message": "get sample",
    }),

ビルドから実行まで

hello worldの時と同様にビルドの実行と、ローカルをホストにします。

$ sam build                                                                                                                                                                                                                                                        [±main ●▴]
Building codeuri: /Users/xxxxxx/aws-sam-sample/sam-app/hello_world runtime: python3.9 metadata: {} architecture: x86_64 functions: ['HelloWorldFunction']
Running PythonPipBuilder:ResolveDependencies
Running PythonPipBuilder:CopySource
Building codeuri: /Users/xxxxxx/aws-sam-sample/sam-app/get_sample runtime: python3.9 metadata: {} architecture: x86_64 functions: ['GetSampleFunction']
Running PythonPipBuilder:ResolveDependencies
Running PythonPipBuilder:CopySource

Build Succeeded

Built Artifacts  : .aws-sam/build
Built Template   : .aws-sam/build/template.yaml

Commands you can use next
=========================
[*] Invoke Function: sam local invoke
[*] Deploy: sam deploy --guided
$ sam local start-api                                                                                                                                                                                                                                              [±main ●▴]

Mounting GetSampleFunction at http://127.0.0.1:3000/get-sample [GET]
Mounting HelloWorldFunction at http://127.0.0.1:3000/hello [GET]
You can now browse to the above endpoints to invoke your functions. You do not need to restart/reload SAM CLI while working on your functions, changes will be reflected instantly/automatically. You only need to restart SAM CLI if you update your AWS SAM template
2021-10-30 15:13:00  * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)

すると、

Mounting GetSampleFunction at http://127.0.0.1:3000/get-sample [GET]

が増えたと思います。
後は、追加されたエンドポイントに対してGETを実行するだけです。

$ curl http://127.0.0.1:3000/get-sample                                                                                   [±main ●▴]
{"message": "get sample"}

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です