|
AWSメモ > AWS LambdaからLambda関数を作成する †AWS Lambda から Lambda を登録(作成)するメモ。 Python3.6 †AWS LambdaからLambda関数を作成する †from __future__ import print_function
import boto3
import zipfile
import base64
def lambda_handler(event, context):
# Lambda関数名
lambda_function = 'sampleFunc1'
# Lambdaランタイム 'nodejs'|'nodejs4.3'|'nodejs6.10'|'java8'|'python2.7'|'python3.6'|'dotnetcore1.0'|'nodejs4.3-edge'
lambda_runtime = 'python3.6'
# ロール名
lambda_role = 'arn:aws:iam::XXXXXXXXXXXX:role/XXXXXXX'
# Lambda関数ハンドラ (ファイル.関数名)
lambda_handler = 'index.main'
# タイムアウト(sec)
lambda_timeout = 300
#メモリサイズ(MB)
lambda_memory = 128
# 一時ファイル
tmp_src_file = '/tmp/index.py'
tmp_zip_file = '/tmp/index.zip'
# 登録するコード
code = 'from __future__ import print_function' + "\n" \
+ "\n" \
+ 'def main(event, context):' + "\n" \
+ " print('s3 function : START')" + "\n" \
+ " return 'Hello lambda function!'"
# pyファイル作成
with open(tmp_src_file, 'w') as f:
f.write(code)
f.close()
# zipファイル作成
with zipfile.ZipFile(tmp_zip_file, 'w', zipfile.ZIP_DEFLATED) as f:
f.write(tmp_src_file, 'index.py')
f.close()
# Lambda関数として登録
with open(tmp_zip_file, 'rb') as f:
zip_data = f.read()
f.close()
client = boto3.client('lambda')
response = client.create_function(
FunctionName=lambda_function,
Runtime=lambda_runtime,
Role=lambda_role,
Handler=lambda_handler,
Code={
'ZipFile': zip_data,
#'S3Bucket': s3_bucket_name,
#'S3Key': s3_object_key,
#'S3ObjectVersion': s3_object_version
},
Description='create function from lambda',
Timeout=lambda_timeout,
MemorySize=lambda_memory,
Publish=True
)
return 'Hello from s3PutFunction'
AWS LambdaからLambda関数コードを変更 †from __future__ import print_function
import boto3
import zipfile
import base64
def lambda_handler(event, context):
lambda_function = 'sampleFunc1'
tmp_src_file = '/tmp/index.py'
tmp_zip_file = '/tmp/index.zip'
# 変更するコード
code = 'from __future__ import print_function' + "\n" \
+ "\n" \
+ 'def main(event, context):' + "\n" \
+ " print('sample function : START')" + "\n" \
+ " print('updated code')" + "\n" \
+ " return 'Hello sample function'"
# pyファイル作成
with open(tmp_src_file, 'w') as f:
f.write(code)
f.close()
# zipファイル作成
with zipfile.ZipFile(tmp_zip_file, 'w', zipfile.ZIP_DEFLATED) as f:
f.write(tmp_src_file, 'index.py')
# Lambda関数を変更
with open(tmp_zip_file, 'rb') as f:
zip_data = f.read()
client = boto3.client('lambda')
response = client.update_function_code(
FunctionName=lambda_function,
ZipFile=zip_data,
#'S3Bucket': s3_bucket_name,
#'S3Key': s3_object_key,
#'S3ObjectVersion': s3_object_version
Publish=True
)
return 'Hello from s3PutFunction'
AWS LambdaからLambda関数を削除 †import boto3
def lambda_handler(event, context):
lambda_function = 'sampleFunc1'
client = boto3.client('lambda')
response = client.delete_function(
FunctionName=lambda_function,
#Qualifier='string'
)
return 'Hello from Lambda'
|