How to retrieve details of ec2 instances?

I tried to use below code to retrieve data of ec2 instances running.

provider “aws” {
region = “us-west-2”
}

data “aws_instances” “instances” {
instance_state_names = [“running”, “stopped”,“pending”, “shutting-down”, “stopping”, “terminated”]
}

output “instances_details” {
value = [
for instance in data.aws_instances.instances.ids : {
name = aws_instance.instance[instance].tags[“Name”]
instance_id = aws_instance.instance[instance].id
state = aws_instance.instance[instance].state.Name
public_ip = aws_instance.instance[instance].public_ip
private_ip = aws_instance.instance[instance].private_ip
instance_type = aws_instance.instance[instance].instance_type
ami_id = aws_instance.instance[instance].image_id
launch_time = aws_instance.instance[instance].launch_time
volume_id = aws_instance.instance[instance].root_block_device[0].volume_id
vpc_id = aws_instance.instance[instance].vpc_id
subnet_id = aws_instance.instance[instance].subnet_id
elastic_ip = coalesce(aws_eip.eip[instance].public_ip, “”)
key_name = aws_instance.instance[instance].key_name
security_groups = aws_instance.instance[instance].security_groups[*].name
platform_tag_value = aws_instance.instance[instance].tags[“Platform”]
}
]
}
But getting error as An input variable with the name “ami_id” has not been declared. This variable can be declared with a variable “ami_id” {} block. This is for all the attributes, just shown one error.

Just below ouptut is working.

output “running_instances” {

value = data.aws_instances.instances.ids
}

Please share a fix so that I can retrieve state,public_ip,private_ip,instance_type,ami_id,launch_time,volume_id,vpc_id,subnet_id,elastic_ip,key,security_groups and specific tag values.