Skip to content

Commit d20dd54

Browse files
Added public dns feature
1 parent c95cfdb commit d20dd54

File tree

8 files changed

+115
-15
lines changed

8 files changed

+115
-15
lines changed

README.md

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,50 @@ A EC2 module module designed to be practical for casual use.
1212
4. SSM and S3 Access Permissions for access and ease of use
1313
5. Creates Key for SSH Access
1414
6. Dynamically Create Ingress Security Rules
15-
16-
Contains a useful scripts for setting up AWS CLI for both SUSE and Ubuntu Linux
15+
7. Provision a Public DNS record for the Red Instance
16+
17+
Contains a useful scripts for setting up AWS CLI for both SUSE and Ubuntu Linux.
18+
19+
```bash
20+
#!/bin/bash
21+
22+
# Install AWS CLI v2 on SUSE and Ubuntu Linux (x86_64 and arm)
23+
24+
# Check the Linux distribution
25+
if [[ -f /etc/os-release ]]; then
26+
source /etc/os-release
27+
if [[ $ID == "ubuntu" ]]; then
28+
# Update Ubuntu and install unzip
29+
sudo apt update
30+
sudo apt upgrade -y
31+
sudo apt install -y unzip
32+
elif [[ $ID == "suse" ]]; then
33+
# Update SUSE and install unzip
34+
sudo zypper refresh
35+
sudo zypper update -y
36+
sudo zypper install -y unzip
37+
else
38+
echo "Unsupported Linux distribution: $ID"
39+
exit 1
40+
fi
41+
else
42+
echo "Unable to determine Linux distribution"
43+
exit 1
44+
fi
45+
46+
# Check the architecture
47+
if [[ $(uname -m) == "x86_64" ]]; then
48+
# Install AWS CLI for x86_64
49+
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
50+
unzip awscliv2.zip
51+
sudo ./aws/install
52+
else
53+
# Install AWS CLI for arm
54+
curl "https://awscli.amazonaws.com/awscli-exe-linux-aarch64.zip" -o "awscliv2.zip"
55+
unzip awscliv2.zip
56+
sudo ./aws/install
57+
fi
58+
```
1759

1860
<!-- prettier-ignore-start -->
1961
<!-- BEGIN_TF_DOCS -->
@@ -48,6 +90,7 @@ No modules.
4890
| [aws_instance.red-instance](https://registry.terraform.io/providers/hashicorp/aws/5.57.0/docs/resources/instance) | resource |
4991
| [aws_internet_gateway.main](https://registry.terraform.io/providers/hashicorp/aws/5.57.0/docs/resources/internet_gateway) | resource |
5092
| [aws_key_pair.red_key](https://registry.terraform.io/providers/hashicorp/aws/5.57.0/docs/resources/key_pair) | resource |
93+
| [aws_route53_record.red_instance_dns](https://registry.terraform.io/providers/hashicorp/aws/5.57.0/docs/resources/route53_record) | resource |
5194
| [aws_route_table.public](https://registry.terraform.io/providers/hashicorp/aws/5.57.0/docs/resources/route_table) | resource |
5295
| [aws_route_table_association.public](https://registry.terraform.io/providers/hashicorp/aws/5.57.0/docs/resources/route_table_association) | resource |
5396
| [aws_security_group.allow_ssh](https://registry.terraform.io/providers/hashicorp/aws/5.57.0/docs/resources/security_group) | resource |
@@ -56,6 +99,7 @@ No modules.
5699
| [local_file.private_key_pem](https://registry.terraform.io/providers/hashicorp/local/latest/docs/resources/file) | resource |
57100
| [tls_private_key.red_private_key](https://registry.terraform.io/providers/hashicorp/tls/latest/docs/resources/private_key) | resource |
58101
| [aws_ami.red_ami](https://registry.terraform.io/providers/hashicorp/aws/5.57.0/docs/data-sources/ami) | data source |
102+
| [aws_route53_zone.zone](https://registry.terraform.io/providers/hashicorp/aws/5.57.0/docs/data-sources/route53_zone) | data source |
59103

60104
## Inputs
61105

@@ -65,9 +109,12 @@ No modules.
65109
| <a name="input_allocate_eip"></a> [allocate\_eip](#input\_allocate\_eip) | Controls whether an Elastic IP should be allocated | `bool` | `true` | no |
66110
| <a name="input_ami_name"></a> [ami\_name](#input\_ami\_name) | The name of the AMI to use for the instance | `string` | `"ubuntu/images/hvm-ssd-gp3/ubuntu-noble-24.04-amd64-server-20240701.1"` | no |
67111
| <a name="input_ami_owner"></a> [ami\_owner](#input\_ami\_owner) | The owner of the AMI to use for the instance | `string` | `"099720109477"` | no |
112+
| <a name="input_apex_domain"></a> [apex\_domain](#input\_apex\_domain) | The apex domain to use for the public DNS record | `string` | `""` | no |
68113
| <a name="input_create_vpc"></a> [create\_vpc](#input\_create\_vpc) | Controls whether networking resources should be created for public exposed server | `bool` | `true` | no |
69114
| <a name="input_disable_api_stop"></a> [disable\_api\_stop](#input\_disable\_api\_stop) | Controls whether API stop is disabled | `bool` | `false` | no |
70115
| <a name="input_disable_api_termination"></a> [disable\_api\_termination](#input\_disable\_api\_termination) | Controls whether API termination is disabled | `bool` | `false` | no |
116+
| <a name="input_dns_name"></a> [dns\_name](#input\_dns\_name) | The DNS name to use for the public DNS record | `string` | `""` | no |
117+
| <a name="input_enable_public_dns"></a> [enable\_public\_dns](#input\_enable\_public\_dns) | Controls whether a public DNS record should be created | `bool` | `false` | no |
71118
| <a name="input_enable_s3_bucket_policy"></a> [enable\_s3\_bucket\_policy](#input\_enable\_s3\_bucket\_policy) | Controls whether an S3 bucket policy should be attached to the instance role | `bool` | `false` | no |
72119
| <a name="input_ingress_rules"></a> [ingress\_rules](#input\_ingress\_rules) | List of ingress rules | <pre>list(object({<br> from_port = number<br> to_port = number<br> protocol = string<br> cidr_blocks = list(string)<br> }))</pre> | <pre>[<br> {<br> "cidr_blocks": [<br> "0.0.0.0/0"<br> ],<br> "from_port": 22,<br> "protocol": "tcp",<br> "to_port": 22<br> }<br>]</pre> | no |
73120
| <a name="input_instance_type"></a> [instance\_type](#input\_instance\_type) | The instance type to use for the instance | `string` | `"t2.micro"` | no |
@@ -86,6 +133,7 @@ No modules.
86133
| <a name="output_key_fingerprint"></a> [key\_fingerprint](#output\_key\_fingerprint) | The fingerprint of the key pair |
87134
| <a name="output_key_name"></a> [key\_name](#output\_key\_name) | The name of the key pair |
88135
| <a name="output_private_key_path"></a> [private\_key\_path](#output\_private\_key\_path) | The path to the private key file |
136+
| <a name="output_public_dns"></a> [public\_dns](#output\_public\_dns) | The public DNS name of the instance |
89137
| <a name="output_public_ip"></a> [public\_ip](#output\_public\_ip) | The public IP address of the instance |
90138
| <a name="output_subnet_id"></a> [subnet\_id](#output\_subnet\_id) | The ID of the created subnet |
91139
| <a name="output_vpc_id"></a> [vpc\_id](#output\_vpc\_id) | The ID of the created VPC |

dev/disabled/main.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,7 @@ output "private_key_path" {
5151
output "public_ip" {
5252
value = module.red-instance.public_ip
5353
}
54+
55+
output "public_dns" {
56+
value = module.red-instance.public_dns
57+
}

dev/enabled/main.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ module "red-instance" {
2020
user_data_script_path = "script.sh"
2121
enable_s3_bucket_policy = true
2222
s3_bucket_name = "red-drop-s3"
23+
enable_public_dns = true
24+
apex_domain = "rag-space.com"
25+
dns_name = "red-instance.rag-space.com"
26+
2327
additional_tags = {
2428
Environment = "Has-VPC"
2529
}
@@ -49,3 +53,7 @@ output "private_key_path" {
4953
output "public_ip" {
5054
value = module.red-instance.public_ip
5155
}
56+
57+
output "public_dns" {
58+
value = module.red-instance.public_dns
59+
}

red-instance/main.tf

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,3 @@ resource "aws_instance" "red-instance" {
9292
var.additional_tags,
9393
)
9494
}
95-
96-
# Resource block for allocating an Elastic IP address (optional)
97-
resource "aws_eip" "red_instance_eip" {
98-
count = var.allocate_eip ? 1 : 0
99-
instance = aws_instance.red-instance.id
100-
101-
tags = merge(
102-
{
103-
Name = "${var.project_name}-red-instance-eip"
104-
},
105-
var.additional_tags,
106-
)
107-
}

red-instance/outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,8 @@ output "public_ip" {
2727
value = var.allocate_eip ? aws_eip.red_instance_eip[0].public_ip : "Public IP not allocated"
2828
description = "The public IP address of the instance"
2929
}
30+
31+
output "public_dns" {
32+
value = var.enable_public_dns ? aws_route53_record.red_instance_dns[0].fqdn : "Public DNS not allocated"
33+
description = "The public DNS name of the instance"
34+
}

red-instance/public_dns.tf

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# This Terraform file creates a public DNS record for the red-instance EC2 instance.
2+
3+
# Data source block for getting the Route 53 zone
4+
data "aws_route53_zone" "zone" {
5+
count = var.enable_public_dns ? 1 : 0
6+
name = var.apex_domain
7+
}
8+
9+
# Resource block for allocating an Elastic IP address (optional)
10+
resource "aws_eip" "red_instance_eip" {
11+
count = var.allocate_eip ? 1 : 0
12+
instance = aws_instance.red-instance.id
13+
14+
tags = merge(
15+
{
16+
Name = "${var.project_name}-red-instance-eip"
17+
},
18+
var.additional_tags,
19+
)
20+
}
21+
22+
# Resource block for creating a public DNS record
23+
resource "aws_route53_record" "red_instance_dns" {
24+
count = var.enable_public_dns ? 1 : 0
25+
zone_id = data.aws_route53_zone.zone[count.index].zone_id
26+
name = var.dns_name
27+
type = "A"
28+
ttl = "300"
29+
records = [aws_eip.red_instance_eip[count.index].public_ip]
30+
}

red-instance/variables.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,21 @@ variable "enable_s3_bucket_policy" {
112112
type = bool
113113
default = false
114114
}
115+
116+
variable "enable_public_dns" {
117+
description = "Controls whether a public DNS record should be created"
118+
type = bool
119+
default = false
120+
}
121+
122+
variable "apex_domain" {
123+
description = "The apex domain to use for the public DNS record"
124+
type = string
125+
default = ""
126+
}
127+
128+
variable "dns_name" {
129+
description = "The DNS name to use for the public DNS record"
130+
type = string
131+
default = ""
132+
}

0 commit comments

Comments
 (0)