# Custom Jinja functions

With Jinja you get to inject your own custom functions to be used in templating, so of course we do.

## The `variable` function
With the `variable` function you can fetch any variable value off any other layer in your repository.
### Example 1: fetching a `vpc_id` variable from the same layer in a different stack
```hcl
# stacks/ec2/stack.tfvars.jinja:
vpc_id = "{{ variable("vpc_id", stack="vpc") }}"
```
### Example 2: fetching a `vpc_id` variable from a different environment in a different stack
```hcl
# stacks/ec2/layers/development/layer.tfvars.jinja:
vpc_id = "{{ variable("vpc_id", stack="vpc", environment="production") }}"
```
### Example 3: full example
```hcl
# stacks/ec2/layers/development@us-east-1_foo/layer.tfvars.jinja:
vpc_id = "{{ variable("vpc_id", stack="vpc", environment="development", subenvironment="us-east-1", instance="foo") }}"  # stack/environment/subenvironment/instance all default to the caller's
```

## The `output` function
With the `output` function you can fetch any output value off the state of any other layer in your repository.
### Example 1: fetching a `vpc_id` output from the same layer in a different stack
```hcl
# stacks/ec2/stack.tfvars.jinja:
vpc_id = "{{ output("vpc_id", stack="vpc") }}"
```

## The `resource` function
With the `resource` function you can fetch any resource attributes off the state of any other layer in your repository.
### Example 1: fetching a `aws_vpc.main` resource from the same layer in a different stack
```hcl
# stacks/ec2/stack.tfvars.jinja:
vpc_id = "{{ resource("aws_vpc.main", stack="vpc")["id"] }}"
```

## The `md5`, `sha1`, `sha256` and `sha512` functions
With these functions you can generate the MD5, SHA-1, SHA-256 and SHA-512 checksums respectively of a given string.
### Example
```hcl
# stacks/ec2/stacks.tfvars.jinja
foo_md5 = "{{ md5("foo") }}"
```