terraform import
- to import existing resources to terraform or manuall created resources add to tf state file
import s3
- backend file and provide
- import resoure two ways
import {
to = aws_s3_bucket.example
identity = {
bucket = "bucket-name"
}
}
resource "aws_s3_bucket" "example" {
### Configuration omitted for brevity ###
}
terraform import aws_s3_bucket.example bucket-name
Note: After import completed remove import configure and run terraform init && terraform plan and fix plan changes and make tf state no changes then apply changes by terraform apply
Task:
create ec2 instance & security group manually and import resources to terraform statefile
Terraform workspace
-
create virtual enviromenmt and deploy resource specific names
-
before create workspace need run
terraform init -backend-config="key=dev/terraform.tfstate" -
terraform workspace
terraform workspace
Usage: terraform [global options] workspace
new, list, show, select and delete Terraform workspaces.
Subcommands:
delete Delete a workspace
list List Workspaces
new Create a new workspace
select Select a workspace
show Show the name of the current workspace
Ex:
- create workspace
terraform workspace new dev - select workspace
terraform workspace select dev
variable "s3-name" {
type = string
default = "qt-demo-s3-app"
}
locals {
enviroment = "${terraform.workspace}"
s3_name = "${var.s3-name}-${terraform.workspace}"
}
resource "aws_s3_bucket" "import_s3" {
bucket = local.s3_name
}
