Skip to content

Commit 171c6dd

Browse files
authored
Merge pull request #9 from Patelvijaykumar/user-data
added user-dats support for ebs volume
2 parents dc62fe3 + 09fd94e commit 171c6dd

File tree

6 files changed

+187
-0
lines changed

6 files changed

+187
-0
lines changed

ebs-with-userdata/ebs_volume.tf

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
resource "aws_ebs_volume" "ebs_volume" {
2+
availability_zone = "us-east-1a"
3+
size = 20
4+
type = "gp2"
5+
6+
tags = {
7+
Name = "ebs-volume-terraform-demo"
8+
}
9+
}
10+
11+
resource "aws_volume_attachment" "ebc_volume_attachment" {
12+
device_name = var.device_name
13+
volume_id = aws_ebs_volume.ebs_volume.id
14+
instance_id = aws_instance.ebs_instance_example.id
15+
}
16+
17+
data "template_file" "init" {
18+
template = "${file("volume.sh")}"
19+
20+
vars = {
21+
device_name = var.device_name
22+
}
23+
}

ebs-with-userdata/instance.tf

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
provider "aws" {
2+
region = "${var.region}"
3+
version = "~> 2.0"
4+
}
5+
6+
7+
resource "aws_instance" "ebs_instance_example" {
8+
ami = lookup(var.ami_id, var.region)
9+
instance_type = var.instance_type
10+
subnet_id = aws_subnet.public_1.id
11+
12+
# Security group assign to instance
13+
vpc_security_group_ids = [aws_security_group.allow_ssh.id]
14+
15+
# key name
16+
key_name = var.key_name
17+
18+
user_data = data.template_file.init.rendered
19+
20+
21+
tags = {
22+
Name = "EBS with userdata"
23+
}
24+
}

ebs-with-userdata/security_group.tf

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
resource "aws_security_group" "allow_ssh" {
2+
name = "allow_SSH"
3+
description = "Allow SSH inbound traffic"
4+
vpc_id = aws_vpc.vpc_demo.id
5+
6+
ingress {
7+
# SSH Port 22 allowed from any IP
8+
from_port = 22
9+
to_port = 22
10+
protocol = "tcp"
11+
cidr_blocks = ["0.0.0.0/0"]
12+
}
13+
14+
ingress {
15+
# SSH Port 80 allowed from any IP
16+
from_port = 80
17+
to_port = 80
18+
protocol = "tcp"
19+
cidr_blocks = ["0.0.0.0/0"]
20+
}
21+
22+
egress {
23+
from_port = 0
24+
to_port = 0
25+
protocol = "-1"
26+
cidr_blocks = ["0.0.0.0/0"]
27+
}
28+
}

ebs-with-userdata/variables.tf

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
variable "region" {
2+
type = "string"
3+
default = "us-east-1"
4+
}
5+
variable "ami_id" {
6+
type = "map"
7+
default = {
8+
us-east-1 = "ami-035b3c7efe6d061d5"
9+
eu-west-2 = "ami-132b3c7efe6sdfdsfd"
10+
eu-central-1 = "ami-9787h5h6nsn75gd33"
11+
}
12+
}
13+
variable "instance_type" {
14+
type = "string"
15+
default = "t2.micro"
16+
}
17+
18+
variable "device_name" {
19+
type = "string"
20+
default = "/dev/xvdh"
21+
}
22+
variable "key_name" {
23+
type = "string"
24+
default = "ec2-demo"
25+
}
26+
27+
variable "cidr" {
28+
description = "The CIDR block for the VPC. Default value is a valid CIDR, but not acceptable by AWS and should be overridden"
29+
type = string
30+
default = "10.0.0.0/16"
31+
}
32+
variable "instance_tenancy" {
33+
description = "A tenancy option for instances launched into the VPC"
34+
type = string
35+
default = "default"
36+
}
37+
38+
variable "enable_dns_hostnames" {
39+
description = "Should be true to enable DNS hostnames in the VPC"
40+
type = bool
41+
default = true
42+
}
43+
44+
variable "enable_dns_support" {
45+
description = "Should be true to enable DNS support in the VPC"
46+
type = bool
47+
default = true
48+
}
49+
50+
variable "enable_classiclink" {
51+
description = "Should be true to enable ClassicLink for the VPC. Only valid in regions and accounts that support EC2 Classic."
52+
type = bool
53+
default = false
54+
}
55+
56+
variable "tags" {
57+
description = "A map of tags to add to all resources"
58+
type = string
59+
default = "Vpc-custom-demo"
60+
}

ebs-with-userdata/volume.sh

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#! /bin/bash
2+
sudo mkdir /data
3+
sudo mkfs.ext4 ${device_name}
4+
mount ${device_name} /data

ebs-with-userdata/vpc.tf

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
resource "aws_vpc" "vpc_demo" {
2+
cidr_block = var.cidr
3+
instance_tenancy = var.instance_tenancy
4+
enable_dns_hostnames = var.enable_dns_hostnames
5+
enable_dns_support = var.enable_dns_support
6+
enable_classiclink = var.enable_classiclink
7+
8+
tags = {
9+
Name = var.tags
10+
}
11+
}
12+
13+
resource "aws_internet_gateway" "gw" {
14+
vpc_id = aws_vpc.vpc_demo.id
15+
16+
tags = {
17+
Name = "internet-gateway-demo"
18+
}
19+
}
20+
21+
resource "aws_subnet" "public_1" {
22+
availability_zone = "us-east-1a"
23+
vpc_id = aws_vpc.vpc_demo.id
24+
map_public_ip_on_launch = true
25+
cidr_block = "10.0.1.0/24"
26+
27+
tags = {
28+
Name = "public_1-demo"
29+
}
30+
}
31+
32+
resource "aws_route_table" "route-public" {
33+
vpc_id = aws_vpc.vpc_demo.id
34+
35+
route {
36+
cidr_block = "10.0.0.0/0"
37+
gateway_id = aws_internet_gateway.gw.id
38+
}
39+
40+
tags = {
41+
Name = "public-route-table-demo"
42+
}
43+
}
44+
45+
resource "aws_route_table_association" "public_1" {
46+
subnet_id = aws_subnet.public_1.id
47+
route_table_id = aws_route_table.route-public.id
48+
}

0 commit comments

Comments
 (0)