Skip to content

Commit

Permalink
add Jsonnet native functions into Readme
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiwara committed Aug 28, 2024
1 parent 21bc50d commit 458bba4
Showing 1 changed file with 100 additions and 18 deletions.
118 changes: 100 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ jobs:
steps:
- checkout
- lambroll/install:
version: v1.0.1
version: v1.1.0
- run:
command: |
lambroll deploy
Expand Down Expand Up @@ -398,6 +398,11 @@ function.json is a definition for Lambda function. JSON structure is based from
}
}
```

The template functions is available in `{{ }}`.
- `env` function expands environment variables.
- `must_env` function expands environment variables. If the environment variable is not defined, lambroll will panic and abort.

#### Tags

When "Tags" key exists in function.json, lambroll set / remove tags to the lambda function at deploy.
Expand All @@ -415,6 +420,33 @@ When "Tags" key exists in function.json, lambroll set / remove tags to the lambd
When "Tags" key does not exist, lambroll doesn't manage tags.
If you hope to remove all tags, set `"Tags": {}` expressly.

### Jsonnet support for function configuration

lambroll also can read function.jsonnet as [Jsonnet](https://jsonnet.org/) format instead of plain JSON.

```jsonnet
{
FunctionName: 'hello',
Handler: 'index.handler',
MemorySize: std.extVar('memorySize'),
Role: 'arn:aws:iam::%s:role/lambda_role' % [ std.extVar('accountID') ],
Runtime: 'nodejs20.x',
}
```

```console
$ lambroll \
--function function.jsonnet \
--ext-str accountID=0123456789012 \
--ext-code memorySize="128 * 4" \
deploy
```

- `--ext-str` sets external string values for Jsonnet.
- `--ext-code` sets external code values for Jsonnet.

v1.1.0 and later, lambroll supports Jsonnet native functions. See below for details.

#### Expand SSM parameter values

At reading the file, lambroll evaluates `{{ ssm }}` syntax in JSON.
Expand All @@ -427,6 +459,19 @@ For example,

SSM parameter value of `/path/to/param` is expanded here.

For Jsonnet, the `ssm` function is available.

```jsonnet
local ssm = std.native('ssm');
{
Environment: {
Variables: {
FOO: ssm('/path/to/param'),
},
},
}
```

#### Expand environment variables

At reading the file, lambroll evaluates `{{ env }}` and `{{ must_env }}` syntax in JSON.
Expand Down Expand Up @@ -457,6 +502,21 @@ Environment variable `FOO` is expanded. When `FOO` is not defined, lambroll will
}
```

For Jsonnet, the `env` and `must_env` native functions are available.

```jsonnet
local env = std.native('env');
local must_env = std.native('must_env');
{
Environment: {
Variables: {
FOO: env('FOO', 'default for FOO'),
BAR: must_env('BAR'),
},
},
}
```

#### Environment variables from envfile

`lambroll --envfile .env1 .env2` reads files named .env1 and .env2 as environment files and export variables in these files.
Expand Down Expand Up @@ -491,7 +551,7 @@ data "aws_iam_role" "lambda" {
"Handler": "index.js",
"MemorySize": 128,
"Role": "{{ tfstate `data.aws_iam_role.lambda.arn` }}",
"Runtime": "nodejs12.x",
"Runtime": "nodejs20.x",
"Timeout": 5,
"TracingConfig": {
"Mode": "PassThrough"
Expand All @@ -508,6 +568,33 @@ data "aws_iam_role" "lambda" {
}
```

For Jsonnet, the `tfstate` native function is available.

```jsonnet
local tfstate = std.native('tfstate');
{
Description: 'hello function',
FunctionName: 'hello',
Handler: 'index.js',
MemorySize: 128,
Role: tfstate('data.aws_iam_role.lambda.arn'),
Runtime: 'nodejs20.x',
Timeout: 5,
TracingConfig: {
Mode: 'PassThrough',
},
VpcConfig: {
SubnetIds: [
tfstate('aws_subnet.lambda["az-a"].id'),
tfstate('aws_subnet.lambda["az-b"].id'),
],
SecurityGroupIds: [
tfstate('aws_security_group.internal["%s"].id' % must_env('WORLD')),
],
},
}
```

Likewise, if you have AWS resource definitions spread across multiple tfstate files, you can utilize `--prefixed-tfstate` option:

e.g.
Expand All @@ -530,28 +617,23 @@ which then exposes additional template functions available like:
}
```

### Jsonnet support for function configuration

lambroll also can read function.jsonnet as [Jsonnet](https://jsonnet.org/) format instead of plain JSON.
For Jsonnet, a `{prefix}_tfstate` native function is generated by the `--prefixed-tfstate` option.

```jsonnet
local first_tfstate = std.native('my_first_tfstate');
local second_tfstate = std.native('my_second_tfstate');
{
FunctionName: 'hello',
Handler: 'index.handler',
MemorySize: std.extVar('memorySize'),
Role: 'arn:aws:iam::%s:role/lambda_role' % [ std.extVar('accountID') ],
Runtime: 'nodejs20.x',
Description: 'hello function',
Environment: {
Variables: {
FIRST_VALUE: first_tfstate('data.aws_iam_role.lambda.arn'),
SECOND_VALUE: second_tfstate('data.aws_iam_role.lambda.arn'),
},
},
"rest of the parameters": "...",
}
```

```console
$ lambroll \
--function function.jsonnet \
--ext-str accountID=0123456789012 \
--ext-code memorySize="128 * 4" \
deploy
```

### .lambdaignore

lambroll will ignore files defined in `.lambdaignore` file at creating a zip archive.
Expand Down

0 comments on commit 458bba4

Please sign in to comment.