Question: what's the best way to have optional resources on a Serverless framework based Lambda?
I want to let Serverless cares about resources that the Lambda needs on lower environments (dev, test, staging), and have independent ones for higher environments, like production.
I was thinking about using something like
resources:
Resources: ${file(../${self:provider.stage}-resources.yml)}
my resources yml is like the following:
SQSQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: ${self:service}-${self:provider.stage}-queue
SNSTopic:
Type: AWS::SNS::Topic
Properties:
DisplayName: TEST SNS Topic
TopicName: ${self:service}-${self:provider.stage}-topic
SNSSubscription:
Type: AWS::SNS::Subscription
Properties:
Endpoint: [email protected]
Protocol: email
TopicArn: { "Fn::Join" : ["", ["arn:aws:sns:${self:provider.region}:", { "Ref" : "AWS::AccountId" }, ":${self:resources.Resources.SNSTopic.Properties.TopicName}" ] ] }
But it's not working. Any ideas on what's the best practice to achieve that?
To reference environment variables, use the ${env:SOME_VAR} syntax in your serverless. yml configuration file. It is valid to use the empty string in place of SOME_VAR . This looks like " ${env:} " and the result of declaring this in your serverless.
AWS Fargate is a serverless compute engine that works with Amazon Elastic Container Service (ECS) and Amazon Elastic Kubernetes Service (EKS).
Loss Of Control One of the biggest disadvantages of serverless is that you don't have the control over your services. We use a lot of services that are managed by third-party cloud providers, like Cloudwatch for logs and DynamoDB for databases.
The description in the question focuses on getting all resources in a single resource file, and use different such files per stage. This works, but has the limitation that you must put at least one resource in each stage. And also forces you to group together resources.
Another way to include optional resources that I used in serverless.yml
is as follows:
provider:
name: aws
stage: ${opt:stage, 'dev'}
resources:
- ${file(./sls-resources/mandatory-resource.yml)}
- ${file(./sls-resources/optional-resource.${self:provider.stage}.yml), ''}
Then, you only create optional-resource.prod.yml
to hold your production-only resource. When generating the template for dev stage, serverless resolves the optional reference to an empty element because the file does not exist, and then just ignores it.
Note I used the stage variable just as example, but it can be any other variable, e.g. region or a custom variable.
Replacing this:
TopicArn: { "Fn::Join" : ["", ["arn:aws:sns:${self:provider.region}:", { "Ref" : "AWS::AccountId" }, ":${self:resources.Resources.SNSTopic.Properties.TopicName}" ] ]
with this:
TopicArn: {"Ref" : "SNSTopic"}
Fixed my issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With