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’.
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
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.
You can create your own environment variables right from the AWS Lambda Console.
Stackery enables you to create re-usable templates for complex stacks of resources, and automatically 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.