Package: picofun
PicoFunThere's little fun in writing boilerplatePicoFun is a tool for generating Python based clients for OpenAPI spec files. The client for each endpoint is packaged as an AWS Lambda function. A terraform module is also generated to deploy the clients to AWS. The generated functions are designed to be be invoked using Step Functions or the Lambda Invoke API.PicoFun only supports OpenAPI version 3 spec files.Swagger files and versions of OpenAPI prior to 3 are not supported.InstallationPicoFun can be installed usingpip, but it is recommended to usepipx. This is particularly useful in a CI/CD pipeline. Invoke PicoFun withpipxlike this:pipxrunpicofun[ARGS]If you need to installpipx, pleaserefer to the documention.ConfigurationPicoFun is configured using a TOML file. The default configuration file ispicofun.tomlin the current working directory. An alternative location for the configuration file can be specified using the optional--configargument.The configuration file has the following structure:bundle="/path/containing/code/to/bundle/into/build"# default is nonelayers="arn:aws:lambda:us-east-1:012345678910:layer:example:1,arn:aws:lambda:us-east-1:012345678910:layer:another-example:123"# default is noneoutput_dir="/path/to/write/output-files"# default is current-working-directory/outputpostprocessor="fully.qualified.reference.to.postprocessor"# default is nonepreprocessor="fully.qualified.reference.to.preprocessor"# default is nonesubnets="subnet-1234567890abcdef0,subnet-234567890abcdef01"# default is none and VPC networking is no enabledtags=...# default is nonetemplate_path="/path/to/templates"# default is current-working-directory/templatesUsagePicoFun is invoked using thepicofuncommand. The minimum arguments required to invoke PicoFun are the project namespace and the OpenAPI spec file. The project namespace is used to generate the names of the generated functions and the terraform module. The OpenAPI spec file is used to generate the clients.Here is a minimal example:picofunexamplehttps://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.0/api-with-examples.jsonThis will create a directory calledoutputin the current working directory. The directory will contain the generated functions and the terraform module. The terraform module is in the root directory ofoutput/. The generated functions are in thelambdassub directory and the code for lambda layer is in thelayersub directory.While theconfig.tomlfile is the preferred way to manage the configuration for the project, there are times when it is useful to override the configuration file. The following arguments can be used to override the configuration file:--config-file  # Full path to the alternate configuration file
  --output-dir   # Directory to output the generated files
  --layers       # Comma separated list of Lambda layer ARNs to include in the function configuration
  --bundle       # Path to code to bundle into a layer. If requirements.txt present pip install will be run.Here is an example of overriding the configuration file:picofun--config~/picofun-example.tomlexampleexample.jsonCommonly the layers argument is used to provide different layer ARNs based on the target environment, region and AWS account. Here is an example of overriding the layers argument:picofun--layers"arn:aws:lambda:us-east-1:012345678912:layer:example:1,arn:aws:lambda:us-east-1:012345678912:layer:another-example:123"exampleexample.yamlBundlePicoFun supports bundling code into a Lambda layer. The code to bundle is specified using thebundleentry in the configuration file or the--bundleargument on the command line. If arequirements.txtfile is present in the bundle directory,pip installwill be run by terraform before creating the layer.The most common use case for using code bundles is to include pre and post processors.Preprocessing and Postprocessing RequestsOut of the box PicoFun generates Lambda functions that make unauthenicated calls to endpoints. Often this isn't what teams need. The preprocessing and postprocessing hooks allow engineers to customize the request payload and response. A common use case for this is to add authentication headers to requests.An example implementation of these hooks can be found in theexamples/processorsdirectory. The example pulls values from SSM Parameter store and adds them as authentication headers for the request. The postprocessor logs the request URL and response status code.Template OverridesThe default templates bundled with PicoFun are usually adequate for most use cases. There are times where more customisation is needed. This could be to include custom logic in the Lambda function or additional resources in the terraform module.If you need to override one PicoFun template, you need to copy both from the package. The templates are located in thetemplatesdirectory in the PicoFun package.You can add the path to the templates to theconfig.tomlfile using thetemplate_pathentry.An example implementation is included in theexamples/templatesdirectory. The example removes all logging and tracing support from the Lambda functions. This isn't recommended for real projects, but it provides a useful example of the feature.TerraformPicoFun generates a terraform module to deploy the generated functions to AWS. The module is located in the root of your configured output directory. Itoutputs the Lambda function ARNs and IAM role ARN.The module can be included in your terraform project like this:module"example_lambdas"{source="/path/to/picofun/output"}