DEV Community

Cover image for First steps with serverless Python: write to s3 and call other Lambdas
Nočnica Mellifera for Stackery

Posted on

First steps with serverless Python: write to s3 and call other Lambdas

cover image: By Ansgar Koreng / CC BY-SA 4.0, CC BY-SA 4.0

Many people writing about AWS Lambda view Node as the code-default. I’ve been guilty of this in my own articles, but it’s important to remember that Python is a ‘first-class citizen’ within AWS and is a great option for writing readable Lambda code. Take a look at these two starter examples of writing functionality in Python.

Saving to S3

In this case, we write to an S3 Bucket. Writing to S3 is much simpler from a Lambda than from a web service sitting outside of AWS. Since you can configure your Lambda to have access to the S3 bucket there’s no authentication hassle or extra work figuring out the right bucket.

import boto3
import os

# Create an S3 client
s3 = boto3.client('s3')
bucket_name = os.environ['BUCKET_NAME'] # Supplied by Function service-discovery wire

def handler(message, context):

 # Add a file to your Object Store
 response = s3.put_object(
     Bucket=bucket_name,
     Key='Object Name',
     Body='Sample Text',
     ACL='public-read'
 )
 return response
Enter fullscreen mode Exit fullscreen mode

This example does make use of an environment variable automatically created by the Stackery canvas. More on this below in ‘A word on Environment Variables’.

Calling one Lambda with another Lambda

You may need to trigger one Lambda from another. This shouldn’t come up in the simplest possible stacks but whenever you have 2 or more Lambdas one handler might need to call another. This bare-bones example uses the Boto AWS SDK library, os to examine environment variables, and json to correctly format the payload.

import boto3
import os
import json

# Create an Lambda client
lambda_client = boto3.client('lambda')
function_name = os.environ['FUNCTION_NAME'] # Supplied by Function service-discovery wire

def handler(message, context):
 params = {
   "source": "invokeFunction",
   "content": "SampleData"
 }

 # Invoke another Function
 response = lambda_client.invoke(
     FunctionName=function_name,
     Payload=json.dumps(params)
 )
 Return
Enter fullscreen mode Exit fullscreen mode

A word on environment variables

The environment variables mentioned here are automatically created by Stackery when connecting resources in the Stackery canvas. Stackery creates templates for your entire serverless stack that deploy seamlessly via AWS CloudFormation.

Alt Text

You can create your own environment variables right from the AWS Lambda Console.

Going further with Stackery

Stackery enables you to create re-usable templates for complex stacks of resources, and automatcially manages the permissions your Lambdas will need to let it access your other AWS resources.

If you want to see this and many other serverless superpowers enabled by Stackery, sign up for an account and try it out. Developer stacks are free to build and Manage with Stackery.

Top comments (0)