Skip to content

Commit

Permalink
Update ec2 resource names inline with issue #89.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeCSykes committed Aug 4, 2023
1 parent 884b04d commit 364c60d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
26 changes: 13 additions & 13 deletions modules/aws/ec2/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ terraform {
}
}

resource "aws_iam_instance_profile" "instance_profile" {
resource "aws_iam_instance_profile" "this" {
name = "${var.project_name}-ec2-monitoring-and-setup"
role = aws_iam_role.instance_role.name
role = aws_iam_role.this.name
}

resource "aws_iam_role" "instance_role" {
resource "aws_iam_role" "this" {
name = "${var.project_name}-ec2-monitoring-and-setup"
assume_role_policy = <<-EOF
{
Expand All @@ -36,30 +36,30 @@ resource "aws_iam_role" "instance_role" {
EOF
}

resource "aws_iam_role_policy_attachment" "instance_role" {
resource "aws_iam_role_policy_attachment" "this" {
for_each = toset([
"arn:aws:iam::aws:policy/service-role/AmazonEC2RoleforSSM",
"arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy"
])
role = aws_iam_role.instance_role.name
role = aws_iam_role.this.name
policy_arn = each.value
}

resource "tls_private_key" "private_key" {
resource "tls_private_key" "this" {
count = var.custom_key_name == "" ? 1 : 0
algorithm = "RSA"
rsa_bits = 4096
}

resource "aws_key_pair" "key_pair" {
resource "aws_key_pair" "this" {
count = var.custom_key_name == "" ? 1 : 0
key_name = "${var.project_name}-key-pair"
public_key = tls_private_key.private_key[0].public_key_openssh
public_key = tls_private_key.this[0].public_key_openssh
}

resource "aws_instance" "ec2" {
resource "aws_instance" "this" {
instance_type = var.ec2_instance_type
key_name = var.custom_key_name == "" ? aws_key_pair.key_pair[0].key_name : var.custom_key_name
key_name = var.custom_key_name == "" ? aws_key_pair.this[0].key_name : var.custom_key_name
ami = var.ami_id
metadata_options {
http_endpoint = "enabled"
Expand All @@ -74,7 +74,7 @@ resource "aws_instance" "ec2" {
vpc_security_group_ids = var.vpc_security_group_ids
associate_public_ip_address = var.associate_public_ip_address

iam_instance_profile = aws_iam_instance_profile.instance_profile.name
iam_instance_profile = aws_iam_instance_profile.this.name

user_data = var.user_data
user_data_replace_on_change = var.user_data_replace_on_change
Expand All @@ -85,10 +85,10 @@ resource "aws_instance" "ec2" {
}
}

resource "aws_eip" "public_elastic_ip" {
resource "aws_eip" "this" {
count = var.needs_elastic_ip == true ? 1 : 0

instance = aws_instance.ec2.id
instance = aws_instance.this.id
domain = "vpc"

tags = {
Expand Down
6 changes: 3 additions & 3 deletions modules/aws/ec2/output.tf
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
output "instance_public_ip_address" {
value = aws_eip.public_elastic_ip[0].public_ip
value = aws_eip.this[0].public_ip
description = "This outputs the public IP associated with the EC2 instance. Note that this output will be the same as the elastic IP if `needs_elastic_ip` is set to `true`. This output is of type `string`."
}

output "instance_id" {
value = aws_instance.ec2.id
value = aws_instance.this.id
description = "This outputs the unique ID of the EC2 instance."
}

output "private_key" {
value = tls_private_key.private_key[0].private_key_pem
value = tls_private_key.this[0].private_key_pem
description = "This outputs the self-generated private key - This will not be populated if you provide your own key"
sensitive = true
}

0 comments on commit 364c60d

Please sign in to comment.