Skip to content

Commit 2173452

Browse files
author
Kamlesh
committed
terraform 0.11.0
0 parents  commit 2173452

File tree

4 files changed

+260
-0
lines changed

4 files changed

+260
-0
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
AWS EC2 MODULE
2+
==============
3+
4+
This module is for create ec2 instance with related resources.
5+
6+
###### main.tf
7+
contain resource code for ec2 instance.
8+
9+
###### variables.tf
10+
contain variables declaration. all variables are empty by default. we will define or put value to variables in equls.tf (main file for infrastructure). these are input vars.
11+
12+
###### outputs.tf
13+
is alse contain variables but we will these variables for output like ec2 instance id.
14+
15+
## Infrastructure creats by this module
16+
17+
1. EC2
18+
19+
20+
21+

main.tf

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Managed By : CloudDrove
2+
# Copyright @ CloudDrove. All Right Reserved.
3+
4+
#Module : Label
5+
#Description : Terraform module to create consistent naming for multiple names.
6+
module "lables" {
7+
source = "git::https://github.com/clouddrove/terraform-lables.git?ref=tags/0.11.0"
8+
name = "${var.name}"
9+
application = "${var.application}"
10+
environment = "${var.environment}"
11+
}
12+
13+
locals {
14+
ebs_iops = "${var.ebs_volume_type == "io1" ? var.ebs_iops : "0"}"
15+
availability_zone = "${var.availability_zone != "" ? var.availability_zone : data.aws_subnet.default.availability_zone}"
16+
}
17+
18+
data "aws_subnet" "default" {
19+
id = "${var.subnet}"
20+
}
21+
22+
resource "aws_instance" "main" {
23+
ami = "${var.ami}"
24+
ebs_optimized = "${var.ebs_optimized}"
25+
instance_type = "${var.instance_type}"
26+
key_name = "${var.key_name}"
27+
monitoring = "${var.monitoring}"
28+
vpc_security_group_ids = ["${var.vpc_security_group_ids_list}"]
29+
subnet_id = "${var.subnet}"
30+
associate_public_ip_address = "${var.associate_public_ip_address}"
31+
ebs_block_device = "${var.ebs_block_device}"
32+
ephemeral_block_device = "${var.ephemeral_block_device}"
33+
disable_api_termination = "${var.disable_api_termination}"
34+
instance_initiated_shutdown_behavior = "${var.instance_initiated_shutdown_behavior}"
35+
placement_group = "${var.placement_group}"
36+
tenancy = "${var.tenancy}"
37+
user_data = "${var.user_data}"
38+
39+
root_block_device {
40+
volume_size = "${var.disk_size}"
41+
delete_on_termination = true
42+
}
43+
44+
tags = "${module.lables.tags}"
45+
46+
lifecycle {
47+
# Due to several known issues in Terraform AWS provider related to arguments of aws_instance:
48+
# (eg, https://github.com/terraform-providers/terraform-provider-aws/issues/2036)
49+
# we have to ignore changes in the following arguments
50+
ignore_changes = ["private_ip", "root_block_device", "ebs_block_device"]
51+
}
52+
}
53+
54+
resource "aws_eip" "default" {
55+
count = "${var.associate_public_ip_address == "true" && var.assign_eip_address == "true" ? 1 : 0}"
56+
network_interface = "${aws_instance.main.primary_network_interface_id}"
57+
vpc = "true"
58+
tags = "${module.lables.tags}"
59+
}
60+
61+
resource "aws_ebs_volume" "default" {
62+
count = "${var.ebs_volume_count}"
63+
availability_zone = "${local.availability_zone}"
64+
size = "${var.ebs_volume_size}"
65+
iops = "${local.ebs_iops}"
66+
type = "${var.ebs_volume_type}"
67+
tags = "${module.lables.tags}"
68+
}
69+
70+
resource "aws_volume_attachment" "default" {
71+
count = "${var.ebs_volume_count}"
72+
device_name = "${element(var.ebs_device_name, count.index)}"
73+
volume_id = "${element(aws_ebs_volume.default.*.id, count.index)}"
74+
instance_id = "${aws_instance.main.id}"
75+
}

outputs.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
output "instance_id" {
2+
value = "${aws_instance.main.id}"
3+
}
4+
5+
output "az" {
6+
value = "${aws_instance.main.availability_zone}"
7+
}

variables.tf

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
##### vpc
2+
3+
variable "ami" {
4+
description = "The AMI to use for the instance"
5+
default = ""
6+
}
7+
8+
variable "tenancy" {
9+
description = "(Optional) The tenancy of the instance (if the instance is running in a VPC). An instance with a tenancy of dedicated runs on single-tenant hardware. The host tenancy is not supported for the import-instance command."
10+
default = ""
11+
}
12+
13+
variable "ebs_optimized" {
14+
description = "(Optional) If true, the launched EC2 instance will be EBS-optimized."
15+
default = ""
16+
}
17+
18+
variable "instance_type" {
19+
description = "(Required) The type of instance to start. Updates to this field will trigger a stop/start of the EC2 instance."
20+
default = ""
21+
}
22+
23+
variable "key_name" {
24+
type = "string"
25+
description = "The key name to use for the instance."
26+
default = ""
27+
}
28+
29+
variable "monitoring" {
30+
description = "If true, the launched EC2 instance will have detailed monitoring enabled. (Available since v0.6.0)"
31+
default = ""
32+
}
33+
34+
variable "vpc_security_group_ids_list" {
35+
description = "A list of security group IDs to associate with."
36+
default = []
37+
}
38+
39+
variable "application" {
40+
type = "string"
41+
description = "Application (e.g. `cp` or `clouddrove`)"
42+
}
43+
44+
variable "environment" {
45+
type = "string"
46+
description = "Environment (e.g. `prod`, `dev`, `staging`)"
47+
}
48+
49+
variable "name" {
50+
description = "Name (e.g. `app` or `cluster`)"
51+
type = "string"
52+
}
53+
54+
variable "delimiter" {
55+
type = "string"
56+
default = "-"
57+
description = "Delimiter to be used between `namespace`, `stage`, `name` and `attributes`"
58+
}
59+
60+
variable "attributes" {
61+
type = "list"
62+
default = []
63+
description = "Additional attributes (e.g. `1`)"
64+
}
65+
66+
variable "tags" {
67+
type = "map"
68+
default = {}
69+
description = "Additional tags (e.g. map(`BusinessUnit`,`XYZ`)"
70+
}
71+
72+
variable "root_block_device" {
73+
description = "Customize details about the root block device of the instance. See Block Devices below for details"
74+
default = []
75+
}
76+
77+
variable "ebs_block_device" {
78+
description = "Additional EBS block devices to attach to the instance"
79+
default = []
80+
}
81+
82+
variable "ephemeral_block_device" {
83+
description = "Customize Ephemeral (also known as Instance Store) volumes on the instance"
84+
default = []
85+
}
86+
87+
variable "disable_api_termination" {
88+
description = "If true, enables EC2 Instance Termination Protection"
89+
default = false
90+
}
91+
92+
variable "placement_group" {
93+
description = "The Placement Group to start the instance in"
94+
default = ""
95+
}
96+
97+
variable "instance_initiated_shutdown_behavior" {
98+
description = "Shutdown behavior for the instance" # https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/terminating-instances.html#Using_ChangingInstanceInitiatedShutdownBehavior
99+
default = ""
100+
}
101+
102+
variable "associate_public_ip_address" {
103+
description = "Associate a public IP address with the instance"
104+
default = "true"
105+
}
106+
107+
variable "assign_eip_address" {
108+
description = "Assign an Elastic IP address to the instance"
109+
default = "true"
110+
}
111+
112+
variable "ebs_volume_count" {
113+
description = "Count of EBS volumes that will be attached to the instance"
114+
default = "0"
115+
}
116+
117+
variable "ebs_iops" {
118+
description = "Amount of provisioned IOPS. This must be set with a volume_type of io1"
119+
default = "0"
120+
}
121+
122+
variable "availability_zone" {
123+
description = "Availability Zone the instance is launched in. If not set, will be launched in the first AZ of the region"
124+
default = ""
125+
}
126+
127+
variable "ebs_device_name" {
128+
type = "list"
129+
description = "Name of the EBS device to mount"
130+
default = ["/dev/xvdb", "/dev/xvdc", "/dev/xvdd", "/dev/xvde", "/dev/xvdf", "/dev/xvdg", "/dev/xvdh", "/dev/xvdi", "/dev/xvdj", "/dev/xvdk", "/dev/xvdl", "/dev/xvdm", "/dev/xvdn", "/dev/xvdo", "/dev/xvdp", "/dev/xvdq", "/dev/xvdr", "/dev/xvds", "/dev/xvdt", "/dev/xvdu", "/dev/xvdv", "/dev/xvdw", "/dev/xvdx", "/dev/xvdy", "/dev/xvdz"]
131+
}
132+
133+
variable "ebs_volume_size" {
134+
description = "Size of the EBS volume in gigabytes"
135+
default = "30"
136+
}
137+
138+
variable "ebs_volume_type" {
139+
description = "The type of EBS volume. Can be standard, gp2 or io1"
140+
default = "gp2"
141+
}
142+
143+
variable "subnet" {
144+
type = "string"
145+
description = "VPC Subnet ID the instance is launched in"
146+
}
147+
148+
variable "user_data" {
149+
type = "string"
150+
description = "The Base64-encoded user data to provide when launching the instances"
151+
default = ""
152+
}
153+
154+
variable "disk_size" {
155+
description = "Size of the root volume in gigabytes"
156+
default = "8"
157+
}

0 commit comments

Comments
 (0)