terraform-community-providers / terraform-provider-railway

Terraform provider for railway.app
https://registry.terraform.io/providers/terraform-community-providers/railway/latest/docs
Mozilla Public License 2.0
22 stars 1 forks source link

Environment switching #23

Closed eliectrlshift closed 6 months ago

eliectrlshift commented 6 months ago

Hello guys, first thank you for you work.

I got a question about environment. I want to create a railway project, then an environment inside it and create different services based on the environment.

Actually it seem that I can't switch environment after :

resource "railway_project" "base" {
  name = var.RAILWAY_PROJECT_NAME
  has_pr_deploys = false
  private = true

  default_environment = {
    name = "production"
  }
}

Like based on this I would like to create 3 databases for production environment and only 1 for staging one.

Could you confirm that I can't achieve this or there is a workaround ?

Thank you !

Funding

Fund with Polar

eliectrlshift commented 6 months ago

Additional infos :

main.tf :

provider "railway" {
  token = var.RAILWAY_TOKEN
}

resource "railway_project" "base" {
  name = var.RAILWAY_PROJECT_NAME
  has_pr_deploys = false
  private = true

  default_environment = {
    name = "production"
  }
}

resource "railway_environment" "base_environment_staging" {
  name       = "staging"
  project_id = railway_project.base.id
}

module "production" {
  source = "./production"

  RAILWAY_PROJECT_ID = railway_project.base.id
}

module "staging" {
  source = "./staging"

  RAILWAY_PROJECT_ID = railway_project.base.id
}

production.tf :

resource "railway_project" "production" {
  name = var.RAILWAY_PROJECT_ID
  default_environment = {
    name = "production"
  }
}

module "postgresql" {
  source = "../modules/postgresql"

  ENVIRONMENT = "production"
  RAILWAY_PROJECT_ID = railway_project.production.id
}

staging.tf :

resource "railway_project" "staging" {
  id = var.RAILWAY_PROJECT_ID
  default_environment = {
    name = "staging"
  }
}

module "postgresql" {
  source = "../modules/postgresql"

  ENVIRONMENT = "staging"
  RAILWAY_PROJECT_ID = railway_project.staging.id
}

postgresql.tf :

locals {
  is_production = var.ENVIRONMENT == "production" ? 1 : 0
  is_staging    = var.ENVIRONMENT == "staging" ? 1 : 0
}

resource "railway_service" "postgresql-authorization" {
  count = local.is_production

  name       = "postgresql-authorization"
  project_id = var.RAILWAY_PROJECT_ID
  source_image = "ghcr.io/railwayapp-templates/postgres-ssl:latest"
}

resource "railway_service" "postgresql-users" {
  count = local.is_production

  name       = "postgresql-users"
  project_id = var.RAILWAY_PROJECT_ID
  source_image = "ghcr.io/railwayapp-templates/postgres-ssl:latest"
}

resource "railway_service" "postgresql-organizations" {
  count = local.is_production

  name       = "postgresql-organizations"
  project_id = var.RAILWAY_PROJECT_ID
  source_image = "ghcr.io/railwayapp-templates/postgres-ssl:latest"
}

resource "railway_service" "postgresql" {
  count = local.is_staging

  name       = "postgresql"
  project_id = var.RAILWAY_PROJECT_ID
  source_image = "ghcr.io/railwayapp-templates/postgres-ssl:latest"
}

this obviously create 3 projects.

pksunkara commented 6 months ago

Using resource "railway_project" will always create a project. Please understand how Terraform works and read the documentation for the provider.

eliectrlshift commented 6 months ago

Could you confirm that I can't achieve this or there is a workaround ?

this obviously create 3 projects

I know that the provided snippet will create defined projects.

My question was about if it's possible to create a service by specifying an environment.

pksunkara commented 6 months ago

No, if you go through railway documentation, you will understand that a service is present in all environments.

eliectrlshift commented 6 months ago

That where I go first and this make me thing that we possibly can operate on service trough environment scope (as you can in UI).

image

eliectrlshift commented 6 months ago

Thank you for your kind words and help.

In 2 hours and zero knowledge about golang I make a POC about linking service to environment directly.

It certainly not ready for production and test fails but I'm not sure that this is the best way to answer to people that simply asking a question.