'Google Cloud Secret Manager - Create a secret in a region

I'm working with the Python library google-cloud-secret-manager and I'm facing some problems in creating a secret within a defined region.

In the method secretmanager.create_secret seems that there is a metadata parameter that can be filled but I keep receiving errors trying something like:

metadata=[{'region': 'europe-west1'}]

The library code below for me is not very clear on these parameters...

This is the original snippet I'm trying to execute:

response = secret_client.create_secret(
        request={
            "parent": parent,
            "secret_id": secret_id,
            "secret": {"replication": {"automatic": {}}},
        }
    )

Can someone help me?

Thanks in advance!!

EDIT

Following the full code, taken from the official documentation, where it is not specified how to specify the region...

def create_secret(project_id, secret_id):
"""
Create a new secret with the given name. A secret is a logical wrapper
around a collection of secret versions. Secret versions hold the actual
secret material.
"""

# Import the Secret Manager client library.
from google.cloud import secretmanager

# Create the Secret Manager client.
client = secretmanager.SecretManagerServiceClient()

# Build the resource name of the parent project.
parent = f"projects/{project_id}"

# Create the secret.
response = client.create_secret(
    request={
        "parent": parent,
        "secret_id": secret_id,
        "secret": {"replication": {"automatic": {}}},
    }
)

# Print the new secret name.
print("Created secret: {}".format(response.name))


Solution 1:[1]

If you want to specify the replication key placement manually, you need to specify it like in the example below:

response = client.create_secret(
    request={
        "parent": parent,
        "secret_id": secret_id,
        "secret": {"user_managed": {
               "replicas": [
                    {"location": "europe-west1"}
                ]}
        },
    }
)

Solution 2:[2]

The accepted answer ist not working for me. There is a layer above "user_managed" which is "replication". Here is my snippet:

client.create_secret(
    request={
        "parent": f"projects/<project_id>",
        "secret_id":"<secret_name>",
        "secret": {"replication": {"user_managed": {"replicas":[{"location": "<region_name>"}]}}},
    }
)

Exchange the values in the brackets ("<", ">") whit your own values.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Limkin