https://pulumi.com logo
#aws
Title
# aws
a

alert-raincoat-81485

01/29/2021, 1:59 AM
Hello folks, i am running into issue of
Copy code
raise error_class(parsed_response, operation_name)
    botocore.errorfactory.LoadBalancerNotFoundException: An error occurred (LoadBalancerNotFound) when calling the DescribeLoadBalancers operation: Load balancers '[test-lb]' not found
    error: an unhandled error occurred: Program exited with non-zero exit code: 1
where i am running load balancer creating api and running boto call to get an ARN of that load balancer as
Copy code
load_balancer = aws.lb.LoadBalancer(  # type: ignore
    "Test-LoadBalancer",
    name=stacktname + "-lb",
    load_balancer_type="network",
    internal="true",
    subnets=subnets,
    enable_deletion_protection=True,
)

nlb_name = stackname + "-lb"
try:
    client = boto3.client("elbv2")
except botocore.exceptions.ClientError as error:
    raise error
nlb_response = client.describe_load_balancers(Names=[nlb_name])

loadbal_arn = nlb_response["LoadBalancers"][0]["LoadBalancerArn"]
Is there any way to handle this situation in pulumi or exception?
b

billowy-army-68599

01/29/2021, 2:23 AM
you're describe the loadbalancer
nlb_name
but you're setting the name of the created loadbalancer to
stacktname + "-lb"
is that correct?
ah yes, looks like it is
so, the most likely scenario is either: 1) that the boto client you've created might not be using the same creds as your Pulumi program 2 )or the boto call is running before your loadbalancer is created
for 1), i'd verify from your boto client it has the right creds with an
sts get-caller-identity
call
and for 2), run the
describe_load_balancers
inside an
apply
I'm on my phone right now, so if you need help with 2), @ me me in a couple hours and I can explain further
🙂 1
a

alert-raincoat-81485

01/29/2021, 2:26 AM
Yes the stackname is a var assigned as a part of environment var.
well yes, when i am running pulumi preview, or up. The boto runs first or parallel with loadbalancer API.
That’s the way it’s not able to find the load bal.
STS won’t be an issue because when i am running boto3 API on existing Load-balancer, i am getting expected result. So the problem here is, boto is not able to find the Load-balancer because the resource has not been created in the current stack yet.
I appreciate your response, whenever you get free.
b

billowy-army-68599

01/29/2021, 2:46 AM
so you definitely need to run the describe load balancer call in the apply, in that case
pulumi creates your load balancer and then when it's been done, it returns a bunch of information about the created LB in an
Output
. Output's are like a python coroutine, you need to wait until they're finished before you do the describe
âś… 1
a

alert-raincoat-81485

01/29/2021, 2:56 AM
Yes i initially tried to get “Load-Balancer.arn” as a output, but it doesn’t return as a string or expected output. All i need here is a arn string output for me to use in other resource.
I didn’t get your statement, “so you definitely need to run the describe load balancer call in the apply, in that case”
I agree on your point, “pulumi creates your load balancer and then when it’s been done, it returns a bunch of information about the created LB in an 
Output
. Output’s are like a python coroutine, you need to wait until they’re finished before you do the describe” And this is what i am looking for, if i can apply something like async or wait function in it.
b

billowy-army-68599

01/29/2021, 3:01 AM
it's not returning an arn at all? what are you trying to pass the arn to, another resource?
a

alert-raincoat-81485

01/29/2021, 3:07 AM
Yes i am trying to pass arn (str) to another resource which isn’t part of pulumi. Basically str arn is being appended to one of the json file.
The arn i am receiving from output is a object style but not coming as a string as mentioned in pulumi doc.
The
loadbalancer.arn
output is coming in form of
<pulumi.output.Output object at 0x1075c4430>
but not actual a string.
b

billowy-army-68599

01/29/2021, 3:23 AM
so arn is an
Output[str]
, so as I said before, you need to do that in an apply. You don't need to use boto3 at all. give me a few minutes
a

alert-raincoat-81485

01/29/2021, 3:24 AM
I am not sure how apply works here, if you can guide me over it. Sure take your time.
b

billowy-army-68599

01/29/2021, 3:28 AM
this is a very simple example
Copy code
def write_to_file(arn):
    f = open("arn.txt", "a")
    f.write(arn)
    f.close()


# anything that is run inside an apply runs only when the resource has finished
# creating and has returned the value you're applying, in this case, the arn
json = lb.arn.apply(lambda a: write_to_file(arn=a))
so you want to run the function
write_to_file
only after the actual resource has been created and the API has returned a value to you. That's what apply does
this video is for nodejs, but the Pulumi principles remain the same

https://www.youtube.com/watch?v=lybOxul2otMâ–ľ

btw it's very very common for people not to grok this, so it's not surprising
a

alert-raincoat-81485

01/29/2021, 3:35 AM
Oh that looks absolutely interesting, let me try this way if it works.
q

quiet-leather-94755

01/30/2021, 8:02 AM
I've found that a useful way to think about it is, your code is creating a "deployment plan".. So any "regular" Python code in the sequential flow of the program runs before deployment, and can really only provide input to the deployment steps, not act on output.
As @billowy-army-68599 said, when you have an Output you want to act on, like
loadbalancer.arn
, you have to pass your action as a lambda that Pulumi can run once the resource referenced by the Output has been deployed / updated.