arabold / serverless-export-env

Serverless plugin to export environment variables into a .env file
MIT License
102 stars 34 forks source link

Collect and set resource values from actual Cloud Formation stack output #21

Closed andersquist closed 4 years ago

andersquist commented 4 years ago

I've made a branch where I'm proposing that resource output values are retrieved from the actual Cloud Formation stack and set if they exist with a fallback with existing behaviour. This would help with cases where one would get values like [object Object] in the resulting .env file.

The implementation might be a bit naive and fail due to things that I'm not aware of and I would gladly take any feedback and hopefully create a PR as well: https://github.com/andersquist/serverless-export-env/tree/feature/stackoutputs

arabold commented 4 years ago

Interesting! Thanks for the branch, I will certainly take a look. I'm not 100% clear about the use case though - but I also haven't worked with AWS Lambda in quite a while to be honest.

When running as a Lambda on AWS the stack output wouldn't be exposed as environment variables automatically, would it? You would have to explicitly define the environment variable in the Serverless config yaml and reference the stack output there (using Ref: or Fn::ImportValue). What exactly does this change do and wouldn't that work differently to how environment variables are resolved in an actual AWS Lambda deployment?

andersquist commented 4 years ago

To give you an use case let's say we have something like this in the Resource section of a serverless.yml:

resources:
  Resources:
   SNSSomethingNotificationTopic:
      Type: AWS::SNS::Topic

  Outputs:
    SNSSomethingNotificationTopicName:
      Value: !GetAtt SNSSomethingNotificationTopic.TopicName

Then the SNSSomethingNotificationTopicName would not get resolved to the actual value but instead a JSON object containing something this after the call to collectResourceOutputs:

{ 'Fn::GetAtt': [ 'SNSSomethingNotificationTopic', 'TopicName' ] }

Which when it gets written to the .env file becomes SNS_NOTIFICATION_TOPIC_NAME: [object Object]

It would seem that the computed value is not fetched when the stack is created or updated for this kind of use case so in short my proposal is to try and get the actual stacks output values from AWS and map them to the outputs before using any other values.

rgustafson-ie commented 4 years ago

I hit this same issue today for outputs using Fn::GetAtt on the Endpoint.Address and Endpoint.Port for a AWS::RDS::DBCluster resource. It's in the AWS::RDS::DBCluster docs, and is a general thing for all kinds of AWS Resources.

I would find this immediately useful. 👍

andersquist commented 4 years ago

I'll just go ahead and make a PR so we can take the discussion from there and see if this is something that is worth considering.

arabold commented 4 years ago

Thanks for the PR. I get the problem and the proposed solution now. I think this makes sense and considering the challenges of supporting Fn::GetAtt directly or other alternatives, I'm open to merge it in. I'll take a look again next week when I have some more availability.

arabold commented 4 years ago

Merged! Thanks again for your contribution.