Skip to content

Commit cbfa7a8

Browse files
authored
Merge pull request #73 from answerdigital/TERRA-53
Added abillity to provide own SSH Key for EC2 module
2 parents 3e3778d + 28fa571 commit cbfa7a8

File tree

6 files changed

+16
-7
lines changed

6 files changed

+16
-7
lines changed

modules/aws/ec2/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ This Terraform module will produce an EC2 instance which can be accessed via ssh
3737
| <a name="input_ami_id"></a> [ami\_id](#input\_ami\_id) | This is the id of the ami image used for the ec2 instance. | `string` | n/a | yes |
3838
| <a name="input_associate_public_ip_address"></a> [associate\_public\_ip\_address](#input\_associate\_public\_ip\_address) | This is a boolean value indicating if a public IP address should be associated with the EC2 instance. | `bool` | `true` | no |
3939
| <a name="input_availability_zone"></a> [availability\_zone](#input\_availability\_zone) | This is the availability zone you want the ec2 instance to be created in. | `string` | n/a | yes |
40+
| <a name="input_custom_key_name"></a> [custom\_key\_name](#input\_custom\_key\_name) | Provide the name of an EC2 key pair to use your own key. By default the SSH key will be managed by this module. | `string` | `""` | no |
4041
| <a name="input_ec2_instance_type"></a> [ec2\_instance\_type](#input\_ec2\_instance\_type) | This is the type of EC2 instance you want. | `string` | `"t2.micro"` | no |
4142
| <a name="input_needs_elastic_ip"></a> [needs\_elastic\_ip](#input\_needs\_elastic\_ip) | This is a boolean value indicating whether an elastic IP should be generated and associated with the EC2 instance. | `bool` | `false` | no |
4243
| <a name="input_owner"></a> [owner](#input\_owner) | This is used to specify the owner of the resources in this module. | `string` | n/a | yes |
@@ -52,7 +53,7 @@ This Terraform module will produce an EC2 instance which can be accessed via ssh
5253
|------|-------------|
5354
| <a name="output_instance_id"></a> [instance\_id](#output\_instance\_id) | This outputs the unique ID of the EC2 instance. |
5455
| <a name="output_instance_public_ip_address"></a> [instance\_public\_ip\_address](#output\_instance\_public\_ip\_address) | 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`. |
55-
| <a name="output_private_key"></a> [private\_key](#output\_private\_key) | This outputs the private key. |
56+
| <a name="output_private_key"></a> [private\_key](#output\_private\_key) | This outputs the self-generated private key - This will not be populated if you provide your own key |
5657
<!-- END_TF_DOCS -->
5758

5859
# Example Usage

modules/aws/ec2/examples/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@
2626

2727
| Name | Description |
2828
|------|-------------|
29+
| <a name="output_generated_private_key"></a> [generated\_private\_key](#output\_generated\_private\_key) | n/a |
2930
| <a name="output_ip"></a> [ip](#output\_ip) | n/a |
30-
| <a name="output_private_key"></a> [private\_key](#output\_private\_key) | n/a |
3131
<!-- END_TF_DOCS -->

modules/aws/ec2/examples/output.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ output "ip" {
22
value = module.ec2_instance_setup.instance_public_ip_address
33
}
44

5-
output "private_key" {
5+
output "generated_private_key" {
66
value = module.ec2_instance_setup.private_key
77
sensitive = true
88
}

modules/aws/ec2/main.tf

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,20 @@ resource "aws_iam_role_policy_attachment" "instance_role" {
4646
}
4747

4848
resource "tls_private_key" "private_key" {
49+
count = var.custom_key_name == "" ? 1 : 0
4950
algorithm = "RSA"
5051
rsa_bits = 4096
5152
}
5253

5354
resource "aws_key_pair" "key_pair" {
55+
count = var.custom_key_name == "" ? 1 : 0
5456
key_name = "${var.project_name}-key-pair"
55-
public_key = tls_private_key.private_key.public_key_openssh
57+
public_key = tls_private_key.private_key[0].public_key_openssh
5658
}
5759

5860
resource "aws_instance" "ec2" {
5961
instance_type = var.ec2_instance_type
60-
key_name = aws_key_pair.key_pair.key_name
62+
key_name = var.custom_key_name == "" ? aws_key_pair.key_pair[0].key_name : var.custom_key_name
6163
ami = var.ami_id
6264
metadata_options {
6365
http_endpoint = "enabled"

modules/aws/ec2/output.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ output "instance_id" {
99
}
1010

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

modules/aws/ec2/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,9 @@ variable "user_data_replace_on_change" {
6363
default = true
6464
description = "This value indicates whether changes to the `user_data` value triggers a rebuild of the EC2 instance."
6565
}
66+
67+
variable "custom_key_name" {
68+
type = string
69+
description = "Provide the name of an EC2 key pair to use your own key. By default the SSH key will be managed by this module."
70+
default = ""
71+
}

0 commit comments

Comments
 (0)