Skip to content

Commit caad225

Browse files
Preparing fopr 1.0.0 Release
Cleanup and resource and tag naming
1 parent d20dd54 commit caad225

File tree

14 files changed

+81
-92
lines changed

14 files changed

+81
-92
lines changed

README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ A EC2 module module designed to be practical for casual use.
77
## Features
88

99
1. Gives to ability for create a EC2 Instance
10-
2. Optionally Create all network infrastructure needed for public access
11-
3. Pass custom user data into instance creation
12-
4. SSM and S3 Access Permissions for access and ease of use
13-
5. Creates Key for SSH Access
14-
6. Dynamically Create Ingress Security Rules
15-
7. Provision a Public DNS record for the Red Instance
10+
2. EC2 is already setup for SSM Agent to be installed
11+
3. Creates Key for SSH Access
12+
4. Dynamically Create Ingress Security Rules
13+
5. Optionally create all network infrastructure needed for public access
14+
6. Optionally create public DNS record for the Red Instance
15+
7. Optionally pass user data into instance creation
16+
8. Optionally enabled S3 Bucket IAM Role Access
1617

17-
Contains a useful scripts for setting up AWS CLI for both SUSE and Ubuntu Linux.
18+
> Contains a useful script for setting up AWS CLI and updating OS packages for
19+
> both SUSE and Ubuntu Linux.
1820
1921
```bash
2022
#!/bin/bash
@@ -86,17 +88,17 @@ No modules.
8688
| [aws_iam_instance_profile.red_instance_profile](https://registry.terraform.io/providers/hashicorp/aws/5.57.0/docs/resources/iam_instance_profile) | resource |
8789
| [aws_iam_role.red_role](https://registry.terraform.io/providers/hashicorp/aws/5.57.0/docs/resources/iam_role) | resource |
8890
| [aws_iam_role_policy.s3_bucket_policy](https://registry.terraform.io/providers/hashicorp/aws/5.57.0/docs/resources/iam_role_policy) | resource |
89-
| [aws_iam_role_policy_attachment.ssm_policy_attachment](https://registry.terraform.io/providers/hashicorp/aws/5.57.0/docs/resources/iam_role_policy_attachment) | resource |
91+
| [aws_iam_role_policy_attachment.red_ssm_policy_attachment](https://registry.terraform.io/providers/hashicorp/aws/5.57.0/docs/resources/iam_role_policy_attachment) | resource |
9092
| [aws_instance.red-instance](https://registry.terraform.io/providers/hashicorp/aws/5.57.0/docs/resources/instance) | resource |
91-
| [aws_internet_gateway.main](https://registry.terraform.io/providers/hashicorp/aws/5.57.0/docs/resources/internet_gateway) | resource |
93+
| [aws_internet_gateway.igw](https://registry.terraform.io/providers/hashicorp/aws/5.57.0/docs/resources/internet_gateway) | resource |
9294
| [aws_key_pair.red_key](https://registry.terraform.io/providers/hashicorp/aws/5.57.0/docs/resources/key_pair) | resource |
9395
| [aws_route53_record.red_instance_dns](https://registry.terraform.io/providers/hashicorp/aws/5.57.0/docs/resources/route53_record) | resource |
9496
| [aws_route_table.public](https://registry.terraform.io/providers/hashicorp/aws/5.57.0/docs/resources/route_table) | resource |
9597
| [aws_route_table_association.public](https://registry.terraform.io/providers/hashicorp/aws/5.57.0/docs/resources/route_table_association) | resource |
9698
| [aws_security_group.allow_ssh](https://registry.terraform.io/providers/hashicorp/aws/5.57.0/docs/resources/security_group) | resource |
9799
| [aws_subnet.public](https://registry.terraform.io/providers/hashicorp/aws/5.57.0/docs/resources/subnet) | resource |
98100
| [aws_vpc.main](https://registry.terraform.io/providers/hashicorp/aws/5.57.0/docs/resources/vpc) | resource |
99-
| [local_file.private_key_pem](https://registry.terraform.io/providers/hashicorp/local/latest/docs/resources/file) | resource |
101+
| [local_file.red_private_key_file](https://registry.terraform.io/providers/hashicorp/local/latest/docs/resources/file) | resource |
100102
| [tls_private_key.red_private_key](https://registry.terraform.io/providers/hashicorp/tls/latest/docs/resources/private_key) | resource |
101103
| [aws_ami.red_ami](https://registry.terraform.io/providers/hashicorp/aws/5.57.0/docs/data-sources/ami) | data source |
102104
| [aws_route53_zone.zone](https://registry.terraform.io/providers/hashicorp/aws/5.57.0/docs/data-sources/route53_zone) | data source |

dev/enabled/script.sh

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

red-instance/ec2_iam.tf

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

33
# The role has a trust policy that allows EC2 instances to assume the role.
44
resource "aws_iam_instance_profile" "red_instance_profile" {
5-
name = "${var.project_name}-instance-profile"
5+
name = "${lower(var.project_name)}-red-instance-profile"
66
role = aws_iam_role.red_role.name
77
}
88

99
# Create the IAM role for the Red Instance
1010
resource "aws_iam_role" "red_role" {
11-
name = "${var.project_name}-role"
11+
name = "${lower(var.project_name)}-role"
1212
assume_role_policy = <<EOF
1313
{
1414
"Version": "2012-10-17",
@@ -26,14 +26,14 @@ EOF
2626
}
2727

2828
# Attach the AmazonSSMManagedInstanceCore policy to the role. All Red Instances will have SSM access.
29-
resource "aws_iam_role_policy_attachment" "ssm_policy_attachment" {
29+
resource "aws_iam_role_policy_attachment" "red_ssm_policy_attachment" {
3030
role = aws_iam_role.red_role.name
3131
policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
3232
}
3333

3434
# Role policy for S3 bucket access
3535
resource "aws_iam_role_policy" "s3_bucket_policy" {
36-
name = "${var.project_name}-s3-bucket-policy"
36+
name = "${lower(var.project_name)}-s3-bucket-policy"
3737
role = aws_iam_role.red_role.name
3838

3939
count = var.enable_s3_bucket_policy ? 1 : 0
@@ -47,10 +47,8 @@ resource "aws_iam_role_policy" "s3_bucket_policy" {
4747
"Action": [
4848
"s3:GetObject",
4949
"s3:PutObject",
50-
"s3:ListBucket",
5150
"s3:DeleteObject",
52-
"s3:ListBucket",
53-
"s3:GetBucketLocation"
51+
"s3:ListBucket"
5452
],
5553
"Resource": [
5654
"arn:aws:s3:::${var.s3_bucket_name}",

red-instance/ec2_key.tf

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
# Creates a key pair for the EC2 instance and saves the private key to a file
2+
13
# Create a key pair for the EC2 instance
24
resource "aws_key_pair" "red_key" {
3-
key_name = "${var.project_name}-red-instance-key"
5+
key_name = "${lower(var.project_name)}-red-instance-key"
46
public_key = tls_private_key.red_private_key.public_key_openssh
57

68
tags = merge(
79
{
8-
Name = "${var.project_name}-red-instance-key"
10+
Name = "${lower(var.project_name)}-red-instance-key"
911
},
1012
var.additional_tags,
1113
)
@@ -18,8 +20,8 @@ resource "tls_private_key" "red_private_key" {
1820
}
1921

2022
# Save the private key to a file
21-
resource "local_file" "private_key_pem" {
22-
filename = "${var.project_name}-ec2-key.pem"
23+
resource "local_file" "red_private_key_file" {
24+
filename = "${lower(var.project_name)}-ec2-key.pem"
2325
content = tls_private_key.red_private_key.private_key_pem
2426
file_permission = "0400"
2527
}

red-instance/main.tf

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Contains the main resource block for creating the Red Instance
2+
13
# Provider configuration with default tags
24
provider "aws" {
35
region = var.region
@@ -25,13 +27,13 @@ data "aws_ami" "red_ami" {
2527
values = ["hvm"]
2628
}
2729

28-
owners = [var.ami_owner] # Canonical
30+
owners = [var.ami_owner]
2931
}
3032

3133
# Dynamic block for creating ingress rules
3234
resource "aws_security_group" "allow_ssh" {
3335
vpc_id = var.create_vpc ? aws_vpc.main[0].id : var.vpc_id
34-
name = "${var.project_name}-ingress-sg"
36+
name = "${lower(var.project_name)}-ingress-sg"
3537

3638
# Dynamic ingress rules
3739
dynamic "ingress" {
@@ -43,7 +45,7 @@ resource "aws_security_group" "allow_ssh" {
4345
cidr_blocks = ingress.value.cidr_blocks
4446
}
4547
}
46-
48+
# Allows all egress traffic
4749
egress {
4850
from_port = 0
4951
to_port = 0
@@ -53,7 +55,7 @@ resource "aws_security_group" "allow_ssh" {
5355

5456
tags = merge(
5557
{
56-
Name = "${var.project_name}-Red-Instance"
58+
Name = "${lower(var.project_name)}-red-instance"
5759
},
5860
var.additional_tags,
5961
)
@@ -87,7 +89,7 @@ resource "aws_instance" "red-instance" {
8789

8890
tags = merge(
8991
{
90-
Name = "${var.project_name}-Red-Instance"
92+
Name = "${lower(var.project_name)}-red-instance"
9193
},
9294
var.additional_tags,
9395
)

red-instance/outputs.tf

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
output "vpc_id" {
2-
value = var.create_vpc ? aws_vpc.main[0].id : "VPC was inherited from another module or resource"
3-
description = "The ID of the created VPC"
4-
}
5-
6-
output "subnet_id" {
7-
value = var.create_vpc ? aws_subnet.public[0].id : "Subnet was inherited from another module or resource"
8-
description = "The ID of the created subnet"
9-
}
10-
111
output "key_name" {
122
value = aws_key_pair.red_key.key_name
133
description = "The name of the key pair"
@@ -19,10 +9,20 @@ output "key_fingerprint" {
199
}
2010

2111
output "private_key_path" {
22-
value = local_file.private_key_pem.filename
12+
value = local_file.red_private_key_file.filename
2313
description = "The path to the private key file"
2414
}
2515

16+
output "vpc_id" {
17+
value = var.create_vpc ? aws_vpc.main[0].id : "VPC was inherited from another module or resource"
18+
description = "The ID of the created VPC"
19+
}
20+
21+
output "subnet_id" {
22+
value = var.create_vpc ? aws_subnet.public[0].id : "Subnet was inherited from another module or resource"
23+
description = "The ID of the created subnet"
24+
}
25+
2626
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"

red-instance/public_dns.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ resource "aws_eip" "red_instance_eip" {
1313

1414
tags = merge(
1515
{
16-
Name = "${var.project_name}-red-instance-eip"
16+
Name = "${lower(var.project_name)}-red-instance-eip"
1717
},
1818
var.additional_tags,
1919
)

red-instance/public_vpc.tf

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ resource "aws_vpc" "main" {
77
enable_dns_support = true
88
enable_dns_hostnames = true
99

10-
tags = {
11-
Name = "main-vpc"
12-
}
10+
tags = merge(
11+
{
12+
Name = "${lower(var.project_name)}-red-instance-vpc"
13+
},
14+
var.additional_tags,
15+
)
1316
}
1417

1518
# Create a public subnet
@@ -19,19 +22,25 @@ resource "aws_subnet" "public" {
1922
cidr_block = "10.0.1.0/24"
2023
map_public_ip_on_launch = true
2124

22-
tags = {
23-
Name = "public-subnet"
24-
}
25+
tags = merge(
26+
{
27+
Name = "${lower(var.project_name)}-red-instance-public-subnet"
28+
},
29+
var.additional_tags,
30+
)
2531
}
2632

2733
# Create an internet gateway
28-
resource "aws_internet_gateway" "main" {
34+
resource "aws_internet_gateway" "igw" {
2935
count = var.create_vpc ? 1 : 0
3036
vpc_id = aws_vpc.main[0].id
3137

32-
tags = {
33-
Name = "main-igw"
34-
}
38+
tags = merge(
39+
{
40+
Name = "${lower(var.project_name)}-red-instance-igw"
41+
},
42+
var.additional_tags,
43+
)
3544
}
3645

3746
# Create a route table and associate it with the public subnet
@@ -41,12 +50,15 @@ resource "aws_route_table" "public" {
4150

4251
route {
4352
cidr_block = "0.0.0.0/0"
44-
gateway_id = aws_internet_gateway.main[0].id
53+
gateway_id = aws_internet_gateway.igw[0].id
4554
}
4655

47-
tags = {
48-
Name = "public-route-table"
49-
}
56+
tags = merge(
57+
{
58+
Name = "${lower(var.project_name)}-red-instance-public-route-table"
59+
},
60+
var.additional_tags,
61+
)
5062
}
5163

5264
# Associate the route table with the public subnet

red-instance/variables.tf

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
####################################################################################################
12
# Required Variables
23
variable "project_name" {
34
description = "Set the project name."
@@ -9,7 +10,8 @@ variable "region" {
910
type = string
1011
}
1112

12-
# Optional EC2 Variables
13+
####################################################################################################
14+
# Optional Red Instance Variables
1315
variable "additional_tags" {
1416
description = "Additional tags to apply to the resources"
1517
type = map(string)
@@ -82,7 +84,8 @@ variable "user_data_script_path" {
8284
default = ""
8385
}
8486

85-
# Optional Variables for Infrastructure
87+
####################################################################################################
88+
# Optional Variables for Red Instance Features
8689
variable "create_vpc" {
8790
description = "Controls whether networking resources should be created for public exposed server"
8891
type = bool

red-instance/versions.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Define the required Terraform version and provider version
2+
13
terraform {
24
required_version = "1.9.0"
35
required_providers {

0 commit comments

Comments
 (0)