# Using data from a file as a string value in serverless.yml

**URL:** <https://forum.serverless.com/t/using-data-from-a-file-as-a-string-value-in-serverless-yml/10713>\
**Category:** Serverless Framework\
**Tags:** variables\
**Created:** [March 5, 2020, 6:36am UTC](https://forum.serverless.com/t/using-data-from-a-file-as-a-string-value-in-serverless-yml/10713 "2020-03-05T06:36:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jswift](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/jswift/32/4037_2.png) [@jswift](https://forum.serverless.com/u/jswift)\
**Post date:** [March 5, 2020, 6:36am UTC](https://forum.serverless.com/t/using-data-from-a-file-as-a-string-value-in-serverless-yml/10713/1 "2020-03-05T06:36:39Z")

</div>

Is it possible to import the contents of an external file as a value in serverless.yml. My usecase is for creating an AWS CloudWatch Dashboard, the `DashboardBody` element takes a string of JSON configuration. I could just write this into the yaml file, but it would be way nicer if I could do the following:

```
resources:
  DashboardIntegrationMonitor:
    Type: AWS::CloudWatch::Dashboard
    Properties:
      DashboardName: "${self:provider.stage}Importer"
      DashboardBody: "${file(./dashboard.json)}"

```

But serverless will read the json file and insert it as the content of DashboardBody:

```
"DashboardIntegrationMonitor": {
  "Type": "AWS::CloudWatch::Dashboard",
  "Properties": {
    "DashboardName": "stagingImporter",
    "DashboardBody": {
      "widgets": [
          ...
      ]

```

instead of

```
"DashboardIntegrationMonitor": {
  "Type": "AWS::CloudWatch::Dashboard",
  "Properties": {
    "DashboardName": "stagingImporter",
    "DashboardBody": "{\"widgets\": [...]}"

```

Is this possible?

---

<div class="post-metadata">

**Author:** ![heath](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/heath/32/4048_2.png) [@heath](https://forum.serverless.com/u/heath)\
**Post date:** [March 11, 2020, 9:22pm UTC](https://forum.serverless.com/t/using-data-from-a-file-as-a-string-value-in-serverless-yml/10713/2 "2020-03-11T21:22:56Z")

</div>

I solved this over on [stackoverflow](https://stackoverflow.com/a/60644596/9636):

You can reference an external `.js` file, and then `stringify` the `.json` file there:

`serverless.yml`:

```
...
resources:
  Resources:
    MyDashboard:
      Type: AWS::CloudWatch::Dashboard
      Properties:
      DashboardName: My-Dashboard
      DashboardBody: ${file(my-dashboard-body.js):myDashboardBody}

```

`${file(my-dashboard-body.js):myDashboardBody}` is a [serverless variable reference](https://serverless.com/framework/docs/providers/aws/guide/variables#reference-variables-in-javascript-files). It means that we want to use the value from the `myDashboardBody` module of the `my-dashboard-body.js` file.

`my-dashboard-body.js`:

```
module.exports.myDashboardBody = (serverless) => {
 const fsPromises = require('fs').promises
 return fsPromises.readFile('my-dashboard-body.json', 'utf-8')
};

```

`my-dashboard-body.json`:

```
{
  "widgets": [
    ...
  ]
}
```

---

<div class="post-metadata">

**Author:** ![jswift](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/jswift/32/4037_2.png) [@jswift](https://forum.serverless.com/u/jswift)\
**Post date:** [March 16, 2020, 12:54am UTC](https://forum.serverless.com/t/using-data-from-a-file-as-a-string-value-in-serverless-yml/10713/3 "2020-03-16T00:54:46Z")

</div>

That is perfect, thanks!

---

<div class="post-metadata">

**Author:** ![nbcch](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/nbcch/32/7422_2.png) [@nbcch](https://forum.serverless.com/u/nbcch)\
**Post date:** [April 2, 2024, 4:21pm UTC](https://forum.serverless.com/t/using-data-from-a-file-as-a-string-value-in-serverless-yml/10713/4 "2024-04-02T16:21:26Z")

</div>

When I use this, the `\n` in the CloudWatch query is resolved as `\\n` and CloudWatch dashboard has a hard time parsing it
