https://pulumi.com logo
#automation-api
Title
# automation-api
w

wonderful-piano-20316

07/10/2021, 10:14 PM
Hi, 👋 I'm fairly new to Pulumi and I would like to receive guidance about the Automation API used inside a Typescript-based REST API. I would like to give my users the ability to create an empty Buckey in Google Cloud simply by sending a POST request. The desired bucket would be stored inside a stack named after the user's name. The issue I'm facing comes only once the user asks for another bucket in the same stack create beforehand. In this scenario, the first bucket would simply be deleted and the second one would replace the first one. This behavior is probably caused by Pulumi not being able to locate the code related to the first bucket during the second bucket creation request. I've based my work on the Automation REST API example, but they don't exactly fit my scenario, unfortunately. Is there anything I can do to avoid the destruction of the previously requested resource while deploying with the UP method? Thank you for your concern. 🙂
b

bored-oyster-3147

07/11/2021, 6:29 PM
It sounds like you’re not storing the state somewhere that it can be retrieved on subsequent http requests. What Pulumi backend are you using?
w

wonderful-piano-20316

07/11/2021, 9:33 PM
I'm storing my backend in a local folder for testing using the Pulumi login file:// command. I've also called the LocalWorkspace.createOrSelectStack method to retrieve it.
b

bored-oyster-3147

07/12/2021, 1:34 PM
How are you setting the backend in automation API? environment variable? project settings?
w

wonderful-piano-20316

07/12/2021, 6:40 PM
My backend is stored in a local file in a dedicated folder, and my users are making requests to access a stack name after them. Each time I receive an API call, I ask Pulumi to create or select a stack depending if there is one prior to the request.
b

bored-oyster-3147

07/12/2021, 7:27 PM
Yes I get that much - but how are you telling the automation api where that backend is located?
w

wonderful-piano-20316

07/13/2021, 1:02 PM
I tried using the PULUMI_BACKEND_URL environment variable to specify the path to my local backend directory. Every time I do so, I receive an error message asking me to set the PULUMI_ACCESS_TOKEN even if I'm using a local backend.
b

bored-oyster-3147

07/13/2021, 1:37 PM
yea the only way I have gotten it to work is by setting the backend url in the project settings file. https://www.pulumi.com/docs/reference/pulumi-yaml/ look at the
backend
option
w

wonderful-piano-20316

07/13/2021, 6:40 PM
Thank you, this option did the trick to fix the backend, but I'm still facing the same issue with my API unfortunately.
b

bored-oyster-3147

07/13/2021, 7:32 PM
meaning everytime it is destroying and recreating the bucket?
w

wonderful-piano-20316

07/13/2021, 8:10 PM
Yes ... I found a dirt workaround by setting the protected value each time a new resource is created. The drawback of this technique is that I need to remove it before proceeding to any deletion. I didn't found any information about the possibility to unset this value programmatically, however.
b

bored-oyster-3147

07/13/2021, 8:12 PM
If it is recreating the bucket everytime than there are a couple possibilities - either the state isn't working and it is not recognizing that the stack/bucket already exists, or the
s3.Bucket
is being given a different name each time your pulumi program executes
can you share any code from your pulumi program?
w

wonderful-piano-20316

07/14/2021, 12:00 PM
Sure! Here's the simplest example of the code I'm trying to run: https://codesandbox.io/s/musing-butterfly-1cq86?file=/src/index.ts
b

bored-oyster-3147

07/14/2021, 12:49 PM
gotcha. so the issue is the second scenario I mentioned. You are potentially giving the Bucket a different name everytime that end point is hit. With this line:
Copy code
const bucket = new gcp.storage.Bucket(content);
look here: https://www.pulumi.com/docs/reference/pkg/gcp/storage/bucket/#create The first argument to create any pulumi resource is the pulumi name. This name is how pulumi tracks resources between calls to
pulumi up
. If you want pulumi to track and update a resource, than the name must be the same on every call to
pulumi up
otherwise pulumi will see it as a different resource and destroy/create
Well pause, sorry. I guess you might be sending the username as
content
? So you're making a request with the same
stackName
and
bucketName
everytime and still seeing create/destroy? If that's the case than scrap what I just said. I will say that I don't see you setting the
backend
property in the sample code as we discussed, that could definitely be an issue. It should be set somewhere in here:
Copy code
const stack = await LocalWorkspace.createOrSelectStack({
      stackName,
      projectName,
      program: createPulumiProgram(content)
    });