Skip to content

Commit 49b8a5e

Browse files
authored
feat: initial version (#1)
* code * docs * r53-vars * r53-records * docs * docs outputs * use data.aws_ec2_instance_type
1 parent 23853fc commit 49b8a5e

File tree

12 files changed

+422
-119
lines changed

12 files changed

+422
-119
lines changed

README.md

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Meet **OPSd**. The unique and effortless way of managing cloud infrastructure.
44

5-
# terraform-module-template
5+
# terraform-module-aws-instance
66

77
## Introduction
88

@@ -12,11 +12,13 @@ What does the module provide?
1212

1313
```hcl
1414
module "module_name" {
15-
source = "github.com/opsd-io/module_name?ref=v0.0.1"
15+
source = "github.com/opsd-io/terraform-module-aws-instance"
1616
17-
# Variables
18-
variable_1 = "foo"
19-
variable_2 = "bar"
17+
name = "my-host"
18+
ami_id = data.aws_ami.debian11.id
19+
instance_type = "t2.micro"
20+
subnet_id = module.network.public_subnet_groups["public1"]["a"].id
21+
key_name = aws_key_pair.me.key_name
2022
}
2123
```
2224

@@ -28,26 +30,72 @@ module "module_name" {
2830
| Name | Version |
2931
|------|---------|
3032
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.3.1 |
33+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | ~> 5.0 |
3134

3235
## Providers
3336

34-
No providers.
37+
| Name | Version |
38+
|------|---------|
39+
| <a name="provider_aws"></a> [aws](#provider\_aws) | ~> 5.0 |
3540

3641
## Modules
3742

3843
No modules.
3944

4045
## Resources
4146

42-
No resources.
47+
| Name | Type |
48+
|------|------|
49+
| [aws_instance.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance) | resource |
50+
| [aws_route53_record.private_cnames](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route53_record) | resource |
51+
| [aws_route53_record.private_ip](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route53_record) | resource |
52+
| [aws_route53_record.public_cnames](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route53_record) | resource |
53+
| [aws_route53_record.public_ip](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route53_record) | resource |
54+
| [aws_ec2_instance_type.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ec2_instance_type) | data source |
4355

4456
## Inputs
4557

46-
No inputs.
58+
| Name | Description | Type | Default | Required |
59+
|------|-------------|------|---------|:--------:|
60+
| <a name="input_ami_id"></a> [ami\_id](#input\_ami\_id) | AMI to use for the instance. Overrides the AMI specified in the Launch Template. | `string` | `null` | no |
61+
| <a name="input_associate_public_ip_address"></a> [associate\_public\_ip\_address](#input\_associate\_public\_ip\_address) | Whether to associate a public IP address with an instance in a VPC. Overrides subnet setting. | `bool` | `null` | no |
62+
| <a name="input_common_tags"></a> [common\_tags](#input\_common\_tags) | A map of tags to assign to every resource in this module. | `map(string)` | `{}` | no |
63+
| <a name="input_ebs_optimized"></a> [ebs\_optimized](#input\_ebs\_optimized) | If true, the launched EC2 instance will be EBS-optimized. | `bool` | `null` | no |
64+
| <a name="input_enable_resource_name_dns_a_record"></a> [enable\_resource\_name\_dns\_a\_record](#input\_enable\_resource\_name\_dns\_a\_record) | Indicates whether to respond to DNS queries for instance hostnames with DNS A records. Overrides subnet setting. | `bool` | `null` | no |
65+
| <a name="input_enable_resource_name_dns_aaaa_record"></a> [enable\_resource\_name\_dns\_aaaa\_record](#input\_enable\_resource\_name\_dns\_aaaa\_record) | Indicates whether to respond to DNS queries for instance hostnames with DNS AAAA records. Overrides subnet setting. | `bool` | `null` | no |
66+
| <a name="input_hostname_type"></a> [hostname\_type](#input\_hostname\_type) | Type of hostname for Amazon EC2 instances. Valid values: ip-name and resource-name. Overrides subnet setting. | `string` | `null` | no |
67+
| <a name="input_iam_instance_profile"></a> [iam\_instance\_profile](#input\_iam\_instance\_profile) | IAM Instance Profile to launch the instance with. | `string` | `null` | no |
68+
| <a name="input_instance_type"></a> [instance\_type](#input\_instance\_type) | Instance type to use for the instance. Overrides the instance type specified in the Launch Template. | `string` | `null` | no |
69+
| <a name="input_key_name"></a> [key\_name](#input\_key\_name) | Key name of the Key Pair to use for the instance. | `string` | `null` | no |
70+
| <a name="input_monitoring"></a> [monitoring](#input\_monitoring) | If true, the launched EC2 instance will have detailed monitoring enabled. | `bool` | `false` | no |
71+
| <a name="input_name"></a> [name](#input\_name) | The 'Name' tag of instance. | `string` | n/a | yes |
72+
| <a name="input_private_zone_id"></a> [private\_zone\_id](#input\_private\_zone\_id) | The ID of the hosted zone to contain private IP record. | `string` | `null` | no |
73+
| <a name="input_private_zone_record_cnames"></a> [private\_zone\_record\_cnames](#input\_private\_zone\_record\_cnames) | The CNAMEs of the private zone record. | `list(string)` | `[]` | no |
74+
| <a name="input_private_zone_record_name"></a> [private\_zone\_record\_name](#input\_private\_zone\_record\_name) | The name of the private zone record. Fallback to instance name. | `string` | `null` | no |
75+
| <a name="input_private_zone_record_ttl"></a> [private\_zone\_record\_ttl](#input\_private\_zone\_record\_ttl) | The TTL of the private records. | `number` | `600` | no |
76+
| <a name="input_public_zone_id"></a> [public\_zone\_id](#input\_public\_zone\_id) | The ID of the hosted zone to contain public IP record, if any. | `string` | `null` | no |
77+
| <a name="input_public_zone_record_cnames"></a> [public\_zone\_record\_cnames](#input\_public\_zone\_record\_cnames) | The CNAMEs of the public zone record. | `list(string)` | `[]` | no |
78+
| <a name="input_public_zone_record_name"></a> [public\_zone\_record\_name](#input\_public\_zone\_record\_name) | The name of the public zone record. Fallback to instance name. | `string` | `null` | no |
79+
| <a name="input_public_zone_record_ttl"></a> [public\_zone\_record\_ttl](#input\_public\_zone\_record\_ttl) | The TTL of the public records. | `number` | `600` | no |
80+
| <a name="input_root_iops"></a> [root\_iops](#input\_root\_iops) | Amount of provisioned IOPS. Only valid for root\_volume\_type of io1, io2 or gp3. Defaults to AMI setting. | `number` | `null` | no |
81+
| <a name="input_root_volume_encryption"></a> [root\_volume\_encryption](#input\_root\_volume\_encryption) | Whether to enable root volume encryption. Defaults to AMI setting. | `bool` | `null` | no |
82+
| <a name="input_root_volume_size"></a> [root\_volume\_size](#input\_root\_volume\_size) | Size of the root volume in gibibytes (GiB). Defaults to AMI setting. | `number` | `null` | no |
83+
| <a name="input_root_volume_type"></a> [root\_volume\_type](#input\_root\_volume\_type) | Type of root volume. Defaults to AMI setting. | `string` | `null` | no |
84+
| <a name="input_security_group_ids"></a> [security\_group\_ids](#input\_security\_group\_ids) | List of security group IDs to associate with. | `list(string)` | `[]` | no |
85+
| <a name="input_source_dest_check"></a> [source\_dest\_check](#input\_source\_dest\_check) | Controls if traffic is routed to the instance when the destination address does not match the instance. | `bool` | `true` | no |
86+
| <a name="input_subnet_id"></a> [subnet\_id](#input\_subnet\_id) | VPC Subnet ID to launch in. | `string` | n/a | yes |
87+
| <a name="input_user_data"></a> [user\_data](#input\_user\_data) | User data to provide when launching the instance. | `string` | `null` | no |
88+
| <a name="input_user_data_base64"></a> [user\_data\_base64](#input\_user\_data\_base64) | User data to provide when launching the instance. | `string` | `null` | no |
4789

4890
## Outputs
4991

50-
No outputs.
92+
| Name | Description |
93+
|------|-------------|
94+
| <a name="output_arn"></a> [arn](#output\_arn) | The ARN of the instance. |
95+
| <a name="output_id"></a> [id](#output\_id) | The ID of the instance. |
96+
| <a name="output_private_ip"></a> [private\_ip](#output\_private\_ip) | The private IP of the instance. |
97+
| <a name="output_public_ip"></a> [public\_ip](#output\_public\_ip) | The public IP of the instance. |
98+
| <a name="output_root_volume_id"></a> [root\_volume\_id](#output\_root\_volume\_id) | The ID of the root volume. |
5199
<!-- END_TF_DOCS -->
52100

53101
## Examples of usage

examples/basic/main.tf

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
data "aws_ami" "debian11" {
2+
most_recent = true
3+
owners = ["amazon"]
4+
5+
filter {
6+
name = "name"
7+
values = ["debian-12-amd64-*"]
8+
}
9+
}
10+
11+
module "network" {
12+
source = "github.com/opsd-io/terraform-module-aws-network"
13+
14+
vpc_name = "test-vpc"
15+
cidr_block = "10.100.0.0/16"
16+
17+
public_subnet_groups = {
18+
"public1" = {
19+
availability_zones = {
20+
"a" = { cidr_block = "10.100.1.0/24" }
21+
"b" = { cidr_block = "10.100.2.0/24" }
22+
"c" = { cidr_block = "10.100.3.0/24" }
23+
}
24+
}
25+
}
26+
}
27+
28+
resource "aws_key_pair" "me" {
29+
public_key = file("~/.ssh/id_rsa.pub")
30+
}
31+
32+
module "vm_bastion" {
33+
source = "github.com/opsd-io/terraform-module-aws-instance"
34+
35+
name = "bastion"
36+
ami_id = data.aws_ami.debian11.id
37+
instance_type = "t2.micro"
38+
subnet_id = module.network.public_subnet_groups["public1"]["a"].id
39+
key_name = aws_key_pair.me.key_name
40+
}
41+
42+
output "network" {
43+
value = module.network
44+
}
45+
46+
output "vm_bastion" {
47+
value = module.vm_bastion
48+
}

examples/basic/override.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Make sure we're using working version (from local directory, not git).
2+
3+
module "vm_bastion" {
4+
source = "./../.."
5+
}
6+
7+
module "network" {
8+
source = "/Users/stawi/work/opsd/terraform-module-aws-network"
9+
}

examples/basic/terraform.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
terraform {
2+
required_providers {
3+
aws = {
4+
source = "hashicorp/aws"
5+
version = "~> 5.0"
6+
}
7+
}
8+
}
9+
10+
provider "aws" {
11+
region = "eu-central-1"
12+
}

examples/example_of_use/.tool-versions

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/example_of_use/README.mkdn

Lines changed: 0 additions & 63 deletions
This file was deleted.

examples/example_of_use/main.tf

Lines changed: 0 additions & 4 deletions
This file was deleted.

examples/example_of_use/versions.tf

Lines changed: 0 additions & 13 deletions
This file was deleted.

main.tf

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,97 @@
1-
# Terraform code goes here
1+
terraform {
2+
required_version = ">= 1.3.1"
3+
required_providers {
4+
aws = {
5+
source = "hashicorp/aws"
6+
version = "~> 5.0"
7+
}
8+
}
9+
}
10+
11+
data "aws_ec2_instance_type" "main" {
12+
instance_type = var.instance_type
13+
}
14+
15+
resource "aws_instance" "main" {
16+
ami = var.ami_id
17+
instance_type = var.instance_type
18+
ebs_optimized = var.ebs_optimized == null ? null : (var.ebs_optimized && data.aws_ec2_instance_type.main.ebs_optimized_support == "supported")
19+
20+
user_data = var.user_data
21+
user_data_base64 = var.user_data_base64
22+
user_data_replace_on_change = true # !!!!
23+
24+
associate_public_ip_address = var.associate_public_ip_address
25+
iam_instance_profile = var.iam_instance_profile
26+
key_name = var.key_name
27+
monitoring = var.monitoring
28+
source_dest_check = var.source_dest_check
29+
subnet_id = var.subnet_id
30+
vpc_security_group_ids = length(var.security_group_ids) > 0 ? var.security_group_ids : null
31+
32+
# launch_template {}
33+
34+
private_dns_name_options {
35+
hostname_type = var.hostname_type
36+
enable_resource_name_dns_a_record = var.enable_resource_name_dns_a_record
37+
enable_resource_name_dns_aaaa_record = var.enable_resource_name_dns_aaaa_record
38+
}
39+
40+
root_block_device {
41+
delete_on_termination = true
42+
encrypted = var.root_volume_encryption == null ? null : (var.root_volume_encryption && data.aws_ec2_instance_type.main.ebs_encryption_support == "supported")
43+
volume_type = var.root_volume_type
44+
volume_size = var.root_volume_size
45+
iops = var.root_iops
46+
tags = merge(var.common_tags, {
47+
Name = "${var.name}-root"
48+
})
49+
}
50+
51+
# ebs_block_device {}
52+
53+
# ephemeral_block_device {}
54+
55+
tags = merge(var.common_tags, {
56+
Name = var.name
57+
})
58+
59+
}
60+
61+
## Route53 records.
62+
63+
resource "aws_route53_record" "private_ip" {
64+
count = var.private_zone_id != null ? 1 : 0
65+
zone_id = var.private_zone_id
66+
name = coalesce(var.private_zone_record_name, var.name)
67+
type = "A"
68+
ttl = var.private_zone_record_ttl
69+
records = [aws_instance.main.private_ip]
70+
}
71+
72+
resource "aws_route53_record" "public_ip" {
73+
count = var.public_zone_id != null ? 1 : 0
74+
zone_id = var.public_zone_id
75+
name = coalesce(var.public_zone_record_name, var.name)
76+
type = "A"
77+
ttl = var.public_zone_record_ttl
78+
records = [aws_instance.main.public_ip]
79+
}
80+
81+
resource "aws_route53_record" "private_cnames" {
82+
for_each = toset(var.private_zone_id != null ? var.private_zone_record_cnames : [])
83+
zone_id = var.private_zone_id
84+
name = each.value
85+
type = "CNAME"
86+
ttl = var.private_zone_record_ttl
87+
records = [aws_route53_record.private_ip[0].fqdn]
88+
}
89+
90+
resource "aws_route53_record" "public_cnames" {
91+
for_each = toset(var.public_zone_id != null ? var.public_zone_record_cnames : [])
92+
zone_id = var.public_zone_id
93+
name = each.value
94+
type = "CNAME"
95+
ttl = var.public_zone_record_ttl
96+
records = [aws_route53_record.public_ip[0].fqdn]
97+
}

outputs.tf

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
1-
# output "variable" {
2-
# description = "output variable description"
3-
# value = variable.main.name
1+
# output "instance" {
2+
# value = aws_instance.main
43
# }
4+
5+
output "id" {
6+
description = "The ID of the instance."
7+
value = aws_instance.main.id
8+
}
9+
10+
output "arn" {
11+
description = "The ARN of the instance."
12+
value = aws_instance.main.arn
13+
}
14+
15+
output "private_ip" {
16+
description = "The private IP of the instance."
17+
value = aws_instance.main.private_ip
18+
}
19+
20+
output "public_ip" {
21+
description = "The public IP of the instance."
22+
value = aws_instance.main.public_ip
23+
}
24+
25+
output "root_volume_id" {
26+
description = "The ID of the root volume."
27+
value = one(aws_instance.main.root_block_device[*].volume_id)
28+
}

0 commit comments

Comments
 (0)