Skip to content

Commit

Permalink
feat: initial version (#1)
Browse files Browse the repository at this point in the history
* code

* docs

* r53-vars

* r53-records

* docs

* docs outputs

* use data.aws_ec2_instance_type
  • Loading branch information
stawii authored May 29, 2024
1 parent 23853fc commit 49b8a5e
Show file tree
Hide file tree
Showing 12 changed files with 422 additions and 119 deletions.
66 changes: 57 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

# terraform-module-template
# terraform-module-aws-instance

## Introduction

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

```hcl
module "module_name" {
source = "github.com/opsd-io/module_name?ref=v0.0.1"
source = "github.com/opsd-io/terraform-module-aws-instance"
# Variables
variable_1 = "foo"
variable_2 = "bar"
name = "my-host"
ami_id = data.aws_ami.debian11.id
instance_type = "t2.micro"
subnet_id = module.network.public_subnet_groups["public1"]["a"].id
key_name = aws_key_pair.me.key_name
}
```

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

## Providers

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

## Modules

No modules.

## Resources

No resources.
| Name | Type |
|------|------|
| [aws_instance.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance) | resource |
| [aws_route53_record.private_cnames](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route53_record) | resource |
| [aws_route53_record.private_ip](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route53_record) | resource |
| [aws_route53_record.public_cnames](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route53_record) | resource |
| [aws_route53_record.public_ip](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route53_record) | resource |
| [aws_ec2_instance_type.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ec2_instance_type) | data source |

## Inputs

No inputs.
| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <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 |
| <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 |
| <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 |
| <a name="input_ebs_optimized"></a> [ebs\_optimized](#input\_ebs\_optimized) | If true, the launched EC2 instance will be EBS-optimized. | `bool` | `null` | no |
| <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 |
| <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 |
| <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 |
| <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 |
| <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 |
| <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 |
| <a name="input_monitoring"></a> [monitoring](#input\_monitoring) | If true, the launched EC2 instance will have detailed monitoring enabled. | `bool` | `false` | no |
| <a name="input_name"></a> [name](#input\_name) | The 'Name' tag of instance. | `string` | n/a | yes |
| <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 |
| <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 |
| <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 |
| <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 |
| <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 |
| <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 |
| <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 |
| <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 |
| <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 |
| <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 |
| <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 |
| <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 |
| <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 |
| <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 |
| <a name="input_subnet_id"></a> [subnet\_id](#input\_subnet\_id) | VPC Subnet ID to launch in. | `string` | n/a | yes |
| <a name="input_user_data"></a> [user\_data](#input\_user\_data) | User data to provide when launching the instance. | `string` | `null` | no |
| <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 |

## Outputs

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

## Examples of usage
Expand Down
48 changes: 48 additions & 0 deletions examples/basic/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
data "aws_ami" "debian11" {
most_recent = true
owners = ["amazon"]

filter {
name = "name"
values = ["debian-12-amd64-*"]
}
}

module "network" {
source = "github.com/opsd-io/terraform-module-aws-network"

vpc_name = "test-vpc"
cidr_block = "10.100.0.0/16"

public_subnet_groups = {
"public1" = {
availability_zones = {
"a" = { cidr_block = "10.100.1.0/24" }
"b" = { cidr_block = "10.100.2.0/24" }
"c" = { cidr_block = "10.100.3.0/24" }
}
}
}
}

resource "aws_key_pair" "me" {
public_key = file("~/.ssh/id_rsa.pub")
}

module "vm_bastion" {
source = "github.com/opsd-io/terraform-module-aws-instance"

name = "bastion"
ami_id = data.aws_ami.debian11.id
instance_type = "t2.micro"
subnet_id = module.network.public_subnet_groups["public1"]["a"].id
key_name = aws_key_pair.me.key_name
}

output "network" {
value = module.network
}

output "vm_bastion" {
value = module.vm_bastion
}
9 changes: 9 additions & 0 deletions examples/basic/override.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Make sure we're using working version (from local directory, not git).

module "vm_bastion" {
source = "./../.."
}

module "network" {
source = "/Users/stawi/work/opsd/terraform-module-aws-network"
}
12 changes: 12 additions & 0 deletions examples/basic/terraform.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}

provider "aws" {
region = "eu-central-1"
}
1 change: 0 additions & 1 deletion examples/example_of_use/.tool-versions

This file was deleted.

63 changes: 0 additions & 63 deletions examples/example_of_use/README.mkdn

This file was deleted.

4 changes: 0 additions & 4 deletions examples/example_of_use/main.tf

This file was deleted.

13 changes: 0 additions & 13 deletions examples/example_of_use/versions.tf

This file was deleted.

98 changes: 97 additions & 1 deletion main.tf
Original file line number Diff line number Diff line change
@@ -1 +1,97 @@
# Terraform code goes here
terraform {
required_version = ">= 1.3.1"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}

data "aws_ec2_instance_type" "main" {
instance_type = var.instance_type
}

resource "aws_instance" "main" {
ami = var.ami_id
instance_type = var.instance_type
ebs_optimized = var.ebs_optimized == null ? null : (var.ebs_optimized && data.aws_ec2_instance_type.main.ebs_optimized_support == "supported")

user_data = var.user_data
user_data_base64 = var.user_data_base64
user_data_replace_on_change = true # !!!!

associate_public_ip_address = var.associate_public_ip_address
iam_instance_profile = var.iam_instance_profile
key_name = var.key_name
monitoring = var.monitoring
source_dest_check = var.source_dest_check
subnet_id = var.subnet_id
vpc_security_group_ids = length(var.security_group_ids) > 0 ? var.security_group_ids : null

# launch_template {}

private_dns_name_options {
hostname_type = var.hostname_type
enable_resource_name_dns_a_record = var.enable_resource_name_dns_a_record
enable_resource_name_dns_aaaa_record = var.enable_resource_name_dns_aaaa_record
}

root_block_device {
delete_on_termination = true
encrypted = var.root_volume_encryption == null ? null : (var.root_volume_encryption && data.aws_ec2_instance_type.main.ebs_encryption_support == "supported")
volume_type = var.root_volume_type
volume_size = var.root_volume_size
iops = var.root_iops
tags = merge(var.common_tags, {
Name = "${var.name}-root"
})
}

# ebs_block_device {}

# ephemeral_block_device {}

tags = merge(var.common_tags, {
Name = var.name
})

}

## Route53 records.

resource "aws_route53_record" "private_ip" {
count = var.private_zone_id != null ? 1 : 0
zone_id = var.private_zone_id
name = coalesce(var.private_zone_record_name, var.name)
type = "A"
ttl = var.private_zone_record_ttl
records = [aws_instance.main.private_ip]
}

resource "aws_route53_record" "public_ip" {
count = var.public_zone_id != null ? 1 : 0
zone_id = var.public_zone_id
name = coalesce(var.public_zone_record_name, var.name)
type = "A"
ttl = var.public_zone_record_ttl
records = [aws_instance.main.public_ip]
}

resource "aws_route53_record" "private_cnames" {
for_each = toset(var.private_zone_id != null ? var.private_zone_record_cnames : [])
zone_id = var.private_zone_id
name = each.value
type = "CNAME"
ttl = var.private_zone_record_ttl
records = [aws_route53_record.private_ip[0].fqdn]
}

resource "aws_route53_record" "public_cnames" {
for_each = toset(var.public_zone_id != null ? var.public_zone_record_cnames : [])
zone_id = var.public_zone_id
name = each.value
type = "CNAME"
ttl = var.public_zone_record_ttl
records = [aws_route53_record.public_ip[0].fqdn]
}
30 changes: 27 additions & 3 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
# output "variable" {
# description = "output variable description"
# value = variable.main.name
# output "instance" {
# value = aws_instance.main
# }

output "id" {
description = "The ID of the instance."
value = aws_instance.main.id
}

output "arn" {
description = "The ARN of the instance."
value = aws_instance.main.arn
}

output "private_ip" {
description = "The private IP of the instance."
value = aws_instance.main.private_ip
}

output "public_ip" {
description = "The public IP of the instance."
value = aws_instance.main.public_ip
}

output "root_volume_id" {
description = "The ID of the root volume."
value = one(aws_instance.main.root_block_device[*].volume_id)
}
Loading

0 comments on commit 49b8a5e

Please sign in to comment.