I am trying to create rabbit my using following co...
# help
p
I am trying to create rabbit my using following construct.
Copy code
const rabbitMQ = new CfnBroker(this, "name', {...});
I want to get connection string out of created rabbitMQ. So I used following code.
Copy code
environment {
  RABBIT: rabbitMQ.attrAmqpEndpoints[0]
}
Even thought
attrAmpqEndpoints
is documented as
string[]
sst run stat
was throwing error saying I can access array using index directly and I can access it using Fn.select(index, list). So I changed code to
Copy code
import { Fn } from '@aws-cdk/core';
...
environment {
  RABBIT: Fn.select(0, rabbitMQ.attrAmqpEndpoints)
}
Above code worked, but, I don't understand why there is need to use
Fn
for accessing array element at index. While at typescript should be able to use
list[0]
right?
s
Morning Pavan, Fn.select is an intrinsic function. These functions need to be used, when properties are not available until runtime. This is because the array of endpoints only becomes available once cloudformation begins to evaluate your stack, inside of AWS. The typescript is evaluated locally on your machine, and at that time, the array of endpoints doesn't exist yet. So, Fn.select tells cloudformation to look for an element in the array, during deployment (once it's available). Hope that makes sense, let me know 👍
p
That makes sense. But, while creating queue I didn't had to use Fn function like the example below.
Copy code
this.addOutputs({
  queUrl: aQueue.sqsQueue.queueUrl,
  rabitEndpoint: Fn.select(0, rabbitMQ.attrAmqpEndpoints),
});
I assume even the queue URL is not available until runtime, but it could used directly like
aQueue.sqsQueue.queueUrl
but rabbitMQ array needs
Fn
. I am puzzled with this disparity.
s
Good question. Amazon sqs queue URLs are deterministic. Composed as: https://sqs.{region}.amazonaws.com/{accountID}/{queueName} So, its fair to say it is probably know ahead of time, but it might use a ref to the string under the hood
f
Hey @Simon Reilly, thanks for pointing out that the endpoint is resolved at deploy time.. didn’t notice that at first 👀
@Pavan Kumar, you are right, both the queue url and rabbit endpoints are not available at build time. And they both have placeholder value (what CDK calls TOKENs)
When the tokens hold string values, ie the queue url, you can refer to them directly.
When the tokens hold list values, ie the queue endpoints, and if you want to access an element in the list, you HAVE TO use intrinsic functions.
Think of it this way, at build time, u don’t know how many items there are in the array. You only know that at deploy time. That’s what intrinsic functions do.
p
Thanks you @Simon Reilly and @Frank for explaining.