https://pulumi.com logo
#general
Title
# general
m

many-house-85254

10/30/2023, 9:12 PM
A random string is added at the end of my google cloud function name eg: "foo-38d9875". Do you know how to avoid that i.e. the cloud function should be called exactly as the name it is given "foo"? When making a cloud function on the console, this does not happen. Happens through Pulumi. Thanks.
l

little-cartoon-10569

10/30/2023, 9:15 PM
Set the "name" property of the resource. When Pulumi names something, it adds that random string to avoid collisions. If you can manage that yourself, then you can set the name property.
m

many-house-85254

10/30/2023, 9:16 PM
I set the name "foo" myself.
l

little-cartoon-10569

10/30/2023, 9:17 PM
Do you mean in the 1st parameter of the constructor? That's not the property of the resource. The property is in the 2nd parameter.
So you need something like
Copy code
new Function("foo", { name: "foo"});
Or whatever syntax applies to your chosen language.
m

many-house-85254

10/30/2023, 9:21 PM
It does not compile,
TypeError: Function._internal_init() got multiple values for argument 'opts'.
l

little-cartoon-10569

10/30/2023, 9:23 PM
I don't know what language you're working in. I provided pseudocode, you'll need to translate.
m

many-house-85254

10/30/2023, 9:25 PM
Code to re-create example:
Copy code
from pulumi_gcp.cloudfunctions import Function

Function(name, {"name": name},  
runtime=runtime,  
region=region,  
source_archive_bucket=source_bucket.name,  
source_archive_object=bucket_archive.name,  
entry_point="foo",  
trigger_http=True,  
opts=opts  )  
invoker = FunctionIamMember(f"{name}-invoker",  
project=function.project,  
region=function.region,  
cloud_function=function.name,  
role=role,  
member="allUsers")  
export(f'lambda-{name}-url', function.https_trigger_url)
l

little-cartoon-10569

10/30/2023, 9:26 PM
Ok, you need to remove
{"name": name},
as the 2nd parameter, and add
name=name,
to the correct existing 2nd parametter.
Everything except the 1st parameter (logical name) and last parameter (opts) are the properties. You need to add the name property.
m

many-house-85254

10/30/2023, 9:27 PM
Indeed that works. Thanks for the quick help.
l

little-cartoon-10569

10/30/2023, 9:28 PM
Don't do it unless you really need to. It always breaks something. The random naming is there for a very good reason; unless you have a better one (e.g. you need a well-known name for a Function so that other things can call it), then you shouldn't change the name of a resource.
m

many-house-85254

10/30/2023, 9:30 PM
On GCP, apigw needs a config file in swagger that points to the cloud function url. The swagger file is static so I need to the url to be deterministic. On AWS, this is much cleaner, I can simply pass
lambda.url
and not worry about any of this mess.
l

little-cartoon-10569

10/30/2023, 9:30 PM
You can do that in Pulumi too. The ARN, URL, generated name, etc. are all available to other resources.
As you get more familiar with the ways to get the values where you need to, this will become easier
m

many-house-85254

10/30/2023, 9:31 PM
I generate the config file at
pulumi update
. The challenge is converting the Output<T> to str. I thought this is not possible because Output<T> means the value will be known in future?
l

little-cartoon-10569

10/30/2023, 9:33 PM
Yes, but "future" is before the program finishes. So you can output it at program end time. And you can pass it to any other Pulumi resource: resources know how to handle Outputs. This is what I meant when I said "as you get more familiar with the ways to get the values". This is all doable.
m

many-house-85254

10/30/2023, 9:34 PM
The config file is static, I meant it needs to be known at plan stage while Output<T> is known at apply stage so it has to be hardcoded (i hate it but GCP designed it that way). On AWS this is not a problem because they don't require a static file (the lambda can be passed as a variable).
l

little-cartoon-10569

10/30/2023, 9:35 PM
Ah. Well you can still do it: for example, you can use automation-api to wrap two Pulumi projects: one creates the Function, and one builds whatever resources need the Function name. You can get the Function name as a stack references into the 2nd project.
There are probably other ways to do it, too.
m

many-house-85254

10/30/2023, 9:36 PM
I see. I will take a look at automation api thanks.
l

little-cartoon-10569

10/30/2023, 9:36 PM
That's a more advanced topic: get what you are currently working on working. Then improve it later.
2 Views