Hello,
I’m tired to utilize terraform as used on this website Terraform Create Multiple EC2 with different Configs - for_each and count together
Terraform Create Multiple EC2 with different Configs – for_each and count together.
Instead of using the HereDocument, I’m trying to use The Terraform templatefile function.
However, I received the following errors:
``` on main.tf line 36, in resource "aws_instance" "web":
```36: user_data = templatefile(${each.value.appl_name}","${each.value.appl_version}")
``` each.value.appl_name is tuple with 1 element
``` ...
``` ...
``` ...
I have included the config files below. Please advise.
Thanks Michael
``` #variables.tf
``` variable "configuration" {
``` description = "The total configuration, List of Objects/Dictionary"
``` default = [{}]
```}
```#dev.tfvars
```configuration = [
``` {
``` "application_name" : "GritfyApp-dev",
``` "ami" : "ami-09e67e426f25ce0d7",
``` "no_of_instances" : "2",
``` "instance_type" : "t2.medium",
``` "subnet_id" : "subnet-0f4f294d8404946eb",
``` "vpc_security_group_ids" : ["sg-0d15a4cac0567478c","sg-0d8749c35f7439f3e"],
``` "appl_version" : [{ appl_1_version = "1.0" }],
``` "appl_name" : [/app_dir/appl_1_script.tpl"]
``` },
``` {
``` "application_name" : "GrityWeb-dev",
``` "ami" : "ami-0747bdcabd34c712a",
``` "instance_type" : "t2.micro",
``` "no_of_instances" : "1"
``` "subnet_id" : "subnet-0f4f294d8404946eb",
``` "vpc_security_group_ids" : ["sg-0d15a4cac0567478c"],
``` "appl_version" : [{ appl_2_version = "5.0" }],
``` "appl_name" : [/app_dir/appl_2_script.tpl"]
``` },
``` {
``` "application_name" : "OpsGrit-dev",
``` "ami" : "ami-0747bdcabd34c712a",
``` "instance_type" : "t3.micro",
``` "no_of_instances" : "3",
``` "subnet_id" : "subnet-0f4f294d8404946eb",
``` "vpc_security_group_ids" : ["sg-0d15a4cac0567478c"],
``` "appl_version" : [{ appl_2_version = "5.1.0" }],
``` "appl_name" : [/app_dir/fixit_script.tpl"]
``` }
```]
```#main.tf
```provider "aws" {
``` region = "us-east-1"
``` profile = "personal"
```}
```locals {
``` serverconfig = [
``` for srv in var.configuration : [
``` for i in range(1, srv.no_of_instances+1) : {
``` instance_name = "${srv.application_name}-${i}"
``` instance_type = srv.instance_type
``` subnet_id = srv.subnet_id
``` ami = srv.ami
``` security_groups = srv.vpc_security_group_ids
``` appl_version = "${srv.appl_version}"
``` appl_name = "${srv.appl_name}"
``` }
``` ]
``` ]
```}
```// We need to Flatten it before using it
```locals {
``` instances = flatten(local.serverconfig)
```}
```resource "aws_instance" "web" {
``` for_each = {for server in local.instances: server.instance_name => server}
``` instance_type = each.value.instance_type
``` vpc_security_group_ids = each.value.security_groups
``` user_data = templatefile(${each.value.appl_name}","${each.value.appl_version}")
```subnet_id = each.value.subnet_id
```tags = {
``` Name = "${each.value.instance_name}"
```}
```}
```output "instances" {
``` value = "${aws_instance.web}"
``` description = "All Machine details"
```})