Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have optional resources with Serverless framework on AWS

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?

like image 363
abierto Avatar asked Jan 09 '18 15:01

abierto


People also ask

How do I set environment variables in serverless?

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.

Which of the following AWS offerings are serverless services?

AWS Fargate is a serverless compute engine that works with Amazon Elastic Container Service (ECS) and Amazon Elastic Kubernetes Service (EKS).

When should you not choose serverless?

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.


2 Answers

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.

like image 93
Dan C. Avatar answered Sep 18 '22 13:09

Dan C.


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.

like image 36
abierto Avatar answered Sep 19 '22 13:09

abierto