Skip to content

Commit 8f5f848

Browse files
author
Nikita Dugar
authored
Merge pull request #1 from clouddrove/ops
Ops
2 parents d83a91f + f6a9c09 commit 8f5f848

File tree

6 files changed

+183
-73
lines changed

6 files changed

+183
-73
lines changed

README.md

+29-20
Original file line numberDiff line numberDiff line change
@@ -72,28 +72,31 @@ This module has a few dependencies:
7272
Here is an example of how you can use this module in your inventory structure:
7373
```hcl
7474
module "ec2" {
75-
source = "git::https://github.com/clouddrove/terraform-aws-ec2.git?ref=tags/0.12.2"
76-
name = "ec2-instance"
77-
application = "clouddrove"
78-
environment = "test"
79-
label_order = ["environment", "name", "application"]
80-
instance_count = 2
81-
ami = "ami-08d658f84a6d84a80"
82-
ebs_optimized = false
83-
instance_type = "t2.nano"
84-
key_name = module.keypair.name
85-
monitoring = false
86-
associate_public_ip_address = true
87-
tenancy = "default"
88-
disk_size = 8
75+
source = "git::https://github.com/clouddrove/terraform-aws-ec2.git?ref=tags/0.12.3"
76+
name = "ec2-instance"
77+
application = "clouddrove"
78+
environment = "test"
79+
label_order = ["environment", "application", "name"] instance_count = 2
80+
ami = "ami-08d658f84a6d84a80"
81+
instance_type = "t2.nano"
82+
key_name = module.keypair.name
83+
monitoring = false
84+
tenancy = "default"
8985
vpc_security_group_ids_list = [module.ssh.security_group_ids, module.http-https.security_group_ids]
9086
subnet_ids = tolist(module.public_subnets.public_subnet_id)
9187
assign_eip_address = true
92-
ebs_volume_enabled = true
93-
ebs_volume_type = "gp2"
94-
ebs_volume_size = 30
95-
user_data = "./_bin/user_data.sh"
96-
instance_tags = {"snapshot"=true}
88+
associate_public_ip_address = true
89+
instance_profile_enabled = true
90+
iam_instance_profile = module.iam-role.name
91+
disk_size = 8
92+
ebs_optimized = false
93+
ebs_volume_enabled = true
94+
ebs_volume_type = "gp2"
95+
ebs_volume_size = 30
96+
user_data = "./_bin/user_data.sh"
97+
instance_tags = { "snapshot" = true }
98+
dns_zone_id = "Z1XJD7SSBKXLC1"
99+
hostname = "ec2"
97100
}
98101
```
99102

@@ -111,12 +114,14 @@ Here is an example of how you can use this module in your inventory structure:
111114
| assign_eip_address | Assign an Elastic IP address to the instance. | bool | `false` | no |
112115
| associate_public_ip_address | Associate a public IP address with the instance. | bool | `true` | no |
113116
| attributes | Additional attributes (e.g. `1`). | list | `<list>` | no |
114-
| availability_zone | Availability Zone the instance is launched in. If not set, will be launched in the first AZ of the region. | string | `` | no |
117+
| availability_zone | Availability Zone the instance is launched in. If not set, will be launched in the first AZ of the region. | list | `<list>` | no |
115118
| cpu_core_count | Sets the number of CPU cores for an instance. | string | `` | no |
116119
| cpu_credits | The credit option for CPU usage. Can be `standard` or `unlimited`. T3 instances are launched as unlimited by default. T2 instances are launched as standard by default. | string | `standard` | no |
117120
| delimiter | Delimiter to be used between `organization`, `environment`, `name` and `attributes`. | string | `-` | no |
118121
| disable_api_termination | If true, enables EC2 Instance Termination Protection. | bool | `false` | no |
119122
| disk_size | Size of the root volume in gigabytes. | number | `8` | no |
123+
| dns_enabled | Flag to control the dns_enable. | bool | `false` | no |
124+
| dns_zone_id | The Zone ID of Route53. | string | `` | no |
120125
| ebs_block_device | Additional EBS block devices to attach to the instance. | list | `<list>` | no |
121126
| ebs_device_name | Name of the EBS device to mount. | list(string) | `<list>` | no |
122127
| ebs_iops | Amount of provisioned IOPS. This must be set with a volume_type of io1. | number | `0` | no |
@@ -127,10 +132,12 @@ Here is an example of how you can use this module in your inventory structure:
127132
| environment | Environment (e.g. `prod`, `dev`, `staging`). | string | `` | no |
128133
| ephemeral_block_device | Customize Ephemeral (also known as Instance Store) volumes on the instance. | list | `<list>` | no |
129134
| host_id | The Id of a dedicated host that the instance will be assigned to. Use when an instance is to be launched on a specific dedicated host. | string | `` | no |
135+
| hostname | DNS records to create. | string | `` | no |
130136
| iam_instance_profile | The IAM Instance Profile to launch the instance with. Specified as the name of the Instance Profile. | string | `` | no |
131137
| instance_count | Number of instances to launch. | number | `1` | no |
132138
| instance_enabled | Flag to control the instance creation. | bool | `true` | no |
133139
| instance_initiated_shutdown_behavior | Shutdown behavior for the instance. | string | `` | no |
140+
| instance_profile_enabled | Flag to control the instance profile creation. | bool | `false` | no |
134141
| instance_tags | Instance tags. | map | `<map>` | no |
135142
| instance_type | The type of instance to start. Updates to this field will trigger a stop/start of the EC2 instance. | string | - | yes |
136143
| ipv6_address_count | Number of IPv6 addresses to associate with the primary network interface. Amazon EC2 chooses the IPv6 addresses from the range of your subnet. | number | `0` | no |
@@ -147,6 +154,8 @@ Here is an example of how you can use this module in your inventory structure:
147154
| subnet_ids | A list of VPC Subnet IDs to launch in. | list(string) | `<list>` | no |
148155
| tags | Additional tags (e.g. map(`BusinessUnit`,`XYZ`). | map | `<map>` | no |
149156
| tenancy | 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. | string | `` | no |
157+
| ttl | The TTL of the record to add to the DNS zone to complete certificate validation. | string | `300` | no |
158+
| type | Type of DNS records to create. | string | `CNAME` | no |
150159
| user_data | The Base64-encoded user data to provide when launching the instances. | string | `` | no |
151160
| vpc_security_group_ids_list | A list of security group IDs to associate with. | list(string) | `<list>` | no |
152161

README.yaml

+22-19
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,30 @@ usage : |-
3636
Here is an example of how you can use this module in your inventory structure:
3737
```hcl
3838
module "ec2" {
39-
source = "git::https://github.com/clouddrove/terraform-aws-ec2.git?ref=tags/0.12.2"
40-
name = "ec2-instance"
41-
application = "clouddrove"
42-
environment = "test"
43-
label_order = ["environment", "name", "application"]
44-
instance_count = 2
45-
ami = "ami-08d658f84a6d84a80"
46-
ebs_optimized = false
47-
instance_type = "t2.nano"
48-
key_name = module.keypair.name
49-
monitoring = false
50-
associate_public_ip_address = true
51-
tenancy = "default"
52-
disk_size = 8
39+
source = "git::https://github.com/clouddrove/terraform-aws-ec2.git?ref=tags/0.12.3"
40+
name = "ec2-instance"
41+
application = "clouddrove"
42+
environment = "test"
43+
label_order = ["environment", "application", "name"] instance_count = 2
44+
ami = "ami-08d658f84a6d84a80"
45+
instance_type = "t2.nano"
46+
key_name = module.keypair.name
47+
monitoring = false
48+
tenancy = "default"
5349
vpc_security_group_ids_list = [module.ssh.security_group_ids, module.http-https.security_group_ids]
5450
subnet_ids = tolist(module.public_subnets.public_subnet_id)
5551
assign_eip_address = true
56-
ebs_volume_enabled = true
57-
ebs_volume_type = "gp2"
58-
ebs_volume_size = 30
59-
user_data = "./_bin/user_data.sh"
60-
instance_tags = {"snapshot"=true}
52+
associate_public_ip_address = true
53+
instance_profile_enabled = true
54+
iam_instance_profile = module.iam-role.name
55+
disk_size = 8
56+
ebs_optimized = false
57+
ebs_volume_enabled = true
58+
ebs_volume_type = "gp2"
59+
ebs_volume_size = 30
60+
user_data = "./_bin/user_data.sh"
61+
instance_tags = { "snapshot" = true }
62+
dns_zone_id = "Z1XJD7SSBKXLC1"
63+
hostname = "ec2"
6164
}
6265
```

_example/.terraform.tfstate.lock.info

-1
This file was deleted.

_example/example.tf

+69-24
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,31 @@ provider "aws" {
33
}
44

55
module "keypair" {
6-
source = "git::https://github.com/clouddrove/terraform-aws-keypair.git?ref=tags/0.12.1"
6+
source = "git::https://github.com/clouddrove/terraform-aws-keypair.git?ref=tags/0.12.2"
77

88
key_path = "~/.ssh/id_rsa.pub"
99
key_name = "main-key"
1010
enable_key_pair = true
1111
}
1212

1313
module "vpc" {
14-
source = "git::https://github.com/clouddrove/terraform-aws-vpc.git?ref=tags/0.12.1"
14+
source = "git::https://github.com/clouddrove/terraform-aws-vpc.git?ref=tags/0.12.4"
1515

1616
name = "vpc"
1717
application = "clouddrove"
1818
environment = "test"
19-
label_order = ["environment", "name", "application"]
19+
label_order = ["environment", "application", "name"]
2020

2121
cidr_block = "172.16.0.0/16"
2222
}
2323

2424
module "public_subnets" {
25-
source = "git::https://github.com/clouddrove/terraform-aws-subnet.git?ref=tags/0.12.1"
25+
source = "git::https://github.com/clouddrove/terraform-aws-subnet.git?ref=tags/0.12.3"
2626

2727
name = "public-subnet"
2828
application = "clouddrove"
2929
environment = "test"
30-
label_order = ["environment", "name", "application"]
30+
label_order = ["environment", "application", "name"]
3131

3232
availability_zones = ["eu-west-1b", "eu-west-1c"]
3333
vpc_id = module.vpc.vpc_id
@@ -37,56 +37,101 @@ module "public_subnets" {
3737
}
3838

3939
module "http-https" {
40-
source = "git::https://github.com/clouddrove/terraform-aws-security-group.git?ref=tags/0.12.1"
40+
source = "git::https://github.com/clouddrove/terraform-aws-security-group.git?ref=tags/0.12.2"
4141

4242
name = "http-https"
4343
application = "clouddrove"
4444
environment = "test"
45-
label_order = ["environment", "name", "application"]
45+
label_order = ["environment", "application", "name"]
4646

4747
vpc_id = module.vpc.vpc_id
4848
allowed_ip = ["0.0.0.0/0"]
4949
allowed_ports = [80, 443]
5050
}
5151

5252
module "ssh" {
53-
source = "git::https://github.com/clouddrove/terraform-aws-security-group.git?ref=tags/0.12.1"
53+
source = "git::https://github.com/clouddrove/terraform-aws-security-group.git?ref=tags/0.12.2"
5454

5555
name = "ssh"
5656
application = "clouddrove"
5757
environment = "test"
58-
label_order = ["environment", "name", "application"]
58+
label_order = ["environment", "application", "name"]
5959

6060
vpc_id = module.vpc.vpc_id
6161
allowed_ip = [module.vpc.vpc_cidr_block]
6262
allowed_ports = [22]
6363
}
6464

65+
module "iam-role" {
66+
source = "git::https://github.com/clouddrove/terraform-aws-iam-role.git?ref=tags/0.12.0"
67+
68+
name = "iam-role"
69+
application = "clouddrove"
70+
environment = "test"
71+
label_order = ["environment", "application", "name"]
72+
assume_role_policy = data.aws_iam_policy_document.default.json
73+
74+
policy_enabled = true
75+
policy = data.aws_iam_policy_document.iam-policy.json
76+
}
77+
78+
data "aws_iam_policy_document" "default" {
79+
statement {
80+
effect = "Allow"
81+
actions = ["sts:AssumeRole"]
82+
principals {
83+
type = "Service"
84+
identifiers = ["ec2.amazonaws.com"]
85+
}
86+
}
87+
}
88+
89+
data "aws_iam_policy_document" "iam-policy" {
90+
statement {
91+
actions = [
92+
"ssm:UpdateInstanceInformation",
93+
"ssmmessages:CreateControlChannel",
94+
"ssmmessages:CreateDataChannel",
95+
"ssmmessages:OpenControlChannel",
96+
"ssmmessages:OpenDataChannel"]
97+
effect = "Allow"
98+
resources = ["*"]
99+
}
100+
}
101+
65102
module "ec2" {
66-
source = "git::https://github.com/clouddrove/terraform-aws-ec2.git?ref=tags/0.12.2"
103+
source = "git::https://github.com/clouddrove/terraform-aws-ec2.git?ref=tags/0.12.3"
67104

68105
name = "ec2-instance"
69106
application = "clouddrove"
70107
environment = "test"
71-
label_order = ["environment", "name", "application"]
72-
73-
instance_count = 2
74-
ami = "ami-08d658f84a6d84a80"
75-
ebs_optimized = false
76-
instance_type = "t2.nano"
77-
key_name = module.keypair.name
78-
monitoring = false
79-
associate_public_ip_address = true
80-
tenancy = "default"
81-
disk_size = 8
108+
label_order = ["environment", "application", "name"]
109+
110+
instance_count = 2
111+
ami = "ami-08d658f84a6d84a80"
112+
instance_type = "t2.nano"
113+
key_name = module.keypair.name
114+
monitoring = false
115+
tenancy = "default"
116+
82117
vpc_security_group_ids_list = [module.ssh.security_group_ids, module.http-https.security_group_ids]
83118
subnet_ids = tolist(module.public_subnets.public_subnet_id)
84119

85-
assign_eip_address = true
120+
assign_eip_address = true
121+
associate_public_ip_address = true
86122

123+
instance_profile_enabled = true
124+
iam_instance_profile = module.iam-role.name
125+
126+
disk_size = 8
127+
ebs_optimized = false
87128
ebs_volume_enabled = true
88129
ebs_volume_type = "gp2"
89130
ebs_volume_size = 30
90-
user_data = "./_bin/user_data.sh"
91-
instance_tags = { "snapshot" = true }
131+
132+
user_data = "./_bin/user_data.sh"
133+
instance_tags = { "snapshot" = true }
134+
135+
dns_zone_id = "Z1XJD7SSBKXLC1"
136+
hostname = "ec2"
92137
}

main.tf

+26-7
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ locals {
1919

2020

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

@@ -38,11 +39,10 @@ resource "aws_instance" "default" {
3839
host_id = var.host_id
3940
cpu_core_count = var.cpu_core_count
4041
user_data = var.user_data != "" ? base64encode(file(var.user_data)) : ""
41-
iam_instance_profile = var.iam_instance_profile
42+
iam_instance_profile = join("", aws_iam_instance_profile.default.*.name)
4243
source_dest_check = var.source_dest_check
4344
ipv6_address_count = var.ipv6_address_count
4445
ipv6_addresses = var.ipv6_addresses
45-
4646
root_block_device {
4747
volume_size = var.disk_size
4848
delete_on_termination = true
@@ -56,15 +56,15 @@ resource "aws_instance" "default" {
5656
module.labels.tags,
5757
{
5858

59-
"Name" = format("%s%s%s", module.labels.id, var.delimiter, (count.index + 1))
59+
"Name" = format("%s%s%s", module.labels.id, var.delimiter, (count.index))
6060
},
6161
var.instance_tags
6262
)
6363

6464
volume_tags = merge(
6565
module.labels.tags,
6666
{
67-
"Name" = format("%s%s%s", module.labels.id, var.delimiter, (count.index + 1))
67+
"Name" = format("%s%s%s", module.labels.id, var.delimiter, (count.index))
6868
}
6969
)
7070

@@ -91,7 +91,7 @@ resource "aws_eip" "default" {
9191
tags = merge(
9292
module.labels.tags,
9393
{
94-
"Name" = format("%s%s%s", module.labels.id, var.delimiter, (count.index + 1))
94+
"Name" = format("%s%s%s-eip", module.labels.id, var.delimiter, (count.index))
9595
}
9696
)
9797
}
@@ -109,7 +109,7 @@ resource "aws_ebs_volume" "default" {
109109
tags = merge(
110110
module.labels.tags,
111111
{
112-
"Name" = format("%s%s%s", module.labels.id, var.delimiter, (count.index + 1))
112+
"Name" = format("%s%s%s", module.labels.id, var.delimiter, (count.index))
113113
}
114114
)
115115
}
@@ -122,4 +122,23 @@ resource "aws_volume_attachment" "default" {
122122
device_name = element(var.ebs_device_name, count.index)
123123
volume_id = element(aws_ebs_volume.default.*.id, count.index)
124124
instance_id = element(aws_instance.default.*.id, count.index)
125+
}
126+
127+
#Module : IAM INSTANCE PROFILE
128+
#Description : Provides an IAM instance profile.
129+
resource "aws_iam_instance_profile" "default" {
130+
count = var.instance_profile_enabled ? 1 : 0
131+
name = format("%s%sinstance-profile", module.labels.id, var.delimiter)
132+
role = var.iam_instance_profile
133+
}
134+
135+
#Module : ROUTE53
136+
#Description : Provides a Route53 record resource.
137+
resource "aws_route53_record" "default" {
138+
count = var.dns_enabled ? var.instance_count : 0
139+
zone_id = var.dns_zone_id
140+
name = format("%s%s%s", var.hostname, var.delimiter, (count.index))
141+
type = var.type
142+
ttl = var.ttl
143+
records = [element(aws_instance.default.*.private_dns, count.index)]
125144
}

0 commit comments

Comments
 (0)