Skip to content

Commit

Permalink
ebs_mount
Browse files Browse the repository at this point in the history
  • Loading branch information
yadavprakash committed Jun 8, 2021
1 parent 39b6d1b commit 5539ee7
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 26 deletions.
1 change: 1 addition & 0 deletions _example/basic_example/.terraform.tfstate.lock.info
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"ID":"a8845ee0-1bcc-b486-2b4a-27bfd1c1d124","Operation":"OperationTypeApply","Info":"","Who":"prakash@prakash","Version":"0.15.0","Created":"2021-06-03T16:14:41.182608774Z","Path":"terraform.tfstate"}
13 changes: 6 additions & 7 deletions _example/basic_example/example.tf
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ data "aws_iam_policy_document" "iam-policy" {
}
}


module "ec2" {
source = "./../../"

Expand All @@ -143,12 +142,10 @@ module "ec2" {
vpc_security_group_ids_list = [module.ssh.security_group_ids, module.http-https.security_group_ids]
subnet_ids = tolist(module.public_subnets.public_subnet_id)

assign_eip_address = true
associate_public_ip_address = true

instance_profile_enabled = true
iam_instance_profile = module.iam-role.name

assign_eip_address = true
associate_public_ip_address = true
instance_profile_enabled = true
iam_instance_profile = module.iam-role.name
disk_size = 8
ebs_optimized = false
ebs_volume_enabled = true
Expand All @@ -162,4 +159,6 @@ module "ec2" {
metadata_http_endpoint_enabled = true
metadata_http_put_response_hop_limit = "2"
delete_on_termination = false
user_data = file("user-data.sh")

}
16 changes: 16 additions & 0 deletions _example/basic_example/user-data.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash
sleep 60
DEVICE=/dev/$(lsblk -n | awk '$NF != "/" {print $1}'| tail -n 1 )
exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1
FS_TYPE=$(file -s $DEVICE | awk '{print $2}')
MOUNT_POINT=/data

# If no FS, then this output contains "data"
if [ "$FS_TYPE" = "data" ]
then
echo "Creating file system on $DEVICE"
mkfs -t ext4 $DEVICE
fi

mkdir $MOUNT_POINT
mount $DEVICE $MOUNT_POINT
1 change: 1 addition & 0 deletions _example/secure_example/example.tf
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,5 @@ module "ec2" {
metadata_http_endpoint_enabled = true
metadata_http_put_response_hop_limit = "2"
delete_on_termination = false
user_data = file("user-data.sh")
}
16 changes: 16 additions & 0 deletions _example/secure_example/user-data.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash
sleep 60
DEVICE=/dev/$(lsblk -n | awk '$NF != "/" {print $1}'| tail -n 1 )
exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1
FS_TYPE=$(file -s $DEVICE | awk '{print $2}')
MOUNT_POINT=/data

# If no FS, then this output contains "data"
if [ "$FS_TYPE" = "data" ]
then
echo "Creating file system on $DEVICE"
mkfs -t ext4 $DEVICE
fi

mkdir $MOUNT_POINT
mount $DEVICE $MOUNT_POINT
29 changes: 12 additions & 17 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,30 @@ locals {
ebs_iops = var.ebs_volume_type == "io1" ? var.ebs_iops : 0
}

data "aws_ami" "amazon_linux" {
most_recent = true

owners = ["amazon"]
data "aws_ami" "ubuntu" {
most_recent = "true"

filter {
name = "name"

values = [
"amzn-ami-hvm-*-x86_64-gp2",
]
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
}

filter {
name = "owner-alias"

values = [
"amazon",
]
}
owners = ["099720109477"]
}

resource "template_file" "userdata" {
template = "userdata.sh"
}


#Module : EC2
#Description : Terraform module to create an EC2 resource on AWS with Elastic IP Addresses
# and Elastic Block Store.
resource "aws_instance" "default" {
count = var.instance_enabled == true ? var.instance_count : 0

ami = var.ami == "" ? data.aws_ami.amazon_linux.id : var.ami
ami = var.ami == "" ? data.aws_ami.ubuntu.id : var.ami
ebs_optimized = var.ebs_optimized
instance_type = var.instance_type
key_name = var.key_name
Expand All @@ -62,7 +57,7 @@ resource "aws_instance" "default" {
tenancy = var.tenancy
host_id = var.host_id
cpu_core_count = var.cpu_core_count
user_data = var.user_data != "" ? base64encode(file(var.user_data)) : ""
user_data = var.user_data
iam_instance_profile = join("", aws_iam_instance_profile.default.*.name)
source_dest_check = var.source_dest_check
ipv6_address_count = var.ipv6_address_count
Expand Down
3 changes: 1 addition & 2 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ variable "root_block_device" {
variable "user_data" {
type = string
default = ""
description = "The Base64-encoded user data to provide when launching the instances."
sensitive = true
description = "(Optional) A string of the desired User Data for the ec2."
}

variable "assign_eip_address" {
Expand Down

0 comments on commit 5539ee7

Please sign in to comment.