First steps with serverless Python: write to s3 and call other Lambdas
Tags: python, serverless, aws, lambda
Published: 2019-09-27 17:42:49

---
title: First steps with serverless Python: write to s3 and call other Lambdas
published: true
description: 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.
cover_image: https://thepracticaldev.s3.amazonaws.com/i/of8oo9fiukc18u3bk9nw.jpg
tags: python, serverless, aws, lambda
---
*[cover image: By Ansgar Koreng / CC BY-SA 4.0, CC BY-SA 4.0]( https://commons.wikimedia.org/w/index.php?curid=58275146)*

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.

```python
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
```

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.

```python
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
```

# 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](https://thepracticaldev.s3.amazonaws.com/i/kzgl3hrsvkbw1jn609bq.jpg)

You can create your own environment variables right from the [AWS Lambda Console](https://docs.aws.amazon.com/lambda/latest/dg/applications-console.html).

# 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](https://app.stackery.io/sign-up) and try it out. Developer stacks are free to build and Manage with Stackery.