# Refer to variable within a variable

**URL:** <https://forum.serverless.com/t/refer-to-variable-within-a-variable/3602>\
**Category:** Serverless Framework\
**Created:** [January 25, 2018, 9:01am UTC](https://forum.serverless.com/t/refer-to-variable-within-a-variable/3602 "2018-01-25T09:01:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![AJFleming](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/ajfleming/32/940_2.png) [@AJFleming](https://forum.serverless.com/u/AJFleming)\
**Post date:** [January 25, 2018, 9:01am UTC](https://forum.serverless.com/t/refer-to-variable-within-a-variable/3602/1 "2018-01-25T09:01:52Z")

</div>

Hi -

I want to assign a variable in **custom** a value which is derived from other variables in **custom**.

Example of code I would like to write (note the variable syntax to avoid collisions with AWS variables):

```
provider:
  name: aws
  variableSyntax: "\\${{([~:a-zA-Z0-9._\\'\",\\-\\/\\(\\)]+?)}}"

config : ${{file(./config.${{opt:instance}}.yml)}}

custom: 
      client: ${{opt:client, self:config:client}}
      product: ${{opt:product, self:config:product}}
      region: ${{opt:region, self:config:region}}
      serviceName: ${{self:custom:client}}-${{self:custom:product}}

```

However, when I run **serverless print** it returns with no output. By process of elimination, I’ve tracked it down to the **serviceName** assignment, and I can make it work by replacing it with :

```
custom:
  serviceName: ${{opt:client, self:config:client}}-${{opt:product, self:config:product}}

```

That’s a bit ugly but I would have expected that I could use self:custom:client elsewhere in the serverless.yml file. However, if I use self:custom:client elsewhere I get:

```
 Trying to populate non string value into a string for variable ${{self:custom:client}}. Please make sure the value of the property is a string.

```

Is there any way to create a variable within the custom block which is initialised with another variable in the custom block? And any idea why the self:custom:client reference isn’t being dereferenced properly elsewhere?

Cheers,

Adam

---

<div class="post-metadata">

**Author:** ![AJFleming](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/ajfleming/32/940_2.png) [@AJFleming](https://forum.serverless.com/u/AJFleming)\
**Post date:** [January 25, 2018, 9:34am UTC](https://forum.serverless.com/t/refer-to-variable-within-a-variable/3602/2 "2018-01-25T09:34:44Z")

</div>

Update to the above - the line :

```
config: config : ${{file(./config.${{opt:instance}}.yml)}}

```

wasn’t working, so I’ve now replaced it with statements similar to throughout:

```
${{opt:client, file(./config.${{opt:instance}}.yml):client}}

```

Which is truly ugly and not going to be nice to maintain…

---

<div class="post-metadata">

**Author:** ![buggy](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/buggy/32/177_2.png) [@buggy](https://forum.serverless.com/u/buggy)\
**Post date:** [January 25, 2018, 10:06am UTC](https://forum.serverless.com/t/refer-to-variable-within-a-variable/3602/3 "2018-01-25T10:06:50Z")

</div>

I think you meant to use

```auto
serviceName: ${{self:custom.client}}-${{self:custom.product}}

```

instead of

```auto
serviceName: ${{self:custom:client}}-${{self:custom:product}}

```

---

<div class="post-metadata">

**Author:** ![AJFleming](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/ajfleming/32/940_2.png) [@AJFleming](https://forum.serverless.com/u/AJFleming)\
**Post date:** [January 25, 2018, 10:26am UTC](https://forum.serverless.com/t/refer-to-variable-within-a-variable/3602/4 "2018-01-25T10:26:41Z")

</div>

I might have just said a somewhat naughty word!

It took me at least 5 minutes of staring at your response before I saw what the difference was!

Thankyou!

1000 internet points to buggy.

---

<div class="post-metadata">

**Author:** ![dankochetov](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/dankochetov/32/3459_2.png) [@dankochetov](https://forum.serverless.com/u/dankochetov)\
**Post date:** [July 8, 2019, 11:28am UTC](https://forum.serverless.com/t/refer-to-variable-within-a-variable/3602/5 "2019-07-08T11:28:40Z")

</div>

For future readers struggling to find the actual difference - it’s using dot instead of colon (custom.x instead of custom:x).
