Skip to content

Commit aa59be9

Browse files
author
Sohan
committed
fix the examples and volumes part
1 parent 11ac26a commit aa59be9

File tree

6 files changed

+164
-149
lines changed

6 files changed

+164
-149
lines changed

.github/workflows/readme.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,4 @@ jobs:
5252
env:
5353
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # required
5454
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_TERRAFORM }} # required
55-
if: always()
55+
if: always()

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
Terraform AWS EC2
88
</h1>
99

10-
<p align="center" style="font-size: 1.2rem;">
10+
<p align="center" style="font-size: 1.2rem;">
1111
Terraform module to create an EC2 resource on AWS with ElasticC IP Addresses and Elastic Block Store.
1212
</p>
1313

@@ -38,7 +38,7 @@
3838
<hr>
3939

4040

41-
We eat, drink, sleep and most importantly love **DevOps**. We are working towards strategies for standardizing architecture while ensuring security for the infrastructure. We are strong believer of the philosophy <b>Bigger problems are always solved by breaking them into smaller manageable problems</b>. Resonating with microservices architecture, it is considered best-practice to run database, cluster, storage in smaller <b>connected yet manageable pieces</b> within the infrastructure.
41+
We eat, drink, sleep and most importantly love **DevOps**. We are working towards strategies for standardizing architecture while ensuring security for the infrastructure. We are strong believer of the philosophy <b>Bigger problems are always solved by breaking them into smaller manageable problems</b>. Resonating with microservices architecture, it is considered best-practice to run database, cluster, storage in smaller <b>connected yet manageable pieces</b> within the infrastructure.
4242

4343
This module is basically combination of [Terraform open source](https://www.terraform.io/) and includes automatation tests and examples. It also helps to create and improve your infrastructure with minimalistic code instead of maintaining the whole infrastructure code yourself.
4444

@@ -49,7 +49,7 @@ We have [*fifty plus terraform modules*][terraform_modules]. A few of them are c
4949

5050
## Prerequisites
5151

52-
This module has a few dependencies:
52+
This module has a few dependencies:
5353

5454
- [Terraform 0.13](https://learn.hashicorp.com/terraform/getting-started/install.html)
5555
- [Go](https://golang.org/doc/install)
@@ -214,7 +214,7 @@ Here is examples of how you can use this module in your inventory structure:
214214

215215

216216
## Testing
217-
In this module testing is performed with [terratest](https://github.com/gruntwork-io/terratest) and it creates a small piece of infrastructure, matches the output like ARN, ID and Tags name etc and destroy infrastructure in your AWS account. This testing is written in GO, so you need a [GO environment](https://golang.org/doc/install) in your system.
217+
In this module testing is performed with [terratest](https://github.com/gruntwork-io/terratest) and it creates a small piece of infrastructure, matches the output like ARN, ID and Tags name etc and destroy infrastructure in your AWS account. This testing is written in GO, so you need a [GO environment](https://golang.org/doc/install) in your system.
218218

219219
You need to run the following command in the testing folder:
220220
```hcl
@@ -223,7 +223,7 @@ You need to run the following command in the testing folder:
223223

224224

225225

226-
## Feedback
226+
## Feedback
227227
If you come accross a bug or have any feedback, please log it in our [issue tracker](https://github.com/clouddrove/terraform-aws-ec2/issues), or feel free to drop us an email at [[email protected]](mailto:[email protected]).
228228

229229
If you have found it worth your time, go ahead and give us a ★ on [our GitHub](https://github.com/clouddrove/terraform-aws-ec2)!

_example/basic_example/example.tf

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ module "kms_key" {
7676
description = "KMS key for ec2"
7777
deletion_window_in_days = 7
7878
enable_key_rotation = true
79-
alias = "alias/ec2"
79+
alias = "alias/ec2-instance"
8080
policy = data.aws_iam_policy_document.kms.json
8181
}
8282

@@ -121,37 +121,56 @@ data "aws_iam_policy_document" "iam-policy" {
121121
}
122122

123123
module "ec2" {
124-
source = "./../../"
125-
124+
source = "./../../"
126125
name = "ec2"
127126
environment = "test"
128127
label_order = ["name", "environment"]
129128

130-
instance_count = 1
131-
ami = "ami-08d658f84a6d84a80"
132-
instance_type = "t2.nano"
133-
monitoring = false
134-
tenancy = "default"
129+
#instance
130+
instance_enabled = true
131+
instance_count = 2
132+
ami = "ami-08d658f84a6d84a80"
133+
instance_type = "t2.nano"
134+
monitoring = false
135+
tenancy = "default"
136+
137+
#Networking
135138
vpc_security_group_ids_list = [module.ssh.security_group_ids, module.http-https.security_group_ids]
136139
subnet_ids = tolist(module.public_subnets.public_subnet_id)
137-
138-
assign_eip_address = true
139-
associate_public_ip_address = true
140-
instance_profile_enabled = true
141-
iam_instance_profile = module.iam-role.name
142-
disk_size = 8
143-
ebs_optimized = false
144-
ebs_volume_enabled = true
145-
ebs_volume_type = "gp2"
146-
ebs_volume_size = 30
147-
instance_tags = { "snapshot" = true }
148-
dns_zone_id = "Z1XJD7SSBKXLC1"
149-
hostname = "ec2"
150-
kms_key_id = module.kms_key.key_arn
151-
metadata_http_tokens_required = true
152-
metadata_http_endpoint_enabled = true
153-
metadata_http_put_response_hop_limit = "2"
154-
delete_on_termination = false
155-
user_data = file("user-data.sh")
140+
assign_eip_address = true
141+
associate_public_ip_address = true
142+
143+
#IAM
144+
instance_profile_enabled = false
145+
iam_instance_profile = module.iam-role.name
146+
147+
#Root Volume
148+
root_block_device = [
149+
{
150+
volume_type = "gp2"
151+
volume_size = 15
152+
delete_on_termination = true
153+
kms_key_id = module.kms_key.key_arn
154+
}
155+
]
156+
157+
#EBS Volume
158+
ebs_optimized = false
159+
ebs_volume_enabled = false
160+
ebs_volume_type = "gp2"
161+
ebs_volume_size = 30
162+
163+
#DNS
164+
dns_enabled = false
165+
dns_zone_id = "Z1XJD7SSBKXLC1"
166+
hostname = "ec2"
167+
168+
#Tags
169+
instance_tags = { "snapshot" = true }
170+
171+
# Metadata
172+
metadata_http_tokens_required = "required"
173+
metadata_http_endpoint_enabled = "enabled"
174+
metadata_http_put_response_hop_limit = 2
156175

157176
}

_example/secure_example/example.tf

Lines changed: 91 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module "vpc" {
88

99
name = "vpc"
1010
environment = "test"
11-
label_order = ["environment", "name"]
11+
label_order = ["name", "environment"]
1212

1313
cidr_block = "172.16.0.0/16"
1414
}
@@ -17,10 +17,9 @@ module "public_subnets" {
1717
source = "clouddrove/subnet/aws"
1818
version = "0.15.0"
1919

20-
2120
name = "public-subnet"
2221
environment = "test"
23-
label_order = ["environment", "name"]
22+
label_order = ["name", "environment"]
2423

2524
availability_zones = ["eu-west-1b", "eu-west-1c"]
2625
vpc_id = module.vpc.vpc_id
@@ -31,79 +30,51 @@ module "public_subnets" {
3130
}
3231

3332
module "http-https" {
34-
source = "clouddrove/security-group/aws"
35-
version = "0.15.0"
36-
33+
source = "clouddrove/security-group/aws"
34+
version = "0.15.0"
3735
name = "http-https"
3836
environment = "test"
39-
label_order = ["environment", "name"]
37+
label_order = ["name", "environment"]
4038

4139
vpc_id = module.vpc.vpc_id
4240
allowed_ip = ["0.0.0.0/0"]
4341
allowed_ports = [80, 443]
4442
}
4543

4644
module "ssh" {
47-
source = "clouddrove/security-group/aws"
48-
version = "0.15.0"
49-
45+
source = "clouddrove/security-group/aws"
46+
version = "0.15.0"
5047
name = "ssh"
5148
environment = "test"
52-
label_order = ["environment", "name"]
49+
label_order = ["name", "environment"]
5350

5451
vpc_id = module.vpc.vpc_id
5552
allowed_ip = [module.vpc.vpc_cidr_block, "0.0.0.0/0"]
5653
allowed_ports = [22]
5754
}
5855

56+
module "keypair" {
57+
source = "clouddrove/keypair/aws"
58+
version = "0.15.0"
59+
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDc4AjHFctUATtd5of4u9bJtTgkh9bKogSDjxc9QqbylRORxUa422jO+t1ldTVdyqDRKltxQCJb4v23HZc2kssU5uROxpiF2fzgiHXRduL+RtyOtY2J+rNUdCRmHz4WQySblYpgteIJZpVo2smwdek8xSpjoHXhgxxa9hb4pQQwyjtVGEdH8vdYwtxgPZgPVaJgHVeJgVmhjTf2VGTATaeR9txzHsEPxhe/n1y34mQjX0ygEX8x0RZzlGziD1ih3KPaIHcpTVSYYk4LOoMK38vEI67SIMomskKn4yU043s+t9ZriJwk2V9+oU6tJU/5E1rd0SskXUhTypc3/Znc/rkYtLe8s6Uy26LOrBFzlhnCT7YH1XbCv3rEO+Nn184T4BSHeW2up8UJ1SOEd+WzzynXczdXoQcBN2kaz4dYFpRXchsAB6ejZrbEq7wyZvutf11OiS21XQ67+30lEL2WAO4i95e4sI8AdgwJgzrqVcicr3ImE+BRDkndMn5k1LhNGqwMD3Iuoel84xvinPAcElDLiFmL3BJVA/53bAlUmWqvUGW9SL5JpLUmZgE6kp+Tps7D9jpooGGJKmqgJLkJTzAmTSJh0gea/rT5KwI4j169TQD9xl6wFqns4BdQ4dMKHQCgDx8LbEd96l9F9ruWwQ8EAZBe4nIEKTV9ri+04JVhSQ== [email protected]"
60+
key_name = "devops"
61+
environment = "test"
62+
enable_key_pair = true
63+
}
64+
5965
module "iam-role" {
6066
source = "clouddrove/iam-role/aws"
6167
version = "0.15.0"
6268

63-
name = "iam-role-ec2"
69+
name = "iam-role"
6470
environment = "test"
65-
label_order = ["environment", "name"]
71+
label_order = ["name", "environment"]
6672
assume_role_policy = data.aws_iam_policy_document.default.json
6773

6874
policy_enabled = true
6975
policy = data.aws_iam_policy_document.iam-policy.json
7076
}
7177

72-
data "aws_iam_policy_document" "default" {
73-
statement {
74-
effect = "Allow"
75-
actions = ["sts:AssumeRole"]
76-
principals {
77-
type = "Service"
78-
identifiers = ["ec2.amazonaws.com"]
79-
}
80-
}
81-
}
82-
83-
data "aws_iam_policy_document" "iam-policy" {
84-
statement {
85-
actions = [
86-
"ssm:UpdateInstanceInformation",
87-
"ssmmessages:CreateControlChannel",
88-
"ssmmessages:CreateDataChannel",
89-
"ssmmessages:OpenControlChannel",
90-
"ssmmessages:OpenDataChannel"]
91-
effect = "Allow"
92-
resources = ["*"]
93-
}
94-
statement {
95-
actions = [
96-
"kms:CreateGrant"]
97-
effect = "Allow"
98-
resources = [module.kms_key.key_arn]
99-
condition {
100-
test = "Bool"
101-
values = [true]
102-
variable = "kms:GrantIsForAWSResource"
103-
}
104-
}
105-
}
106-
10778
module "kms_key" {
10879
source = "clouddrove/kms/aws"
10980
version = "0.15.0"
@@ -114,10 +85,11 @@ module "kms_key" {
11485
description = "KMS key for ec2"
11586
deletion_window_in_days = 7
11687
enable_key_rotation = true
117-
alias = "alias/ec2"
88+
alias = "alias/ec2-instance"
11889
policy = data.aws_iam_policy_document.kms.json
11990
}
12091

92+
12193
data "aws_iam_policy_document" "kms" {
12294
version = "2012-10-17"
12395
statement {
@@ -133,39 +105,86 @@ data "aws_iam_policy_document" "kms" {
133105

134106
}
135107

136-
module "ec2" {
137-
source = "./../../"
108+
data "aws_iam_policy_document" "default" {
109+
statement {
110+
effect = "Allow"
111+
actions = ["sts:AssumeRole"]
112+
principals {
113+
type = "Service"
114+
identifiers = ["ec2.amazonaws.com"]
115+
}
116+
}
117+
}
138118

119+
data "aws_iam_policy_document" "iam-policy" {
120+
statement {
121+
actions = [
122+
"ssm:UpdateInstanceInformation",
123+
"ssmmessages:CreateControlChannel",
124+
"ssmmessages:CreateDataChannel",
125+
"ssmmessages:OpenControlChannel",
126+
"ssmmessages:OpenDataChannel"]
127+
effect = "Allow"
128+
resources = ["*"]
129+
}
130+
}
131+
132+
module "ec2" {
133+
source = "./../../"
139134
name = "ec2"
140135
environment = "test"
141136
label_order = ["name", "environment"]
142137

143-
instance_count = 2
144-
ami = "ami-08d658f84a6d84a80"
145-
instance_type = "t2.nano"
146-
monitoring = false
147-
tenancy = "default"
138+
#Instance
139+
instance_enabled = true
140+
instance_count = 2
141+
ami = "ami-08d658f84a6d84a80"
142+
instance_type = "t2.nano"
143+
monitoring = false
144+
tenancy = "default"
145+
146+
#Keypair
147+
key_name = module.keypair.name
148+
149+
#Networking
148150
vpc_security_group_ids_list = [module.ssh.security_group_ids, module.http-https.security_group_ids]
149151
subnet_ids = tolist(module.public_subnets.public_subnet_id)
150-
151152
assign_eip_address = true
152153
associate_public_ip_address = true
153154

154-
instance_profile_enabled = true
155+
#IAM
156+
instance_profile_enabled = false
155157
iam_instance_profile = module.iam-role.name
156158

157-
disk_size = 8
158-
ebs_optimized = false
159-
ebs_volume_enabled = true
160-
ebs_volume_type = "gp2"
161-
ebs_volume_size = 30
162-
kms_key_id = module.kms_key.key_arn
163-
instance_tags = { "snapshot" = true }
164-
dns_zone_id = "Z1XJD7SSBKXLC1"
165-
hostname = "ec2"
166-
metadata_http_tokens_required = true
167-
metadata_http_endpoint_enabled = true
168-
metadata_http_put_response_hop_limit = "2"
169-
delete_on_termination = false
170-
user_data = file("user-data.sh")
159+
#Root Volume
160+
root_block_device = [
161+
{
162+
volume_type = "gp2"
163+
volume_size = 15
164+
delete_on_termination = true
165+
kms_key_id = module.kms_key.key_arn
166+
}
167+
]
168+
169+
#EBS Volume
170+
ebs_optimized = false
171+
ebs_volume_enabled = true
172+
ebs_volume_type = "gp2"
173+
ebs_volume_size = 30
174+
175+
#DNS
176+
dns_enabled = false
177+
dns_zone_id = "Z1XJD7SSBKXLC1"
178+
hostname = "ec2"
179+
180+
#Tags
181+
instance_tags = { "snapshot" = true }
182+
183+
# Metadata
184+
metadata_http_tokens_required = "required"
185+
metadata_http_endpoint_enabled = "enabled"
186+
metadata_http_put_response_hop_limit = 2
187+
188+
#Mount EBS With User Data
189+
user_data = file("user-data.sh")
171190
}

0 commit comments

Comments
 (0)