github.com/Jeffail/benthos/v3@v3.65.0/website/docs/configuration/templating.md (about) 1 --- 2 title: Templating 3 description: Learn how Benthos templates work. 4 --- 5 6 <!-- 7 THIS FILE IS AUTOGENERATED! 8 9 To make changes please edit the contents of: 10 internal/template/docs.md.tmpl 11 --> 12 13 EXPERIMENTAL: Templates are an experimental feature and therefore subject to change outside of major version releases. 14 15 Templates are a way to define new Benthos components (similar to plugins) that are implemented by generating a Benthos config snippet from pre-defined parameter fields. This is useful when a common pattern of Benthos configuration is used but with varying parameters each time. 16 17 A template is defined in a YAML file that can be imported when Benthos runs using the flag `-t`: 18 19 ```sh 20 benthos -t "./templates/*.yaml" -c ./config.yaml 21 ``` 22 23 The template describes the type of the component and configuration fields that can be used to customize it, followed by a [Bloblang mapping][bloblang.about] that translates an object containing those fields into a benthos config structure. This allows you to use logic to generate more complex configurations: 24 25 import Tabs from '@theme/Tabs'; 26 27 <Tabs defaultValue="template" values={[ 28 { label: 'Template', value: 'template', }, 29 { label: 'Config', value: 'config', }, 30 { label: 'Result', value: 'result', }, 31 ]}> 32 33 import TabItem from '@theme/TabItem'; 34 35 <TabItem value="template"> 36 37 ```yml 38 name: aws_sqs_list 39 type: input 40 41 fields: 42 - name: urls 43 type: string 44 kind: list 45 - name: region 46 type: string 47 default: us-east-1 48 49 mapping: | 50 root.broker.inputs = this.urls.map_each(url -> { 51 "aws_sqs": { 52 "url": url, 53 "region": this.region, 54 } 55 }) 56 ``` 57 58 </TabItem> 59 <TabItem value="config"> 60 61 ```yml 62 input: 63 aws_sqs_list: 64 urls: 65 - https://sqs.us-east-2.amazonaws.com/123456789012/MyQueue1 66 - https://sqs.us-east-2.amazonaws.com/123456789012/MyQueue2 67 68 pipeline: 69 processors: 70 - bloblang: | 71 root.id = uuid_v4() 72 root.foo = this.inner.foo 73 root.body = this.outter 74 ``` 75 76 </TabItem> 77 <TabItem value="result"> 78 79 ```yaml 80 input: 81 broker: 82 inputs: 83 - aws_sqs: 84 url: https://sqs.us-east-2.amazonaws.com/123456789012/MyQueue1 85 region: us-east-1 86 - aws_sqs: 87 url: https://sqs.us-east-2.amazonaws.com/123456789012/MyQueue2 88 region: us-east-1 89 90 pipeline: 91 processors: 92 - bloblang: | 93 root.id = uuid_v4() 94 root.foo = this.inner.foo 95 root.body = this.outter 96 ``` 97 98 </TabItem> 99 100 </Tabs> 101 102 You can see more examples of templates, including some that are included as part of the standard Benthos distribution, at [https://github.com/Jeffail/benthos/tree/master/template](https://github.com/Jeffail/benthos/tree/master/template). 103 104 ## Fields 105 106 The schema of a template file is as follows: 107 108 ### `name` 109 110 The name of the component this template will create. 111 112 113 Type: `string` 114 115 ### `type` 116 117 The type of the component this template will create. 118 119 120 Type: `string` 121 Options: `cache`, `input`, `output`, `processor`, `rate_limit`. 122 123 ### `status` 124 125 The stability of the template describing the likelihood that the configuration spec of the template, or it's behaviour, will change. 126 127 128 Type: `string` 129 Default: `"stable"` 130 131 | Option | Summary | 132 |---|---| 133 | `stable` | This template is stable and will therefore not change in a breaking way outside of major version releases. | 134 | `beta` | This template is beta and will therefore not change in a breaking way unless a major problem is found. | 135 | `experimental` | This template is experimental and therefore subject to breaking changes outside of major version releases. | 136 137 138 ### `categories` 139 140 An optional list of tags, which are used for arbitrarily grouping components in documentation. 141 142 143 Type: list of `string` 144 Default: `[]` 145 146 ### `summary` 147 148 A short summary of the component. 149 150 151 Type: `string` 152 Default: `""` 153 154 ### `description` 155 156 A longer form description of the component and how to use it. 157 158 159 Type: `string` 160 Default: `""` 161 162 ### `fields` 163 164 The configuration fields of the template, fields specified here will be parsed from a Benthos config and will be accessible from the template mapping. 165 166 167 Type: list of `object` 168 169 ### `fields[].name` 170 171 The name of the field. 172 173 174 Type: `string` 175 176 ### `fields[].description` 177 178 A description of the field. 179 180 181 Type: `string` 182 Default: `""` 183 184 ### `fields[].type` 185 186 The scalar type of the field. 187 188 189 Type: `string` 190 Options: `string`, `int`, `float`, `bool`. 191 192 ### `fields[].kind` 193 194 The kind of the field. 195 196 197 Type: `string` 198 Default: `"scalar"` 199 Options: `scalar`, `map`, `list`. 200 201 ### `fields[].default` 202 203 An optional default value for the field. If a default value is not specified then a configuration without the field is considered incorrect. 204 205 206 Type: `unknown` 207 208 ### `fields[].advanced` 209 210 Whether this field is considered advanced. 211 212 213 Type: `bool` 214 Default: `false` 215 216 ### `mapping` 217 218 A [Bloblang](/docs/guides/bloblang/about) mapping that translates the fields of the template into a valid Benthos configuration for the target component type. 219 220 221 Type: `string` 222 223 ### `metrics_mapping` 224 225 An optional [Bloblang mapping](/docs/guides/bloblang/about) that allows you to rename or prevent certain metrics paths from being exported. 226 227 228 Type: `string` 229 Default: `""` 230 231 ```yml 232 # Examples 233 234 metrics_mapping: this.replace(".foo.count", ".count") 235 236 metrics_mapping: if ![ "count", "error", "latency" ].contains(this) { deleted() } 237 ``` 238 239 ### `tests` 240 241 Optional unit test definitions for the template that verify certain configurations produce valid configs. These tests are executed with the command `benthos template lint`. 242 243 244 Type: list of `object` 245 Default: `[]` 246 247 ### `tests[].name` 248 249 A name to identify the test. 250 251 252 Type: `string` 253 254 ### `tests[].config` 255 256 A configuration to run this test with, the config resulting from applying the template with this config will be linted. 257 258 259 Type: `object` 260 261 ### `tests[].expected` 262 263 An optional configuration describing the expected result of applying the template, when specified the result will be diffed and any mismatching fields will be reported as a test error. 264 265 266 Type: `object` 267 268 [bloblang.about]: /docs/guides/bloblang/about