is there a way to just create resources at the pro...
# permit-saas
a
is there a way to just create resources at the project level, rather than needing to recreate at the environment level each time?
a
Hi @Alec Handal, I'm an AI assistant for Permit.io. I'll analyze available sources and get back to you shortly.
p
Hey, thanks for reaching out ! It seems our team is a little busy right now, a human will be with you within the next hour or so. In the meantime, can I help you ?
a
In Permit.io, resources are created at the environment level, not at the project level (1) (2) (3) . Resources are tied to a selected environment (1) , and the API paths show that resources live under an environment:
/v2/schema/{proj_id}/{env_id}/resources
(2) . However, you can copy environments to avoid recreating resources each time (4) . When you copy an environment into a new environment, it includes all the resources :
Copy code
curl --location '<https://api.permit.io/v2/projects/{project_id}/envs/{env_id}/copy>' \
The copy operation includes resources along with other policy elements . You can also use scope parameters to include or exclude specific resources during the copy:
Copy code
curl --location '<https://api.permit.io/v2/projects/{project_id}/envs/{env_id}/copy>' \
 -H 'authorization: Bearer API_SECRET_KEY'\
 --data '{
 "target_env": {
 "new": {
 "key": "prod",
 "name": "production"
 }
 },
 "scope": {
 "roles": {
 "exclude": ["*"]
 },
 "resources": {
 "include": ["folder", "file"]
 },
 "resource_sets": {
 "exclude": ["test*"]
 },
 "user_sets": {
 "exclude": ["canada_users"]
 },
 "custom_policies": {
 "include": ["*.rego"]
 }
 }
}'
This approach lets you maintain resources in one environment and copy them to new environments as needed, rather than recreating them manually each time.
To learn more, see the sources I considered: [[1] Policy Basics](https://docs.permit.io/how-to/build-policies/policy-basics) [[2] Migrating v1 to v2](https://docs.permit.io/api/v2-migration-guide) [[3] GET /v2/schema/{proj_id}/{env_id}/resources/{resource_id}/roles/{role_id}/descendants](https://api.permit.io/v2/redoc#tag/Resource-Roles/operation/get_resource_role_descendants) [[4] Create a new Environment](https://docs.permit.io/manage-your-account/creating-environments)
o
AI is correct. I'd also recommend using our Terraform provider, allowing you to easy apply the same policy objects on multiple environment. @Ask Inkeep
a
Thanks for tagging me @Or Weis. I'll look through relevant sources and get back to you shortly.
Yes, that's absolutely right! The Terraform Provider is an excellent way to manage policy objects across multiple environments (1) . With the Terraform Provider, you can define your resources, roles, and permissions once in your Terraform configuration and then apply them to different environments . This approach aligns with Infrastructure as Code practices, allowing you to version control your authorization policies and maintain consistency across environments . Here's how you can use it to manage resources across environments:
Copy code
terraform {
  required_providers {
    permitio = {
      source = "<http://registry.terraform.io/permitio/permit-io|registry.terraform.io/permitio/permit-io>"
      version = "~> 0.0.14" # Use the latest version
    }
  }
}

# Configure the <http://Permit.io|Permit.io> provider
provider "permitio" {
  api_key = "YOUR_API_KEY" # Replace with your actual API key
  # api_url = "<https://api.permit.io>" # Optional: defaults to <https://api.permit.io>
}

# Define a resource (e.g., documents in your application)
resource "permitio_resource" "document" {
  key = "document"
  name = "Document"
  description = "A confidential document that users can read and write"
  
  actions = {
    "read" = {
      "name" = "Read"
      "description" = "Read a document"
    }
    "write" = {
      "name" = "Write"
      "description" = "Write to a document"
    }
    "delete" = {
      "name" = "Delete"
      "description" = "Delete a document"
    }
  }
  
  attributes = {
    "title" = {
      "description" = "The title of the document"
      "type" = "string"
    }
    "owner" = {
      "description" = "The owner of the document"
      "type" = "string"
    }
  }
}
You can also use Terraform workspaces to manage different environments :
Copy code
# Create separate workspaces for different environments
terraform workspace new dev
terraform workspace new staging
terraform workspace new prod

# Switch between workspaces
terraform workspace select dev
This way, you define your resources once and deploy them consistently across all your environments, avoiding the need to recreate them manually each time .
To learn more, see the sources I considered: [[1] Permit.io Terraform Provider](https://docs.permit.io/integrations/infra-as-code/terraform-provider)