How to install and use tools that allow you to develop, run, and deploy lambdas on remote machines. Use python's lambda-uploader pip.
See here python (pyenv + virtualenv) + CentOS7 installation memo
$ pip install lambda-uploader
Install because aws-cli is also used
$ pip install awscli
Register the AWS authentication information with the following command.
$ aws configure --profile dev
AWS Access Key ID [None]: {Your Access Key Id}
AWS Secret Access Key [None]: {Your Secret Access Key}
Default region name [None]: ap-northeast-1
Default output format [None]: json
Create a file with the following directory configuration.
|- ~/lambda-uploader/
|- bin/
└ lambda-exec
|- sample-api/ #Any name can be used One directory for each lambda function
└ requirements.txt
└ lambda.json
└ index.py
└ event.json
It doesn't have to be this configuration,
These four files are always required for each function, and these four files must be in the same hierarchy.
~/lambda-uploader/bin/lambda-exec
A script to execute lambda in a remote environment. Since the aws command is long, create it with the following contents so that it can be easily executed.
#!/bin/bash
FUNCTION_NAME=$1
EVENT_JSON=$2
echo ${FUNCTION_NAME}
echo ${EVENT_JSON}
aws lambda invoke \
--invocation-type RequestResponse \
--function-name ${FUNCTION_NAME} \
--payload file://${EVENT_JSON} \
/tmp/lambda_outputfile.txt
echo ''
echo '[result]'
cat /tmp/lambda_outputfile.txt
echo ''
Grant execute permission
$ chmod +x ~/lambda-uploader/bin/lambda-exec
Add the following to bash_profile to pass PATH
$ vi ~/.bash_profile
export PATH=$PATH:"$HOME/lambda-uploader/bin"
Apply changes
$ source ~/.bash_profile
~/lambda-uploader/sample-api/requirements.txt
A file with a list of pip modules. Generate with the following command.
$ pip freeze > requirements.txt
~/lambda-uploader/sample-api/lambda.json
lambda configuration file. Describe the function name etc. here.
{
"name": "{Name of lambda function}",
"description": "{Description of lambda function}",
"region": "ap-northeast-1",
"handler": "index.lambda_handler",
"role": "arn:aws:iam::123456789012:role/role-name",
"timeout": 300,
"memory": 128
}
handler means that the lambda_handler function in index.py is executed in the above example.
For role, define arn of Execution Role of Lambda function. It is necessary to create a separate role in advance.
For reference, when I created Lambda that works with API Gateway, SQS, and SNS, I gave the following permissions to the IAM role.
~/lambda-uploader/sample-api/index.py
Create a Lambda function. Below is a sample that just receives and returns a POST request.
# -*- coding:utf-8 -*-
def lambda_handler(event, context):
text = '%s is %s years old.' %(event['name'], event['age'])
return text
~/lambda-uploader/sample-api/event.json
Defines the content of the POST request to be made during the test. (Empty is OK for testing GET requests)
{
"name" : "Michael",
"age" : "30"
}
Deploy your lambda function to AWS. Go to the directory where you have lambda.json, index.py, etc. and run the deploy command.
$ cd ~/lambda-uploader/sample-api
Deploy command
$ lambda-uploader
Execute the lambda function in the remote environment. It must have been deployed to AWS before it can run. Again, go to the directory where you have lambda.json, index.py, etc. and run it.
$ cd ~/lambda-uploader/sample-api
Execute with the following command Specify the Lambda function name in the first argument and the event.json file in the second argument.
$ lambda-exec {Lambda function name} event.json
If the following response is returned, it is successful.
sample-api
event.json
{
"StatusCode": 200
}
[result]
"Michael is 30 years old."
that's all
Deploy AWS Lambda Python with lambda-uploader
Recommended Posts