a year ago

PythonでAmplify functionをつくる


叩いたコマンドとかトラブルシューティングの備忘録

API追加

 % amplify add api
? Please select from one of the below mentioned services: REST
? Provide a friendly name for your resource to be used as a label for this category in the project: <API名>
? Provide a path (e.g., /book/{isbn}): /tw
? Choose a Lambda source Create a new Lambda function
? Provide an AWS Lambda function name: <lambda関数名>
? Choose the runtime that you want to use: Python
python3 found but version Python 3.7.9 is less than the minimum required version.
You must have python >= 3.8 installed and available on your PATH as "python3" or "python". It can be installed from https://www.python.org/downloads
You must have virtualenv installed and available on your PATH as "venv". It can be installed by running "pip3 install venv".
Only one template found - using Hello World by default.

Available advanced settings:
- Resource access permissions
- Scheduled recurring invocation
- Lambda layers configuration
- Environment variables configuration
- Secret values configuration

? Do you want to configure advanced settings? No
? Do you want to edit the local lambda function now? No
Successfully added resource gettweet locally.

Next steps:
Check out sample function code generated in <project-dir>/amplify/backend/function/gettweet/src
"amplify function build" builds all of your functions currently in the project
"amplify mock function <functionName>" runs your function locally
"amplify push" builds all of your local backend resources and provisions them in the cloud
"amplify publish" builds all of your local backend and front-end resources (if you added hosting category) and provisions them in the cloud
Succesfully added the Lambda function locally
? Restrict API access No
? Do you want to add another path? No
Successfully added resource api7168af60 locally

Some next steps:
"amplify push" will build all your local backend resources and provision it in the cloud
"amplify publish" will build all your local backend and frontend resources (if you have hosting category added) and provision it in the cloud

lambda実装

import json
import datetime


def handler(event, context):
    print('received event:')
    print(event)
    current_time = datetime.datetime.now().time()
    body = {
      'message': 'Hello, the current time is ' + str(current_time)
    }
    response = {
      'statusCode': 200,
      'body': json.dumps(body),
      'headers': {
        'Content-Type': 'application/json',
        'Access-Control-Allow-Headers': '*',
        'Access-Control-Allow-Origin': '*',
        'Access-Control-Allow-Methods': 'OPTIONS,POST,GET'
      },
    }

    return response

layer追加

% amplify add function
? Select which capability you want to add: Lambda layer (shared code & resource used across functions)
? Provide a name for your Lambda layer: <layer名>
? Choose the runtime that you want to use: Python
? The current AWS account will always have access to this layer.
Optionally, configure who else can access this layer. (Hit <Enter> to skip) 
✅ Lambda layer folders & files created:
amplify/backend/function/nuxtamplifyicolablayer

Next steps:
Move your libraries to the following folder:
[Python]: amplify/backend/function/nuxtamplifyicolablayer/lib/python

Include any files you want to share across runtimes in this folder:
amplify/backend/function/nuxtamplifyicolablayer/opt

"amplify function update <function-name>" - configure a function with this Lambda layer
"amplify push" - builds all of your local backend resources and provisions them in the cloud

amplify pushができなくなったら

 % amplify push
✖ There was an error pulling the backend environment dev.
An error occurred during the push operation: ENOENT: no such file or directory, stat '/Users/xxx/workspace/amplify/sample-amplify/amplify/.temp/#current-cloud-backend/function/samplefunction/.venv/bin/python'

functionのコード内にシンボリックリンクを含めてしまうと、2度とpushもpullもできない詰み状態になります。エグいですね。

こうなってしまったらまずはS3を漁ります。

amplify-なんとか-deploymentみたいなバケットがいくつかあるはず。更新日時が最新のものを開きます。 

その中にある#current-cloud-backend.zipを見つけ出して、ダウンロード。

amplifyはpush/pullの前にこのzipを落としてきてローカルの資源と比較します。

zipを解凍して、上でエラーに表示されているファイルを探して削除します。今回の例だとfunction/samplefunction/.venvを消します。

削除したら再度zipして#current-cloud-backend.zipという名前でS3に戻します。

% amplify push
✔ Successfully pulled backend environment dev from the cloud.
...

治りました。


Related Articles