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?
rowanu
September 21, 2017, 4:34am
2
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?
rowanu
September 21, 2017, 9:14am
4
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
3 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
bfieber
September 21, 2017, 7:44pm
6
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.
6 Likes
Awesome, it works. Thanks a lot!
1 Like
Same questions…
Thanks! You rock!
s-chand
November 14, 2018, 10:45am
9
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
1 Like
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
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