abierto
abierto

Reputation: 1467

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?

Upvotes: 7

Views: 2180

Answers (2)

Dan C.
Dan C.

Reputation: 3683

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.

Upvotes: 9

abierto
abierto

Reputation: 1467

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.

Upvotes: 3

Related Questions