TomDane
TomDane

Reputation: 1066

Terraform can't deploy API Gateway changes to one stage out of many?

Using this code:

resource "aws_api_gateway_deployment" "example_deployment" {
  depends_on = [
    "aws_api_gateway_method.example_api_method",
    "aws_api_gateway_integration.example_api_method_integration"
  ]
  rest_api_id = "${aws_api_gateway_rest_api.example_api.id}"
  stage_name = "${var.stage_name}"
}

I can deploy API Gateway changes to whatever stage I specify. However, this will override any existing stages. That is, if I first deploy to a stage called 'dev', then deploy to 'prod', it will erase 'dev'.

How can I achieve multi-stage deployments? So that I can first deploy to dev or staging, and if it all looks good, then deploy to the prod stage.

Upvotes: 2

Views: 2763

Answers (2)

Jay
Jay

Reputation: 9477

In 2024, if someone needs this feature ...

You have to make use of terraform workspace to deploy to multiple stages with the same code base and different tfvars per stage.

For example, if you want to deploy the same code to 2 environments dev and prod You would have 2 tfvars called dev.tfvars and prod.tfvars

Then create terraform workspace using below command

terraform workspace new dev

Then run the below command

terraform deploy -var-file=dev.tfvars

This will deploy your code to dev stage.

Now repeat the same for prod

Create terraform workspace using below commaond

terraform workspace new prod

Then run the below command

terraform deploy -var-file=prod.tfvars

This will deploy your code to prod stage.

In this way, you don't need to create duplicate folders.

Note: You can switch workspaces using the command

terraform workspace select

Ensure you are in the right workspace below deploying

Upvotes: 1

TomDane
TomDane

Reputation: 1066

After some research, we ended up taking a different tack. Based on articles like this and this, we split our terraform into folders per stage. So if you want to deploy dev, you run terraform inside the dev folder. To avoid code duplication, you use modules. It seems to be working well, and allows us to deploy different versions of the API.

Upvotes: 2

Related Questions