Last active
November 18, 2019 21:14
-
-
Save knowsuchagency/7b5041fb99dc90929990fd5d402f1d42 to your computer and use it in GitHub Desktop.
boto3 lambda hello world
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tempfile | |
import zipfile | |
from pathlib import Path | |
from pprint import pprint | |
import boto3 | |
def hello_lambda( | |
role, | |
function_name="hello-world", | |
region="us-east-1", | |
runtime="python3.7", | |
# the destination path to your module within the zip archive | |
path="hello_world.py", | |
# the path to the hello world module on your local machine | |
path_to_module="hello_world.py", | |
# assume module hello_world.py has a `main` function | |
handler="hello_world.main", | |
): | |
client = boto3.client("lambda", region_name=region) | |
with tempfile.NamedTemporaryFile() as fp: | |
zip_file = Path(fp.name) | |
with zipfile.ZipFile(fp, "w") as zf: | |
zf.write(path_to_module, arcname=path) | |
create_function_response = client.create_function( | |
FunctionName=function_name, | |
Runtime=runtime, | |
Role=role, | |
Handler=handler, | |
Code={"ZipFile": zip_file.read_bytes()}, | |
) | |
invoke_function_response = client.invoke( | |
FunctionName=create_function_response["FunctionArn"], LogType="Tail" | |
) | |
pprint(invoke_function_response) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def main(*args, **kwargs): | |
print("hello, world") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment