Outils pour utilisateurs

Outils du site


tech:cloud_-_terraform_import

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

tech:cloud_-_terraform_import [2025/03/24 15:06] – créée - modification externe 127.0.0.1tech:cloud_-_terraform_import [2026/05/29 17:54] (Version actuelle) Jean-Baptiste
Ligne 1: Ligne 1:
 +<!DOCTYPE markdown>
 +{{tag>Cloud IAC}}
 +
 +# Cloud - Terraform import - OpenTofu import
 +
 +## Importation d'une instance dans le "state"
 +
 +`import.tf`
 +~~~
 +provider "aws" {
 +  profile    = "default"
 +  region     = "eu-west-1"
 +}
 +
 +resource "aws_instance" "inst1" {
 +  # (resource arguments)
 +}
 +~~~
 +
 +~~~bash
 +terraform import aws_instance.inst1 i-0127e23e987f1f08a
 +~~~
 +
 +## Création de la définition de notre instance
 +
 +~~~bash
 +terraform state list
 +terraform state show -no-color aws_instance.inst1 > aws_instance-inst1.tf
 +
 +mv import.tf import.tf.bak
 +
 +terraform fmt aws_instance-inst1.tf
 +~~~
 +
 +Créer ce fichier
 +`region.tf`
 +~~~
 +provider "aws" {
 +access_key = ""
 +secret_key = ""
 +region = "eu-west-1"
 +}
 +~~~
 +
 +
 +### Correction du code
 +
 +~~~bash
 +terraform plan -target=aws_instance.inst1 2>&1 | grep Err
 +~~~
 +
 +~~~
 +Error: "public_ip": this field cannot be set
 +Error: "arn": this field cannot be set
 +Error: "public_dns": this field cannot be set
 +Error: "instance_state": this field cannot be set
 +Error: "root_block_device.0.volume_id": this field cannot be set
 +Error: "private_dns": this field cannot be set
 +Error: "primary_network_interface_id": this field cannot be set
 +Error: : invalid or unknown key: id
 +~~~
 +
 +
 +Commenter les ligner en erreur ("public_ip, arn...")
 +
 +`aws_instance-inst1.tf`
 +~~~
 +# aws_instance.inst1:
 +resource "aws_instance" "inst1" {
 +  ami                          = "ami-0a3c5e107eda3e29b"
 +  #arn                          = "arn:aws:ec2:eu-west-1:299632874688:instance/i-0127e23e987f1f08a"
 +  associate_public_ip_address  = true
 +  availability_zone            = "eu-west-1b"
 +  #cpu_core_count               = 1
 +  #cpu_threads_per_core         = 1
 +  disable_api_termination      = false
 +  ebs_optimized                = false
 +  get_password_data            = false
 +  #id                           = "i-0127e23e987f1f08a"
 +  #instance_state               = "running"
 +  instance_type                = "t2.micro"
 +  ipv6_address_count           = 0
 +  ipv6_addresses               = []
 +  key_name                     = "My keyPair"
 +  monitoring                   = false
 +  #primary_network_interface_id = "eni-034b4bb5957d77123"
 +  #private_dns                  = "ip-172-31-237-138.eu-west-1.compute.internal"
 +  private_ip                   = "172.31.237.138"
 +  #public_dns                   = "ec2-34-241-189-63.eu-west-1.compute.amazonaws.com"
 +  #public_ip                    = "34.241.189.63"
 +  security_groups              = []
 +  source_dest_check            = true
 +  subnet_id                    = "subnet-0d63fcd2f893bbb9b"
 +  tags = {
 +    "Name" = "InstanceName"
 +  }
 +  tenancy     = "default"
 +  volume_tags = {}
 +  vpc_security_group_ids = [
 +    "sg-0e09491e039edf3dd",
 +  ]
 +
 +  credit_specification {
 +    cpu_credits = "standard"
 +  }
 +
 +#  root_block_device {
 +#    delete_on_termination = true
 +#    iops                  = 100
 +#    volume_id             = "vol-02ccd390ba7b838cd"
 +#    volume_size           = 8
 +#    volume_type           = "gp2"
 +#  }
 +
 +  timeouts {}
 +}
 +~~~
 +
 +~~~bash
 +#terraform plan -target=aws_instance.inst1
 +terraform apply
 +~~~
 +
 +
 +~~~
 +Error: Error launching source instance: UnsupportedOperation: The t2.micro instance type does not support specifying CpuOptions.
 +        status code: 400, request id: e6bb04ea-3f2a-47d7-a140-98d12857ceed
 +~~~
 +
 +Commenter les lignes "cpu_core_count" et "cpu_threads_per_core" puis
 +
 +~~~bash
 +terraform plan | grep 'known after apply'
 +#terraform taint aws_instance.inst1
 +terraform state rm aws_instance.inst1
 +terraform apply
 +~~~
 +
  

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki