Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pratikshinde55/terraform-aws-count
Create multiple EC2 instances and give dynamically name & instance type using terraform.
https://github.com/pratikshinde55/terraform-aws-count
hcl infrastucture-as-code terraform terraform-aws
Last synced: about 1 month ago
JSON representation
Create multiple EC2 instances and give dynamically name & instance type using terraform.
- Host: GitHub
- URL: https://github.com/pratikshinde55/terraform-aws-count
- Owner: Pratikshinde55
- Created: 2024-03-30T12:28:15.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-01-06T12:23:22.000Z (about 1 month ago)
- Last Synced: 2025-01-06T13:28:25.273Z (about 1 month ago)
- Topics: hcl, infrastucture-as-code, terraform, terraform-aws
- Language: HCL
- Homepage:
- Size: 80.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Multi-Instance AWS EC2 Deployment with Terraform
Create multiple EC2 instance and give dynamically name & instance type
## Count:
Count is Terraform meta-argument is used in resource block to specify that resource how many times run.count is a meta-argument defined by the Terraform
language. It can be used with modules and with every resource type.- Meta-argument: The terraform argument which is used in any resources are known as 'Meta-argument'.
- Count.index: It's special variable which define length of list of count. such as os[0] , os[1], os[2]......
In this work i have created below files for my code , mypsfile is created by "-out" keyword which store all setup code in binary format.
" # terraform.exe plan -out=mypsfile "
## Step-1:
provider file for plugin and put credentials info .(aws provider plugin)notepad provider.tf

## Step-2:
Here main.tf file for all code related to aws_instance Resource block.Data Source for AMI: It fetches the latest Amazon Linux 2 AMI with a specific naming convention (al2023-ami-*-x86_64) and other filters.
EC2 Instance Resource: It defines an EC2 instance resource, using the AMI fetched in the previous step. It specifies the instance type, security group ID,
and creates multiple instances using the count meta-argument.Variables: "names": A list of names for instances, "instanceType": A list of instance types, "sec-grpID": Security Group ID.
Output: It outputs a message based on the value of the Give-true-false variable. If it's true, it outputs a welcome message; otherwise, it suggests trying
again later.Locals: It defines some local values, such as admin name, owner, salary, and current time.
notepad main.tf

In terraform 'varible' we can't use terraform function, Because of this "locals" is used. (check main.tf file). Output block is required to run local value.
max(): max function is used to retrieve maximum value .
formatdate(): It's is terraform function which give format for date.
timestamp(): It's terraform function which run in real time and give real time.
## Step-3:
Now, 1st use terraform init & then terraform apply command entire setup is launched, i give true value output is welcome show during apply command run. & current
date and time show .
terraform init
terraform apply

## Now check on AWS Console:---
- here all three instances is launched with given instance type as well as instance name .

- For each instance bellow security group created by terraform:
- I give security group id in "vpc_security_group_ids" argument , the security group is created on aws console manually and only security_group id i give in terraform
main block.