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