When I made a disny monitoring bot using lambda, I wanted to execute it regularly using lambda, so I decided to use aws lambda. At this time, it was necessary to incorporate line-sdk-bot into lambda, so I will describe the method.
First, create a docker-lambda directory.
mkdir docker-lambda
cd docker-lambda
The structure of the directory is as follows.
docker-lambda ├── Dockerfile ├── deploy.sh └── requirements.txt
Each file is configured as follows.
The Dockerfile is below.
Dockerfile
FROM lambci/lambda:build-python3.7
ENV AWS_DEFAULT_REGION ap-northeast-1
ADD . .
CMD pip install -r requirements.txt -t python/lib/python3.7/site-packages/ && \
zip -r linetest.zip ./python
In lambda, I wanted to put line-bot-sdk, pandas, numpy, requests, boto3, so I created requirements.txt as follows.
requirements.txt
line-bot-sdk
pandas
numpy
requests
boto3
Finally, the sh file is configured as follows for execution.
docker build -t lambda_test .
Regarding, once you have created an image, you can comment it out.
deploy.sh
#First time only container launch
docker build -t lambda_test .
#Execute container
docker run -v "$PWD":/var/task lambda_test
#Granting authority(Make it possible to read with lambda)
chmod -R 755 ./*
After creating each file, execute the following in the terminal.
terminal
sh deploy.sh
output
adding: python/lib/python3.7/site-packages/urllib3-1.26.2.dist-info/RECORD (deflated 62%)
adding: python/lib/python3.7/site-packages/urllib3-1.26.2.dist-info/WHEEL (deflated 14%)
adding: python/lib/python3.7/site-packages/urllib3-1.26.2.dist-info/top_level.txt (stored 0%)
When the execution is completed in this way, a folder called python and a zip file called linetest.zip are completed. Here linetest.zip is a zipped version of python. External modules can be used by incorporating the created zip file as a layer of lambda.
ls
Dockerfile lambda_function.py python
deploy.sh linetest.zip requirements.txt
Go to the aws console and select lambda → layer → create layer.
Name: Favorite layer name (so you can see what the layer is for later) Description: Write a description of what modules are included upload: <= 10MB → Upload zip file
10MB → Upload via s3 Runtime: Select python3.7 this time License: You don't have to write anything
Since the size of the zip file exceeds 10MB this time, upload the zip file via S3.
Service → s3 → Create bucket
Bucket name: Anything is fine Region: Match to lambda (this time unified in Tokyo) Packet settings: block all Packet versioning: Disable Default encryption: disable Advanced settings: unchanged
↓ Creating a bucket
After creating the bucket, select the created bucket. (This time I created a packet called pypy-test) Upload → Add file → Upload the zip file created earlier
If you select the uploaded zip file, you can see the object URL, so copy it.
Go back to lambda again and select the layer. Paste the object URL at the upload location.
lambda → function → add function
How to make: Create from scratch Function name: Appropriate Runtime: python3.7 ↓ Creating a function
Please note that s3 cannot be used unless the region is Tokyo. At first it should have been North Africa. .. ..
Select a function and select Add Layer. Select a custom layer, select the layer you created and click Add. (If you don't see the layer you created here, check that the runtime may not be there.)
Finally, set up API Gateway for testing. Select Trigger → API Gateway → Create API → HTTP API → Security: Open → Add
This time, check if numpy can be used for testing. Rewrite the lambda code as follows.
Select a test to make sure it runs successfully.
If you select API Gateway and jump from the API endpoint to the url, you can see that the result of sin (1) is returned as shown below.
From the next time onward, I will apply this to create a linebot.
-About local settings of lambda
-How to upload python module to lambda
Recommended Posts