# Generate local include file containing ARNs of my yml created resources

**URL:** <https://forum.serverless.com/t/generate-local-include-file-containing-arns-of-my-yml-created-resources/3084>\
**Category:** Serverless Framework\
**Tags:** aws\
**Created:** [November 13, 2017, 2:38am UTC](https://forum.serverless.com/t/generate-local-include-file-containing-arns-of-my-yml-created-resources/3084 "2017-11-13T02:38:21Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonsmirl](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/jonsmirl/32/776_2.png) [@jonsmirl](https://forum.serverless.com/u/jonsmirl)\
**Post date:** [November 13, 2017, 2:38am UTC](https://forum.serverless.com/t/generate-local-include-file-containing-arns-of-my-yml-created-resources/3084/1 "2017-11-13T02:38:21Z")

</div>

I’m creating several AWS resources with using cloudformation in severless.yml. I need to get the ARNs from these resources into a local file so that I can include them in the build of my client app. Is there some scheme to do this?

---

<div class="post-metadata">

**Author:** ![bill](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/bill/32/1072_2.png) [@bill](https://forum.serverless.com/u/bill)\
**Post date:** [November 13, 2017, 9:47am UTC](https://forum.serverless.com/t/generate-local-include-file-containing-arns-of-my-yml-created-resources/3084/2 "2017-11-13T09:47:39Z")

</div>

can you set outputs in serverless.yml, then you can easily get it by command `serverless info -v`?

For example, you can output the dynamodb ARN with below codes:

```
Resources:
  DynamoDbTable:
    Type: 'AWS::DynamoDB::Table'
    ....

  Outputs:
    DynamoDbTable:
      Value:
        "Ref": DynamoDbTable
    DynamoDbTableARN:
      Value:
        "Fn::GetAtt": [DynamoDbTable, Arn]
```

---

<div class="post-metadata">

**Author:** ![tommedema](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/tommedema/32/1257_2.png) [@tommedema](https://forum.serverless.com/u/tommedema)\
**Post date:** [November 13, 2017, 3:31pm UTC](https://forum.serverless.com/t/generate-local-include-file-containing-arns-of-my-yml-created-resources/3084/3 "2017-11-13T15:31:30Z")

</div>

I did this like so.

In serverless.yml

```
plugins:
  # allow for scripts to inject into serverless lifecycle events
  - serverless-scriptable-plugin

custom:
  scriptHooks:
    # compile the web app after cloudformation stack data is known
    after:aws:info:gatherData: scripts/compile-web.js

```

Where my cloudformation template has outputs like this:

```
  Outputs:
    AttachmentsBucketName:
      Description: name of the attachments s3 bucket
      Value:
        Ref: AttachmentsBucket

```

And scripts/compile-web.js:

```
/* global serverless, options */

serverless.cli.log('compile-web: compiling web package with cloudformation outputs')

const shell = require('shelljs')

const awsInfo = serverless.pluginManager.plugins.find(p => p.constructor.name === 'AwsInfo')

const getOutput = (key) => {
  const entry = awsInfo.gatheredData.outputs.find(o => o.OutputKey === key)
  return entry && entry.OutputValue
}

const region = options.region || serverless.service.provider.region
const apiRoot = getOutput('ServiceEndpoint')
const attachmentsBucketName = getOutput('AttachmentsBucketName')
const cognitoUserPoolId = getOutput('CognitoUserPoolId')
const cognitoUserPoolClientId = getOutput('CognitoUserPoolClientId')
const cognitoIdentityPoolId = getOutput('CognitoIdentityPoolId')
const webBucketName = getOutput('WebBucketName')
const webCloudfrontDistId = getOutput('WebCloudfrontDistId')
const webRedirectCloudfrontDistId = getOutput('WebRedirectCloudfrontDistId')

serverless.cli.log(`compile-web: gathered cloudformation outputs:\n
  region: ${region}\n
  apiRoot: ${apiRoot}\n
  attachmentsBucketName: ${attachmentsBucketName}\n
  cognitoUserPoolId: ${cognitoUserPoolId}\n
  cognitoUserPoolClientId: ${cognitoUserPoolClientId}\n
  cognitoIdentityPoolId: ${cognitoIdentityPoolId}\n
  webBucketName: ${webBucketName}\n
  webCloudfrontDistId: ${webCloudfrontDistId}\n
  webRedirectCloudfrontDistId: ${webRedirectCloudfrontDistId}`)

// invalidate redirect distribution only if it is present
let webRedirectInvalidationCommand = ''
if (webRedirectCloudfrontDistId) {
  webRedirectInvalidationCommand = `&& aws cloudfront create-invalidation --distribution-id ${webRedirectCloudfrontDistId} --paths '/*'`
}

if (options.web) {
  serverless.cli.log('compile-web: compiling and deploying for web as --web option is provided')

  if (shell.exec(
    `cd ${__dirname}/../src/web && \
    REACT_APP_AWS_REGION=${region} \
    REACT_APP_API_ROOT=${apiRoot} \
    REACT_APP_ATTACHMENTS_BUCKET=${attachmentsBucketName} \
    REACT_APP_COGNITO_USER_POOL_ID=${cognitoUserPoolId} \
    REACT_APP_COGNITO_USER_POOL_CLIENT_ID=${cognitoUserPoolClientId} \
    REACT_APP_COGNITO_IDENTITY_POOL_ID=${cognitoIdentityPoolId} \
    npm run build && \
    aws s3 sync build/ s3://${webBucketName} && \
    aws configure set preview.cloudfront true && \
    aws cloudfront create-invalidation --distribution-id ${webCloudfrontDistId} --paths '/*' \
    ${webRedirectInvalidationCommand}`).code !== 0
  ) {
    shell.echo('compile-web error: web build and deployment failed with exit code !== 0')
    shell.exit(1)
  }
} else {
  serverless.cli.log('compile-web: skipping web compilation as --web option is not provided')
}

```

Note that this is making use of create react app’s support for injecting environment variables with webpack.

Does this answer your question?

---

<div class="post-metadata">

**Author:** ![jonsmirl](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/jonsmirl/32/776_2.png) [@jonsmirl](https://forum.serverless.com/u/jonsmirl)\
**Post date:** [November 13, 2017, 4:12pm UTC](https://forum.serverless.com/t/generate-local-include-file-containing-arns-of-my-yml-created-resources/3084/4 "2017-11-13T16:12:37Z")

</div>

That is the conclusion I have come to also, you need to write plugin scripts to do it. Thanks for the example.

---

<div class="post-metadata">

**Author:** ![tommedema](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/tommedema/32/1257_2.png) [@tommedema](https://forum.serverless.com/u/tommedema)\
**Post date:** [November 13, 2017, 4:37pm UTC](https://forum.serverless.com/t/generate-local-include-file-containing-arns-of-my-yml-created-resources/3084/5 "2017-11-13T16:37:12Z")

</div>

No worries. Are you going to open source your plugin? Would be interesting to compare our approaches.

---

<div class="post-metadata">

**Author:** ![jonsmirl](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/jonsmirl/32/776_2.png) [@jonsmirl](https://forum.serverless.com/u/jonsmirl)\
**Post date:** [November 13, 2017, 4:51pm UTC](https://forum.serverless.com/t/generate-local-include-file-containing-arns-of-my-yml-created-resources/3084/6 "2017-11-13T16:51:06Z")

</div>

First I would need to write it. I just hit this problem yesterday.

---

<div class="post-metadata">

**Author:** ![DavidWells](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/davidwells/32/201_2.png) [@DavidWells](https://forum.serverless.com/u/DavidWells)\
**Post date:** [November 13, 2017, 5:38pm UTC](https://forum.serverless.com/t/generate-local-include-file-containing-arns-of-my-yml-created-resources/3084/7 "2017-11-13T17:38:46Z")

</div>

You can also checkout the `serverless-stack-output` plugin. It will write the cloudformation outputs to a file and you can run custom code against it (if you wish. eg postDeploy stuff)

> <https://github.com/sbstjn/serverless-analytics/blob/master/serverless.yml#L6>

Here is an example of postDeploy script writing all my endpoints out to a manifest file [https://github.com/serverless/forms-service/blob/master/backend/serverless.yml#L15-L17](https://github.com/serverless/forms-service/blob/master/backend/serverless.yml#L15-L17)

---

<div class="post-metadata">

**Author:** ![tommedema](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/tommedema/32/1257_2.png) [@tommedema](https://forum.serverless.com/u/tommedema)\
**Post date:** [November 13, 2017, 7:54pm UTC](https://forum.serverless.com/t/generate-local-include-file-containing-arns-of-my-yml-created-resources/3084/8 "2017-11-13T19:54:25Z")

</div>

@DavidWells that’s a good tip as well, though keep in mind that with this approach you won’t have access to the `serverless` or `options` objects. Of course you can try to parse `serverless.yml`, but that doesn’t give you all information (e.g. command line options, compiled references, or dynamic modifications made by other plugins).

Do you .gitignore the output file? Just curious. I guess it’d be better to do so, since it is technically not source code and you wouldn’t want to have to recommit after every deploy.

---

<div class="post-metadata">

**Author:** ![DavidWells](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/davidwells/32/201_2.png) [@DavidWells](https://forum.serverless.com/u/DavidWells)\
**Post date:** [November 13, 2017, 11:07pm UTC](https://forum.serverless.com/t/generate-local-include-file-containing-arns-of-my-yml-created-resources/3084/9 "2017-11-13T23:07:38Z")

</div>

Yeah I’m trying to figure out the best way for the using the manifest file I create in a CI/CD flow.

Committing the `manifest.json` to git is the simple solution. But that has it’s limitations for multiple stages etc.

The serverless-stack-outputs plugin doesn’t have the `serverless` object but it could =) [https://github.com/sbstjn/serverless-stack-output/blob/master/src/plugin.ts#L51](https://github.com/sbstjn/serverless-stack-output/blob/master/src/plugin.ts#L51). That might be nice as a secondary param. @sbstjn what do you think? (`require(splits.join('.'))[func](data, serverless)`)

Currently I am parsing the raw `serverless.yml` and that has the pitfalls you mention (no env vars and limited variable usage)

---

<div class="post-metadata">

**Author:** ![sbstjn](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/sbstjn/32/812_2.png) [@sbstjn](https://forum.serverless.com/u/sbstjn)\
**Post date:** [November 14, 2017, 4:34pm UTC](https://forum.serverless.com/t/generate-local-include-file-containing-arns-of-my-yml-created-resources/3084/10 "2017-11-14T16:34:12Z")

</div>

I just released [v0.2.2 of serverless-stack-output](https://github.com/sbstjn/serverless-stack-output) on GitHub which includes this feature ( hopefully 😉 ). Should be on NPM in a few minutes, please test the changes and let me know if this works for you!

The CLI options are available in the handler as well.
