Package: sam-template-creator
SAM Template CreatorIntroThe SAM Template Creator helps you set up Infrastructure as Code for an AWS serverless project.
It reads your project folder and generates aSAM templatecontaining the necessary functions, globals, environment variables, etc.Compared to a full-fledged framework likeServerless, the scope of this template creator islimited. But this has advantages as well:
the tool is lightweight, generating no additional files except your IaC yaml, and is simple to use. Though it may not suffice for very complex
use-cases, it can still provide a starting point to expand upon.Requirementspython 3.5 or higherpipa serverless project to scan.Currently only Python and Go projects are supported. Support for Node and Java is planned.Usage Guide / How-toInstall the tool using pippip install sam-template-creator

# or alternatively

pip3 install sam-template-creatorNow either go to the root directory of your project and typesam-template-creator --location .

# or with shorthand argument names

sam-template-creator -l .Or use an absolute path from any directorysam-template-creator --location /path/to/projectThe script will create atemplate.yamlfile in the root of your project's directory. Check its contents! It might point out some things you have to fill in.You can also pass some (optional) arguments:language: by default, the tool will guess which language the project is written in. If it does not find the correct language, or if you want an older version of
the runtime (the script will default to the latest version), you can use this argument.globals: if used, the memory and timeout of the lambdas will be set globally, instead of per functionWith your template created, you should now be able to deploy to AWS usingaws cloudformation package --template-file template.yaml --s3-bucket YOUR-BUCKET-NAME --output-template-file outputSamTemplate.yaml
aws cloudformation deploy --template-file outputSamTemplate.yaml --stack-name PICK-A-STACK-NAME --capabilities CAPABILITY_IAMNotes on usageImportant!SAM Template Creator requires your project to be organised in a certain way.DirectoryThe tool will work best if every lambda has its own directory, though it should be able to handle other project structures as well.The file containing the handler and files it directly refers to will be checked for information on required permissions, environment variables, etc.If a single zip (for most languages) or executable is present in the folder of the handler file, or a subfolder, the tool assumes this zip contains the code you want to upload.Naming conventionsPythonthe name of the Lambda handler function should contain the word 'handler'. The event should end with 'event' and the context should be 'context'.
For exampledef lambda_handler(s3event, context)if part of the name equals a http method, we assume you want to map it to an api gateway method with the path represented by the rest of the name.
For example, if your handler's name isdef put_hello_world_hander(event, context), the function is mapped to aPUTto/hello/world.if the lambda is triggered by an event source, the name should reflect this.
For example, if s3 is the source, the name of the event should contains3:s3eventors3_eventor...Gothe name of your executable should behandler, except if you have an executable in the folder of your lambda, calledmain. If so, the tool will assume that this
executable has the code of your lambda. It will setHandlerandCodeUriaccordingly.For example, if your foldermylambdacontains amainfile underdist/main, theHandlerwill becomemain, with theCodeUriequal to/mylambda/dist/main.if you want to map a function to an api gateway method, the lambda handler should end with the word Request, with the path and method prepended to this word.
For example,func PostAddHelloRequest(_ context.Context, event events.APIGatewayProxyRequest) erroris mapped to aPOSTto/add/hello.if the lambda is triggered by an event source, the name should reflect that.
For example, if s3 is the source, the name of the event should contains3, like this:s3eventors3_eventor...NodeTODOJavaTODOProject StructureThere are three main parts to this projectreader: contains files that help with reading the files in the project. TheFileInfo.pyclass reads an individual file and retrieves
resources and other configuration information. Because files will look very different depending on the language, it uses the strategy pattern
to aid in these language-specific tasks. For example, when dealing with Python, thePythonStrategyclass is used.middleware: these files and functions take the information from the read side and do transforms, adding/removing certain config, before this is
passed to the writers.writer: these files are responsible for writing the information to yaml.Besides these folders, there is autilfolder, thecoordinator.pyfile which coordinates the work of the other files and thecommand_line.py,
which contains the argument parser and calls the coordinator after checking the input.TestsUnit tests can be run withpython -m unittest. A relatively simple it-test is run with the bash scripttest.shunder tests/it-test.
It requires a bucket as argument (for uploading the lambda zip) anddefault AWS credentials.RoadmapLanguagesNodeJavaIncremental templates by checking previous ones for values for timeout, env vars etc. (and maybe notifying user of finds)Robust error handlingAsk questions. See you call dynamo, add to template? generate outputs? how many buckets for events? deploy template? use 'middleware' for thisMore flexibility in location of lambdas: searching more folders, or ability to specify folder structure?Polyglot projects?