-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
112 lines (93 loc) · 3.22 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
-
----- -
--------- --
--------- - -----
--------- ------ -------
------- --------- ----------
---- ---------- ----------
-- ---------- ----------
Terraform module for generating SSH - ---------- -------
key pair to be used to control login --- ----- ---
login access to AWS EC2 instances -------- -
----------
----------
---------
-----
-
Amazon EC2 Key Pair Parameters
Type: ED25519 / RSA
Length: 4096 / 3072 / 2048
Output Format: OpenSSH public key format
Public Key Algorithm References
RFC4253 The Secure Shell (SSH) Transport Layer Protocol, Section 6.6
https://www.rfc-editor.org/rfc/rfc4253
RFC4716 The Secure Shell (SSH) Public Key File Format
https://www.ietf.org/rfc/rfc4716.txt
RFC8709 Ed25519 and Ed448 Public Key Algorithms for the Secure Shell
(SSH) Protocol
https://www.rfc-editor.org/rfc/rfc8709
*/
resource "random_string" "key_name_suffix" {
keepers = {}
length = 13
special = false
lower = true
min_lower = 8
upper = false
min_upper = 0
numeric = true
min_numeric = 3
}
resource "tls_private_key" "main" {
# Generating public/private SSH key pair
algorithm = var.algorithm
rsa_bits = var.rsa_bits
depends_on = [
random_string.key_name_suffix,
]
}
resource "aws_key_pair" "ssh_key_pair" {
key_name = "${lower(var.algorithm)}-${random_string.key_name_suffix.result}"
# `string` variable or file("path_to_keyfile.pub")
public_key = tls_private_key.main.public_key_openssh
tags = merge(
{
Name = "${lower(var.algorithm)}-${random_string.key_name_suffix.result}"
Resource = "key"
FullName = "${
lower(var.algorithm)
}-${random_string.key_name_suffix.result
}%{if var.domain != null && var.domain != ""}.key${var.domain}%{endif}"
},
var.all_tags
)
depends_on = [
tls_private_key.main,
random_string.key_name_suffix,
]
}
/*
Export private and public `ssh` key-pair to file
*/
resource "local_sensitive_file" "private_key" {
# sensitive data
filename = "${path.root}/${
lower(var.algorithm)}-${random_string.key_name_suffix.result
}"
file_permission = "0600"
content = tls_private_key.main.private_key_openssh
depends_on = [
tls_private_key.main,
]
}
resource "local_file" "public_key" {
filename = "${path.root}/${
lower(var.algorithm)}-${random_string.key_name_suffix.result
}.pub"
file_permission = "0644"
content = trimspace(tls_private_key.main.public_key_openssh)
depends_on = [
tls_private_key.main,
]
}