Skip to content

Instantly share code, notes, and snippets.

@knowsuchagency
Last active November 18, 2019 21:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save knowsuchagency/7b5041fb99dc90929990fd5d402f1d42 to your computer and use it in GitHub Desktop.
Save knowsuchagency/7b5041fb99dc90929990fd5d402f1d42 to your computer and use it in GitHub Desktop.
boto3 lambda hello world
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)
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