Cloud Formation generated unique physical IDs

Hi Folks

Question regarding cloud formation resources names and lambda environment vars:

Currently, in order to pass AWS resources names to lambda code we give explicit names to resources and pass these names to the deployed lambda code using serverless-plugin-write-env-vars.
Our serverless.yml file looks like this:

plugins:
  - serverless-plugin-write-env-vars

custom:
  writeEnvVars:
      SERVERLESS_RESOURCE_PREFIX: ${opt:stage, self:provider.stage}-${self:service}
      MY_BUCKET: ${self:custom.writeEnvVars.SERVERLESS_RESOURCE_PREFIX}-my-bucket

resources:
  Resources:
    MyBucket:
      Type: AWS::S3::Bucket
        Properties:
          Bucket: ${self:custom.writeEnvVars.MY_BUCKET}
          ...

Is there an easy way to let cloud formation\serverless generate unique names for resources and pass these generated names to the lambda code?

Basically, I want to avoid having to specify the bucket name in the resources section so that our serverless.yml file would look like this:

plugins:
  - serverless-plugin-write-env-vars

custom:
   writeEnvVars:
      MY_BUCKET: { "Ref" : "MyBucket"}

resources:
  Resources:
    MyBucket:
      Type: AWS::S3::Bucket
        Properties:
          ...

This currently doesn’t work. I assume it’s because serverless-plugin-write-env-vars generates the .env file before deploying the stack to cloud formation so the resources names aren’t available at that stage.

Before jumping in and extending serverless-plugin-write-env-vars, has someone already solved this?

Thanks,
Ron

This is a super important feature for us and we’re working with AWS to resolve this. Basically it needs to be set through CF to really work well and we need to wait on AWS to implement this so we can integrate this. Once they will put this in place at some point we’ll integrate immediately. They know its an important feature. For now I would look into a simple way to fix it like Environment variables through dotenv or something: Serverless Framework Guide - AWS Lambda Guide - Packaging

thanks @flomotlik.

I followed the link you shared and i’m already using serverless-plugin-write-env-vars that relies on dotenv which is the recommended method to handle env vars in javascript.

the thing is that serverless-plugin-write-env-vars runs before serverless deploys the stack to CF so resource ids aren’t generated at that stage yet.

I think i’ll try to extend serverless-plugin-write-env-vars so it would run after deploying the stack but before uploading the lambda code. Hopefully i’d be able to query cloud formation for the generated ids at that stage and update the .env file before packaging and deploying the code to lambda.

Any thoughts or pointers regarding this solution approach?

The problem is deployment of resources and Lambda code happens in the same step as its all done through CF. Thats why we need to have CF implement something better if we really want this to work out well.

got it. in that case i’ll wait for a proper solution.

thanks.