Creating and tagging multiple AWS EC2 instances-without modules using Terraform

Posted: January 29, 2018 in Amazon Web Services (AWS), Linux, terraform

In one of previous posts we created multiple EC2 instances using modules, instead of using modules, we can just add following line instance.tf file

count=X (where X is number of instances
tags {
Name="${format("test-%01d",count.index+1)}"
}

01d is number of “zero” prefixes-1 (test-01) if we want more zeros change number in front of d (test-%03d would be test002,for example).For the sake of simplicity i created just one terraform file

provider "aws" {
access_key="access keys here"
secret_key="secret keys here"
region = "eu-west-1"
}

variable "count" {
default=2
}


resource "aws_instance" "example" {
count="${var.count}"
ami = "ami-d834aba1"
instance_type = "t2.micro"
tags { Name="${format("test-%01d",count.index+1)}" }
}

output "ip" {
    value = "${aws_instance.example.*.public_ip}"
}

In this case 2 instances were created, with test-1/2 tags

1

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s