Develop, run, and deploy AWS Lambda remotely using lambda-uploader

Overview

How to install and use tools that allow you to develop, run, and deploy lambdas on remote machines. Use python's lambda-uploader pip.

environment

python installation

See here python (pyenv + virtualenv) + CentOS7 installation memo

Install lambda-uploader

$ pip install lambda-uploader

Install because aws-cli is also used

$ pip install awscli

Initial setting

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

Prepare the necessary files

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 Lambda

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 Lambda function

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

reference

Deploy AWS Lambda Python with lambda-uploader

Recommended Posts

Develop, run, and deploy AWS Lambda remotely using lambda-uploader
Develop and deploy Python APIs using Kubernetes and Docker
Web scraping using AWS lambda
Summary if using AWS Lambda (Python)
Run Python on Schedule on AWS Lambda
Tweet WakaTime Summary using AWS Lambda
Using Lambda with AWS Amplify with Go
Run and see AWS Kinesis Firehose
I compared Node.js and Python in creating thumbnails using AWS Lambda
[Python] I wrote a REST API using AWS API Gateway and Lambda.
[AWS] Using ini files with Lambda [Python]
Regularly post to Twitter using AWS lambda!
[AWS] Link Lambda and S3 with boto3
[Python] Run Headless Chrome on AWS Lambda
Periodically run a python program on AWS Lambda
Differences between queryStringParameters and multiValueQueryStringParameters in AWS Lambda
Serverless scraping using selenium with [AWS Lambda] -Part 1-
Amazon API Gateway and AWS Lambda Python version