Python library for generating AWS SAM (Serverless Application Model) templates with validations.
- Render templates with YAML or JSON
- Validations are done with Valley
- Python 3.6+
pip install sammy
from sammy import SAM, Function, API, SimpleTable
f = Function('testpublish',Handler='s3imageresize.handler',
Runtime='python3.6',
CodeUri='s3://your-bucket/photoresizer.zip')
ddb = SimpleTable('maintable',PrimaryKey={'Name':'_id','Type':'String'})
s = SAM(render_type='yaml')
s.add_resource(f)
s.add_resource(ddb)
print(s.to_yaml())
SAM is the class that generates the SAM template.
from sammy import SAM, SimpleTable
s = SAM(resources=[SimpleTable('maintable',PrimaryKey={'Name':'_id','Type':'String'})],
render_type='json')
- resources - List of resource classes (API, SimpleTable, or Function)
- render_type - This is a string and there are only two options JSON or YAML.
Add a resource class to the template
from sammy import Function, SAM
s = SAM(render_type='json')
f = Function('testpublish',Handler='s3imageresize.handler',
Runtime='python3.6',
CodeUri='s3://your-bucket/photoresizer.zip')
s.add_resource(f)
Add a parameter class to the template
import sammy as sm
s = sm.SAM(render_type='json')
s.add_parameter(sm.Parameter(name='Bucket',Type='String'))
Returns Python dict object representation of the template.
Returns a JSON representation of the template.
Returns a YAML representation of the template.
Returns a YAML or JSON representation of the template depending on what you set the render_type to on initialization.
Publishes the SAM template to S3
Publishes the SAM template to Cloudformation
This class represents an AWS Lambda function
from sammy import Function
f = Function('testpublish',Handler='s3imageresize.handler',
Runtime='python3.6',
CodeUri='s3://your-bucket/photoresizer.zip')
This class represents an AWS API Gateway
from sammy import API
a = API(StageName='dev',DefinitionUri='s3://your-bucket/your-swagger.yml',
CacheClusterEnabled=False,CacheClusterSize=None,Variables={'SOME_VAR':'test'})
This class represents a simple DynamoDB table
from sammy import SimpleTable
ddb = SimpleTable('maintable',PrimaryKey={'Name':'_id','Type':'String'})
This class represents a reference
import sammy as sm
sam = sm.SAM(Description='A hello world application.',render_type='yaml')
sam.add_parameter(sm.Parameter(name='Bucket',Type='String'))
sam.add_parameter(sm.Parameter(name='CodeZipKey',Type='String'))
sam.add_resource(
sm.Function(name='HelloWorldFunction',
Handler='sample.handler', Runtime='python3.6', CodeUri=sm.S3URI(
Bucket=sm.Ref(Ref='Bucket'),Key=sm.Ref(Ref='CodeZipKey'))))