Skip to content

Commit

Permalink
caller_identity template function.
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiwara committed Aug 28, 2024
1 parent 19a0118 commit 1c03e1b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -530,9 +530,17 @@ local must_env = std.native('must_env');

#### Resolve AWS caller identity

Note: This function is available only in Jsonnet.
The `caller_identity` template function resolves the AWS caller identity.

The `caller_identity` native function resolves the AWS caller identity.
```json
{
"Account": "{{ caller_identity.Account }}",
"Arn": "{{ caller_identity.Arn }}",
"UserId": "{{ caller_identity.UserId }}"
}
```

The `caller_identity` native function also available in Jsonnet.

```jsonnet
local caller = std.native('caller_identity')();
Expand Down
12 changes: 12 additions & 0 deletions lambroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@ func (c *CallerIdentity) JsonnetNativeFuncs(ctx context.Context) []*jsonnet.Nati
}
}

func (c *CallerIdentity) FuncMap(ctx context.Context) template.FuncMap {
return template.FuncMap{
"caller_identity": func() map[string]any {
if err := c.resolve(ctx); err != nil {
return nil
}
return c.data
},
}
}

// App represents lambroll application
type App struct {
callerIdentity *CallerIdentity
Expand Down Expand Up @@ -250,6 +261,7 @@ func New(ctx context.Context, opt *Option) (*App, error) {
},
}
app.nativeFuncs = append(app.nativeFuncs, app.callerIdentity.JsonnetNativeFuncs(ctx)...)
app.loader.Funcs(app.callerIdentity.FuncMap(ctx))
return app, nil
}

Expand Down
2 changes: 1 addition & 1 deletion test/function.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"FunctionName": "{{ must_env `FUNCTION_NAME` }}",
"FileSystemConfigs": [
{
"Arn": "arn:aws:elasticfilesystem:ap-northeast-1:123456789012:access-point/fsap-04fc0858274e7dd9a",
"Arn": "arn:aws:elasticfilesystem:ap-northeast-1:{{ caller_identity.Account }}:access-point/fsap-04fc0858274e7dd9a",
"LocalMountPath": "/mnt/lambda"
}
],
Expand Down

0 comments on commit 1c03e1b

Please sign in to comment.