SQS Ref in serverless.yml

Hi,

I’m trying to use serverless to deploy a service which reads a JSON payload on an http endpoint and then passes it to an SQS queue. Here’s my serverless.yml:

provider:
  name: aws
  runtime: nodejs6.10
  environment:
    sqs_url: !Ref MessagesQueue

functions:
  process:
    handler: handler.handler
    events:
      - http: get handler

resources:
  Resources:
    MessagesQueue:
      Type: AWS::SQS::Queue

This gives me an error saying: unknown tag !<!Ref>

Basically, I’m trying to set the environment variable sqs_url to the newly created SQS queue’s url. Is this possible?

You can’t (yet) use the YAML CFN functions in Serverless - it’s all converted to JSON under the hood.

If you replace it with a straight “Ref:” object, it should work fine.

Hi,

Do you have a sample serverless.yml which does that?

Changing the first part of your snippet will fix the error you’re reporting:

provider:
  name: aws
  runtime: nodejs6.10
  environment:
    sqs_url:
      Ref: MessagesQueue
4 Likes

Awesome that worked! One more question:

Can I also limit the iamRoleStatements to this Queue, Right now I have this:

iamRoleStatements:
    - Effect: Allow
      Action:
        - sqs:*
      Resource: arn:aws:sqs:*:*:*

I tried changing that to the following but that doesn’t work.

iamRoleStatements:
    - Effect: Allow
      Action:
        - sqs:*
      Resource:
        Ref: MessageQueue

Yes, but the IAM Resource requires the Arn instead of the Url.
So use the following:

  iamRoleStatements:
    - Effect: Allow
      Action:
        - sqs:*
     Resource:
       Fn::GetAtt: [ MessageQueue, Arn ]

Or if you prefer, limit the role to only allow adding messages to the queue like so:

  iamRoleStatements:
    - Effect: Allow
      Action:
        - sqs:SendMessage
     Resource:
       Fn::GetAtt: [ MessageQueue, Arn ]

Hope it helps.

5 Likes

Awesome, it works. Thanks a lot!

1 Like

Same questions…
Thanks! You rock! :muscle:t6:

So I have just hit this snag. However in place of the SQS QueueUrl being return I get [object Object] as the environment variable. Not sure why

1 Like

This is why your env variable points to a reference not to a value. Maybe you are running your lambda on your local machine (as me).This is because there is not exist a defined stage in our local machines.
In my case I used this plugin in order to get the value in my environment value.
See this post which use serverless-cloudside-plugin in order to define a stage locally and then, get the real environment variable value.

  environment:
    QUEUE_URL:
      Ref: MyQueue

At the beginning QUEUE_URL had [Object object] string value, after using cloudside plugin I got the real value