# SQS Ref in serverless.yml

**URL:** <https://forum.serverless.com/t/sqs-ref-in-serverless-yml/2739>\
**Category:** Serverless Framework\
**Created:** [September 20, 2017, 10:13pm UTC](https://forum.serverless.com/t/sqs-ref-in-serverless-yml/2739 "2017-09-20T22:13:10Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![zainzafar](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/zainzafar/32/1060_2.png) [@zainzafar](https://forum.serverless.com/u/zainzafar)\
**Post date:** [September 20, 2017, 10:13pm UTC](https://forum.serverless.com/t/sqs-ref-in-serverless-yml/2739/1 "2017-09-20T22:13:10Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![rowanu](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/rowanu/32/75_2.png) [@rowanu](https://forum.serverless.com/u/rowanu)\
**Post date:** [September 21, 2017, 4:34am UTC](https://forum.serverless.com/t/sqs-ref-in-serverless-yml/2739/2 "2017-09-21T04:34:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![zainzafar](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/zainzafar/32/1060_2.png) [@zainzafar](https://forum.serverless.com/u/zainzafar)\
**Post date:** [September 21, 2017, 4:46am UTC](https://forum.serverless.com/t/sqs-ref-in-serverless-yml/2739/3 "2017-09-21T04:46:17Z")

</div>

Hi,

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

---

<div class="post-metadata">

**Author:** ![rowanu](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/rowanu/32/75_2.png) [@rowanu](https://forum.serverless.com/u/rowanu)\
**Post date:** [September 21, 2017, 9:14am UTC](https://forum.serverless.com/t/sqs-ref-in-serverless-yml/2739/4 "2017-09-21T09:14:00Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![zainzafar](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/zainzafar/32/1060_2.png) [@zainzafar](https://forum.serverless.com/u/zainzafar)\
**Post date:** [September 21, 2017, 6:48pm UTC](https://forum.serverless.com/t/sqs-ref-in-serverless-yml/2739/5 "2017-09-21T18:48:53Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![bfieber](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/bfieber/32/653_2.png) [@bfieber](https://forum.serverless.com/u/bfieber)\
**Post date:** [September 21, 2017, 7:44pm UTC](https://forum.serverless.com/t/sqs-ref-in-serverless-yml/2739/6 "2017-09-21T19:44:50Z")

</div>

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

```auto
  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:

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

```

Hope it helps.

---

<div class="post-metadata">

**Author:** ![zainzafar](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/zainzafar/32/1060_2.png) [@zainzafar](https://forum.serverless.com/u/zainzafar)\
**Post date:** [September 21, 2017, 9:44pm UTC](https://forum.serverless.com/t/sqs-ref-in-serverless-yml/2739/7 "2017-09-21T21:44:56Z")

</div>

Awesome, it works. Thanks a lot!

---

<div class="post-metadata">

**Author:** ![colcodev](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/colcodev/32/2229_2.png) [@colcodev](https://forum.serverless.com/u/colcodev)\
**Post date:** [July 11, 2018, 11:53pm UTC](https://forum.serverless.com/t/sqs-ref-in-serverless-yml/2739/8 "2018-07-11T23:53:05Z")

</div>

Same questions…  
Thanks! You rock! 💪🏿

---

<div class="post-metadata">

**Author:** ![s-chand](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/s-chand/32/2743_2.png) [@s-chand](https://forum.serverless.com/u/s-chand)\
**Post date:** [November 14, 2018, 10:45am UTC](https://forum.serverless.com/t/sqs-ref-in-serverless-yml/2739/9 "2018-11-14T10:45:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![locoBit](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/locobit/32/3395_2.png) [@locoBit](https://forum.serverless.com/u/locoBit)\
**Post date:** [June 11, 2019, 9:40pm UTC](https://forum.serverless.com/t/sqs-ref-in-serverless-yml/2739/10 "2019-06-11T21:40:38Z")

</div>

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](https://www.jeremydaly.com/projects/serverless-cloudside-plugin/) which use [serverless-cloudside-plugin](https://www.npmjs.com/package/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

---

<div class="post-metadata">

**Author:** ![parifuture](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@parifuture](https://forum.serverless.com/u/parifuture)\
**Post date:** [June 9, 2020, 7:21pm UTC](https://forum.serverless.com/t/sqs-ref-in-serverless-yml/2739/11 "2020-06-09T19:21:23Z")

</div>

Adding a **proper working example** for anyone who lands on this page.

CREDIT TO: [https://dev.to/piczmar\_0/aws-lambda-sqs-events-with-serverless-framework-oj6](https://dev.to/piczmar_0/aws-lambda-sqs-events-with-serverless-framework-oj6)

```auto
service: sqs-triggers-demo

provider:
  name: aws
  runtime: nodejs12.x
  profile: sls
  region: us-west-2
  iamRoleStatements:
    - Effect: "Allow"
      Action:
        - "sqs:SendMessage"
        - "sqs:GetQueueUrl"
      Resource: "arn:aws:sqs:${self:provider.region}:811338114632:MyQueue"
    - Effect: "Allow"
      Action:
        - "sqs:ListQueues"
      Resource: "arn:aws:sqs:${self:provider.region}:811338114632:*"

functions:
  sender:
    handler: sender.handler
    events:
      - http:
          path: v1/sender
          method: post
  receiver:
    handler: receiver.handler
    events:
      - sqs:
          arn:
            Fn::GetAtt:
              - MyQueue
              - Arn

resources:
  Resources:
    MyQueue:
      Type: "AWS::SQS::Queue"
      Properties:
        QueueName: "MyQueue"

```

CREDIT TO: [https://dev.to/piczmar\_0/aws-lambda-sqs-events-with-serverless-framework-oj6](https://dev.to/piczmar_0/aws-lambda-sqs-events-with-serverless-framework-oj6)
