Skip to content

Commit e2e9c9b

Browse files
committed
Add some docs
1 parent 9990e55 commit e2e9c9b

File tree

1 file changed

+104
-2
lines changed

1 file changed

+104
-2
lines changed

README.md

Lines changed: 104 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,107 @@
11
# PicoFun
22

3-
'cos writing boilerplate is little fun
3+
*There's little fun in writing boilerplate*
44

5-
`#TODO: Write some docs`
5+
PicoFun 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.
6+
7+
**PicoFun only supports OpenAPI version 3 spec files.** Swagger files and versions of OpenAPI prior to 3 are not supported.
8+
9+
## Installation
10+
11+
PicoFun can be installed using `pip`, but it is recommended to use `pipx`. This is particularly useful in a CI/CD pipeline. Invoke PicoFun with `pipx` like this:
12+
13+
```bash
14+
pipx run picofun [ARGS]
15+
```
16+
17+
If you need to install `pipx`, please [refer to the documention](https://pypa.github.io/pipx/).
18+
19+
## Configuration
20+
21+
PicoFun is configured using a TOML file. The default configuration file is `picofun.toml` in the current working directory. An alternative location for the configuration file can be specified using the optional `--config` argument.
22+
23+
The configuration file has the following structure:
24+
25+
```toml
26+
bundle="/path/containing/code/to/bundle/into/build" # default is none
27+
layers="arn:aws:lambda:us-east-1:012345678910:layer:example:1,arn:aws:lambda:us-east-1:012345678910:layer:another-example:123" # default is none
28+
output_dir="/path/to/write/output-files" # default is current-working-directory/output
29+
postprocessor="fully.qualified.reference.to.postprocessor" # default is none
30+
preprocessor="fully.qualified.reference.to.preprocessor" # default is none
31+
subnets="subnet-1234567890abcdef0,subnet-234567890abcdef01" # default is none and VPC networking is no enabled
32+
tags=... # default is none
33+
template_path="/path/to/templates" # default is current-working-directory/templates
34+
```
35+
36+
## Usage
37+
38+
PicoFun is invoked using the `picofun` command. 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.
39+
40+
Here is a minimal example:
41+
42+
```bash
43+
44+
picofun example https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.0/api-with-examples.json
45+
46+
```
47+
48+
This will create a directory called `output` in the current working directory. The directory will contain the generated functions and the terraform module. The terraform module is in the root directory of `output/`. The generated functions are in the `lambdas` sub directory and the code for lambda layer is in the `layer` sub directory.
49+
50+
While the `config.toml` file 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:
51+
52+
```
53+
--config-file # Full path to the alternate configuration file
54+
--output-dir # Directory to output the generated files
55+
--layers # Comma separated list of Lambda layer ARNs to include in the function configuration
56+
--bundle # Path to code to bundle into a layer. If requirements.txt present pip install will be run.
57+
```
58+
59+
Here is an example of overriding the configuration file:
60+
61+
```bash
62+
63+
picofun --config ~/picofun-example.toml example example.json
64+
65+
```
66+
67+
Commonly 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:
68+
69+
```bash
70+
71+
picofun --layers "arn:aws:lambda:us-east-1:012345678912:layer:example:1,arn:aws:lambda:us-east-1:012345678912:layer:another-example:123" example example.yaml
72+
73+
```
74+
75+
## Bundle
76+
77+
PicoFun supports bundling code into a Lambda layer. The code to bundle is specified using the `bundle` entry in the configuration file or the `--bundle` argument on the command line. If a `requirements.txt` file is present in the bundle directory, `pip install` will be run by terraform before creating the layer.
78+
79+
The most common use case for using code bundles is to include pre and post processors.
80+
81+
## Preprocessing and Postprocessing Requests
82+
83+
Out 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.
84+
85+
An example implementation of these hooks can be found in the [`examples/processors`](examples/processors) directory. 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.
86+
87+
## Template Overrides
88+
89+
The 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.
90+
91+
If you need to override one PicoFun template, you need to copy both from the package. The templates are located in the `templates` directory in the PicoFun package.
92+
93+
You can add the path to the templates to the `config.toml` file using the `template_path` entry.
94+
95+
An example implementation is included in the [`examples/templates`](examples/templates) directory. 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.
96+
97+
## Terraform
98+
99+
PicoFun generates a terraform module to deploy the generated functions to AWS. The module is located in the root of your configured output directory. It `output`s the Lambda function ARNs and IAM role ARN.
100+
101+
The module can be included in your terraform project like this:
102+
103+
```hcl
104+
module "example_lambdas" {
105+
source = "/path/to/picofun/output"
106+
}
107+
```

0 commit comments

Comments
 (0)