Skip to content

Commit

Permalink
terraform 0.11.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamlesh committed Aug 12, 2019
0 parents commit 2173452
Show file tree
Hide file tree
Showing 4 changed files with 260 additions and 0 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
AWS EC2 MODULE
==============

This module is for create ec2 instance with related resources.

###### main.tf
contain resource code for ec2 instance.

###### variables.tf
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.

###### outputs.tf
is alse contain variables but we will these variables for output like ec2 instance id.

## Infrastructure creats by this module

1. EC2




75 changes: 75 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Managed By : CloudDrove
# Copyright @ CloudDrove. All Right Reserved.

#Module : Label
#Description : Terraform module to create consistent naming for multiple names.
module "lables" {
source = "git::https://github.com/clouddrove/terraform-lables.git?ref=tags/0.11.0"
name = "${var.name}"
application = "${var.application}"
environment = "${var.environment}"
}

locals {
ebs_iops = "${var.ebs_volume_type == "io1" ? var.ebs_iops : "0"}"
availability_zone = "${var.availability_zone != "" ? var.availability_zone : data.aws_subnet.default.availability_zone}"
}

data "aws_subnet" "default" {
id = "${var.subnet}"
}

resource "aws_instance" "main" {
ami = "${var.ami}"
ebs_optimized = "${var.ebs_optimized}"
instance_type = "${var.instance_type}"
key_name = "${var.key_name}"
monitoring = "${var.monitoring}"
vpc_security_group_ids = ["${var.vpc_security_group_ids_list}"]
subnet_id = "${var.subnet}"
associate_public_ip_address = "${var.associate_public_ip_address}"
ebs_block_device = "${var.ebs_block_device}"
ephemeral_block_device = "${var.ephemeral_block_device}"
disable_api_termination = "${var.disable_api_termination}"
instance_initiated_shutdown_behavior = "${var.instance_initiated_shutdown_behavior}"
placement_group = "${var.placement_group}"
tenancy = "${var.tenancy}"
user_data = "${var.user_data}"

root_block_device {
volume_size = "${var.disk_size}"
delete_on_termination = true
}

tags = "${module.lables.tags}"

lifecycle {
# Due to several known issues in Terraform AWS provider related to arguments of aws_instance:
# (eg, https://github.com/terraform-providers/terraform-provider-aws/issues/2036)
# we have to ignore changes in the following arguments
ignore_changes = ["private_ip", "root_block_device", "ebs_block_device"]
}
}

resource "aws_eip" "default" {
count = "${var.associate_public_ip_address == "true" && var.assign_eip_address == "true" ? 1 : 0}"
network_interface = "${aws_instance.main.primary_network_interface_id}"
vpc = "true"
tags = "${module.lables.tags}"
}

resource "aws_ebs_volume" "default" {
count = "${var.ebs_volume_count}"
availability_zone = "${local.availability_zone}"
size = "${var.ebs_volume_size}"
iops = "${local.ebs_iops}"
type = "${var.ebs_volume_type}"
tags = "${module.lables.tags}"
}

resource "aws_volume_attachment" "default" {
count = "${var.ebs_volume_count}"
device_name = "${element(var.ebs_device_name, count.index)}"
volume_id = "${element(aws_ebs_volume.default.*.id, count.index)}"
instance_id = "${aws_instance.main.id}"
}
7 changes: 7 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
output "instance_id" {
value = "${aws_instance.main.id}"
}

output "az" {
value = "${aws_instance.main.availability_zone}"
}
157 changes: 157 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
##### vpc

variable "ami" {
description = "The AMI to use for the instance"
default = ""
}

variable "tenancy" {
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."
default = ""
}

variable "ebs_optimized" {
description = "(Optional) If true, the launched EC2 instance will be EBS-optimized."
default = ""
}

variable "instance_type" {
description = "(Required) The type of instance to start. Updates to this field will trigger a stop/start of the EC2 instance."
default = ""
}

variable "key_name" {
type = "string"
description = "The key name to use for the instance."
default = ""
}

variable "monitoring" {
description = "If true, the launched EC2 instance will have detailed monitoring enabled. (Available since v0.6.0)"
default = ""
}

variable "vpc_security_group_ids_list" {
description = "A list of security group IDs to associate with."
default = []
}

variable "application" {
type = "string"
description = "Application (e.g. `cp` or `clouddrove`)"
}

variable "environment" {
type = "string"
description = "Environment (e.g. `prod`, `dev`, `staging`)"
}

variable "name" {
description = "Name (e.g. `app` or `cluster`)"
type = "string"
}

variable "delimiter" {
type = "string"
default = "-"
description = "Delimiter to be used between `namespace`, `stage`, `name` and `attributes`"
}

variable "attributes" {
type = "list"
default = []
description = "Additional attributes (e.g. `1`)"
}

variable "tags" {
type = "map"
default = {}
description = "Additional tags (e.g. map(`BusinessUnit`,`XYZ`)"
}

variable "root_block_device" {
description = "Customize details about the root block device of the instance. See Block Devices below for details"
default = []
}

variable "ebs_block_device" {
description = "Additional EBS block devices to attach to the instance"
default = []
}

variable "ephemeral_block_device" {
description = "Customize Ephemeral (also known as Instance Store) volumes on the instance"
default = []
}

variable "disable_api_termination" {
description = "If true, enables EC2 Instance Termination Protection"
default = false
}

variable "placement_group" {
description = "The Placement Group to start the instance in"
default = ""
}

variable "instance_initiated_shutdown_behavior" {
description = "Shutdown behavior for the instance" # https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/terminating-instances.html#Using_ChangingInstanceInitiatedShutdownBehavior
default = ""
}

variable "associate_public_ip_address" {
description = "Associate a public IP address with the instance"
default = "true"
}

variable "assign_eip_address" {
description = "Assign an Elastic IP address to the instance"
default = "true"
}

variable "ebs_volume_count" {
description = "Count of EBS volumes that will be attached to the instance"
default = "0"
}

variable "ebs_iops" {
description = "Amount of provisioned IOPS. This must be set with a volume_type of io1"
default = "0"
}

variable "availability_zone" {
description = "Availability Zone the instance is launched in. If not set, will be launched in the first AZ of the region"
default = ""
}

variable "ebs_device_name" {
type = "list"
description = "Name of the EBS device to mount"
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"]
}

variable "ebs_volume_size" {
description = "Size of the EBS volume in gigabytes"
default = "30"
}

variable "ebs_volume_type" {
description = "The type of EBS volume. Can be standard, gp2 or io1"
default = "gp2"
}

variable "subnet" {
type = "string"
description = "VPC Subnet ID the instance is launched in"
}

variable "user_data" {
type = "string"
description = "The Base64-encoded user data to provide when launching the instances"
default = ""
}

variable "disk_size" {
description = "Size of the root volume in gigabytes"
default = "8"
}

0 comments on commit 2173452

Please sign in to comment.