|
1 | 1 | # Template
|
| 2 | + |
| 3 | +This component is used behind the scenes to deploy your YAML templates. It is loaded by the serverless CLI and executed just as any other component. |
| 4 | +However, there's a lot you can do with this component programmatically. See the sections below for some examples. |
| 5 | + |
| 6 | +### Programmatic usage and custom environments |
| 7 | + |
| 8 | +To deploy to multiple environments you must utilize the programmatic API, via `serverless.js` file. |
| 9 | + |
| 10 | +`serverless.js` |
| 11 | + |
| 12 | +```js |
| 13 | +const { Component } = require('@serverless/core') |
| 14 | + |
| 15 | +class Deploy extends Component { |
| 16 | + async default(inputs = {}) { |
| 17 | + const { env } = inputs |
| 18 | + |
| 19 | + const template = await this.load('@serverless/template', env) |
| 20 | + return await template({ template: __dirname + '/serverless.yml' }) |
| 21 | + } |
| 22 | + |
| 23 | + async remove(inputs = {}) { |
| 24 | + const { env } = inputs |
| 25 | + |
| 26 | + const template = await this.load('@serverless/template', env) |
| 27 | + await template.remove(inputs) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +module.exports = Deploy |
| 32 | +``` |
| 33 | + |
| 34 | +`serverless.yml` |
| 35 | + |
| 36 | +```yml |
| 37 | +name: test |
| 38 | + |
| 39 | +lambda: |
| 40 | + component: '@serverless/function' |
| 41 | + inputs: |
| 42 | + name: my-function |
| 43 | + description: My Serverless Function |
| 44 | + memory: 128 |
| 45 | + timeout: 20 |
| 46 | + code: './code' |
| 47 | + hanlder": 'handler.handler' |
| 48 | + region: us-east-1 |
| 49 | + runtime: nodejs10.x |
| 50 | +``` |
| 51 | +
|
| 52 | +Invoking `sls --env=dev` will result in state files in `.serverless/` being prefixed with the value of your `env`: |
| 53 | +`Deploy.dev.json`, etc. That way you can deploy unlimited environments, add pre/post processing, load whatever `.env` you need, etc. |
| 54 | + |
| 55 | +### Running custom methods on a template |
| 56 | + |
| 57 | +A template itself does not contain any methods so custom methods are executed on the specific template aliases. |
| 58 | + |
| 59 | +`sls install --component lambda` will load the `lambda` alias from your template, instantiate the corresponding component, and execute the `install` method passing in the inputs you specify via CLI parameters. |
| 60 | +Inputs from the template itself are not passed as they can not be interpreted/resolved without running the entire template. |
| 61 | + |
| 62 | +You can pass as many `--component` parameters as you need. |
| 63 | + |
| 64 | +### Running custom methods programmatically |
| 65 | + |
| 66 | +`serverless.js` |
| 67 | + |
| 68 | +```js |
| 69 | +const { Component } = require('@serverless/core') |
| 70 | +
|
| 71 | +class Deploy extends Component { |
| 72 | + /* ...skipped default method for brevity ... */ |
| 73 | +
|
| 74 | + async install(inputs = {}) { |
| 75 | + const template = await this.load('@serverless/template') |
| 76 | + await template.install({ template: __dirname + '/serverless.yml', ...inputs }) |
| 77 | + } |
| 78 | +} |
| 79 | +
|
| 80 | +module.exports = Deploy |
| 81 | +``` |
| 82 | + |
| 83 | +When invoking methods on a `template` instance, you always need to pass in a path to the template file or a template object. |
| 84 | + |
| 85 | +Running `sls install --component lambda --debug` - this will load the template, find the `lambda` alias, and invoke the `install` method on the instance of the component. |
| 86 | + |
| 87 | +### Couldn't find what you were looking for? |
| 88 | + |
| 89 | +Visit our github and file an issue with as much info as possible about your problem. |
| 90 | +If you think you've found a bug - please do post a complete reproduction repo. It will help us and our contributors to help you much faster. |
0 commit comments