Terraform Import and Plan shows change in user data whereas there is no change

611 Views Asked by At

We are running terraform import in the below code. We are importing an EC2 instance to get the Infrastructure into code.

module.tf

module "ec2_app_demo" {
  source = "./aws-ec2-application/"
  ec2_instances     = var.ec2_instances

}

main.tf

resource "aws_instance" "instances" {
  for_each = { for instance in var.ec2_instances : instance.name => instance }

  ami                    = each.value.ami
  instance_type          = each.value.type
  key_name               = each.value.key_name
  subnet_id              = join("\", \"", data.aws_subnet_ids.subnet_id["${each.value.subnet_name}"].ids)
  user_data = each.value.user_data != "" ? file("${path.module}/../${each.value.user_data}") : null
  vpc_security_group_ids = data.aws_security_groups.sg_id[each.value.name].ids
  secondary_private_ips  = each.value.secondary_private_ips
  iam_instance_profile   = each.value.instance_profile
  disable_api_termination = each.value.disable_api_termination

  root_block_device  {
    volume_type = each.value.root_block_device_volume_type
    volume_size = each.value.root_block_device_volume_size
    # tags = each.value.tags
    tags = each.value.tags_root_volume
    kms_key_id = each.value.kms_key != "" ? each.value.kms_key : null
    
  }

  tags = each.value.tags


}

tfvars file:

ec2_instances=[ {
    "additional_eni": 0,
    "ami": "ami-xxxxx",
    "disable_api_termination": true,
    "instance_profile": "iam-profile-ec2",
    "key_name": "keypair",
    "kms_key": "",
    "name": "Iacshell",
    "root_block_device_volume_size": 300,
    "root_block_device_volume_type": "gp3",
    "secondary_private_ips": [],
    "security_groups": [],
    "subnet_name": "Test-VPC-Subnet1A",
    "tags": {
      "Environment": "dev",
    },
    "tags_root_volume": {
      "Budget": "IaC",
      "Environment": "dev",
    },
    "type": "m5.2xlarge",
    "user_data": "Iacshell.sh",
    "vpc_name": "Test-VPC"
  }
]

Import Output:

[0m[0m
[0m[1mmodule.ec2_app_demo.aws_instance.instances["Iacshell"]: Importing from ID "i-0a6833b201f1fea6a"...[0m
[0m[1m[32mmodule.ec2_app_demo.aws_instance.instances["Iacshell"]: Import prepared![0m
[0m[32m  Prepared aws_instance for import[0m
[0m[1mmodule.ec2_app_demo.aws_instance.instances["Iacshell"]: Refreshing state... [id=i-0a6833b201f1fea6a][0m
[0m[32m
Import successful!

After running the import successfully, when we run the terraform plan it shows there is change in user data whereas we haven't made any change.

terraform plan output:

Terraform will perform the following actions:

  # module.ec2_app_demo.aws_instance.instances["devmedagent01"] will be updated in-place
  ~ resource "aws_instance" "instances" {
        id                                   = "i-0a6833b201f1fea6a"
        tags                                 = {
            "Environment"    = "dev"
        }
      ~ user_data                            = "af77afc8379a0a220e8772fd5d8670d66d12978f" -> "f543ec5ca251db148930f92e4bad4de6705f2dd6"
      + user_data_replace_on_change          = false
        # (29 unchanged attributes hidden)
        # (9 unchanged blocks hidden)
    }

Plan: 0 to add, 1 to change, 0 to destroy.

Iacshell.sh

#!/bin/bash
#echo "sshd_config";
sed -i 's|PasswordAuthentication no|PasswordAuthentication yes|g' /etc/ssh/sshd_config ;
sed -i 's|#PubkeyAuthentication yes|PubkeyAuthentication yes|g' /etc/ssh/sshd_config ;
sed -i 's|PermitRootLogin no|PermitRootLogin yes|g' /etc/ssh/sshd_config ;
systemctl restart sshd;
echo "g0tsh0t3" | passwd --stdin root
hostnamectl set-hostname iacshell.xxx.com
echo "HOSTNAME=Iacshell" >>/etc/sysconfig/network
yum install nmve-cli lvm2 -y

Please check and help why this happens. Let me know if any more information is required.

1

There are 1 best solutions below

0
On BEST ANSWER

It was weird spacing issue. There was a space in the end of script as a new line and 'Copy User data' option wasn't picking it. When we manually selected it, we were able to see. The issue is fixed now. Thank you.