i am looking for the way to delete a specific route from a VPC routing table. From all the example i saw , they are all about how to add a route.
If Terraform is managing a route you just need to change/remove the code to remove that route. Could you give a bit more information about what you already have? Has Terraform been used to create the VPC & routing tables? What code do you currently have?
Thank you. I am new on Terraform. Our current VPCs are not created from Terraform , we will need delete /modify some static route in route table. My test case is route to 8.8.8.8/32 next hop is on IGW in a rt
i found this work based on you suggest, first add it to the routing table even it is there already, thus generated file terraform.tfstate
###########################################################
terraform {
required_providers {
aws = {
source = “hashicorp/aws”
version = “~> 3.27”
}
}
required_version = “>= 0.14.9”
}
provider “aws” {
profile = “default”
region = var.region
}
resource “aws_route” “r” {
route_table_id = “rtb-0bef5f10b6ca41b78”
destination_cidr_block = “8.8.8.8/32”
gateway_id = “igw-082e1f5f13116627a”
}
then remove the aws-route config, run again. 8.8.8.8/32 route is removed from rt
terraform {
required_providers {
aws = {
source = “hashicorp/aws”
version = “~> 3.27”
}
}
required_version = “>= 0.14.9”
}
provider “aws” {
profile = “default”
region = var.region
}
#resource “aws_route” “r” {
route_table_id = “rtb-0bef5f10b6ca41b78”
destination_cidr_block = “8.8.8.8/32”
gateway_id = “igw-082e1f5f13116627a”
#}
it is the right way ? or have any other good method ?
sorry, copy/paste is messed in previous reply.
first i run even 8.8.8.8/32 is already in RT .
terraform {
required_providers {
aws = {
source = “hashicorp/aws”
version = “~> 3.27”
}
}
required_version = “>= 0.14.9”
}
provider “aws” {
profile = “default”
region = var.region
}
resource “aws_route” “r” {
route_table_id = “rtb-0bef5f10b6ca41b78”
destination_cidr_block = “8.8.8.8/32”
gateway_id = “igw-082e1f5f13116627a”
}
then run below ,
terraform {
required_providers {
aws = {
source = “hashicorp/aws”
version = “~> 3.27”
}
}
required_version = “>= 0.14.9”
}
provider “aws” {
profile = “default”
region = var.region
}
8.8.8.8/32 is removed
Terraform works by managing a set of resources, with all those resources defined in code and updated using the terraform apply
command. Resources should either be managed by Terraform or by something else (but never by both Terraform as well as something else).
To tell Terraform to manage a resource you can either just create the resource (adding code and running apply) or for existing resources use terraform import
.
You need to decide what you are trying to achieve? Are you wanting Terraform to take over the management of your VPC, subnets, routes, etc.? If so, create some code and them import all the existing resources. Then any time you want to make changes just change the code.
Or are you just wanting to remove an existing route without Terraform taking over management of anything else? If so Terraform probably isn’t the tool you are looking for. To just perform actions (e.g. add/remove a route) just use the AWS CLI. Terraform is “defined state” so you don’t think of actions, just the state you want things to be in - Terraform then figures out what to add/remove/change to achieve that.
Thank you very much for the explanation We plan to use Terraform to take over the management of your VPC, subnets, routes. I will check terraform import .
Thanks.xiaolin
Stuart-c
Looks like there is “terraform import” and “terraformer import”. what is the difference between them ? if i want to manage existing VPC, subnets route, should i use terrafromer import ?
thanks/xi
terraform import
is the built in command to add a resource into Terraform’s state file. That is the basic mechanism, but there might be third party tools to make it easier to mass import several resources, or create HCL code which matches the current reality.
Will Terraform import create configuration code ?
No. terraform import
just adds a specific resource into the Terraform state file. It is your responsibility to add the associated HCL code to describe the resource (either manually or using a different tool).
Thank you very much.
-xl