'terraform > forces new resource on security group

I've got a very simple piece of Terraform code:

provider "aws" {
  region = "eu-west-1"
}

module ec2 {
  source = "./ec2_instance"
  name = "EC2 Instance 1"
} 

where the module is:

variable "name" {
    default = "Default Name from ec2_instance.tf"
}

resource "aws_instance" "example" {
  ami = "ami-e5083683"
  instance_type = "t2.nano"
  subnet_id = "subnet-3e976259"
  associate_public_ip_address = true
  security_groups = [ "sg-7310e10b" ]
  tags {
    Name = "${var.name}"
  }
}

When I first run it I get this output:

security_groups.#:            "" => "1"
security_groups.1642973399:   "" => "sg-7310e10b"

However, the next time I try a plan I get:

  security_groups.#:            "0" => "1" (forces new resource)
  security_groups.1642973399:   "" => "sg-7310e10b" (forces new resource)

What gives?!



Solution 1:[1]

Your json["result"] is null. If there is a Result id is not null. Check the logique to add "as String" or"as String?" in fromMap.

class Result {
  Result({
    required this.id,
    this.nama,
    this.kode,
    this.jenis,
    this.jumlah,
    this.hargaSetor,
    this.hargaJual,
    this.foto,
    this.createdAt,
    this.v,
  });

  String id;
  String? nama;
  String? kode;
  String? jenis;
  String? jumlah;
  int? hargaSetor;
  int? hargaJual;
  String? foto;
  DateTime? createdAt;
  int? v;

  factory Result.fromJson(String str) => Result.fromMap(json.decode(str));

  String toJson() => json.encode(toMap());

  factory Result.fromMap(Map<String, dynamic> map) {
    return Result(
      id: map['id'] as String,
      nama: map['nama'] as String?,
      kode: map['kode'] as String?,
      jenis: map['jenis'] as String?,
      jumlah: map['jumlah'] as String?,
      hargaSetor: map['hargaSetor'] as int?,
      hargaJual: map['hargaJual'] as int?,
      foto: map['foto'] as String?,
      createdAt: map['createdAt'] as DateTime?,
      v: map['v'] as int?,
    );
  }

  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'nama': nama,
      'kode': kode,
      'jenis': jenis,
      'jumlah': jumlah,
      'hargaSetor': hargaSetor,
      'hargaJual': hargaJual,
      'foto': foto,
      'createdAt': createdAt,
      'v': v,
    };
  }
}

And in the GetSampah() at least the result should be empty.

class GetSampah {
  GetSampah({
    required this.hasil,
    required this.result,
  });

  bool hasil;
  List<Result> result;

  factory GetSampah.fromJson(String str) => GetSampah.fromMap(json.decode(str));

  String toJson() => json.encode(toMap());

  factory GetSampah.fromMap(Map<String, dynamic> json) => GetSampah(
    hasil: json["hasil"],
    result: List<Result>.from(json["result"].map((x) => Result.fromMap(x))),
  );

  Map<String, dynamic> toMap() => {
    "hasil": hasil,
    "result": List<dynamic>.from(result.map((x) => x.toMap())),
  };
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mario francois