diff --git a/.gitignore b/.gitignore index 7a3e2fd..5ead60d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ # Local .terraform directories **/.terraform/* +**/.terraform.lock.hcl # .tfstate files *.tfstate diff --git a/.tflint.hcl b/.tflint.hcl new file mode 100644 index 0000000..c366055 --- /dev/null +++ b/.tflint.hcl @@ -0,0 +1,37 @@ +rule "terraform_unused_declarations" { + enabled = true +} + +rule "terraform_deprecated_index" { + enabled = true +} + +rule "terraform_documented_variables" { + enabled = true +} + +rule "terraform_typed_variables" { + enabled = true +} + +rule "terraform_module_pinned_source" { + enabled = true +} + +rule "terraform_naming_convention" { + enabled = true +} + +rule "terraform_unused_required_providers" { + enabled = true +} + +rule "terraform_required_version" { + enabled = true +} + +plugin "aws" { + enabled = true + source = "github.com/terraform-linters/tflint-ruleset-aws" + version = "0.12.0" +} diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..6fa168f --- /dev/null +++ b/main.tf @@ -0,0 +1,24 @@ +locals { + + create_new_ssh_key = var.create_ssh_keypair && (length(var.ssh_public_key) > 0) + +} + +# Generate SSH keypair +resource "tls_private_key" "generated_ssh_key_pair" { + count = local.create_new_ssh_key ? 1 : 0 + algorithm = "RSA" +} + +resource "aws_key_pair" "ssh" { + count = var.create_ssh_keypair ? 1 : 0 + + key_name_prefix = var.name + public_key = try(tls_private_key.generated_ssh_key_pair[0].public_key_openssh, var.ssh_public_key) + + tags = merge( + { "Name" = var.name }, + var.tags, + var.key_pair_tags, + ) +} diff --git a/outputs.tf b/outputs.tf new file mode 100644 index 0000000..e69de29 diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..fa58da8 --- /dev/null +++ b/variables.tf @@ -0,0 +1,62 @@ +variable "name" { + description = "Name to be used on all the resources as identifier" + type = string + default = "" +} + +variable "cloud_provider" { + description = "The cloud provider this module will be used against." + type = string + default = "aws" + validation { + condition = contains(["aws"], var.cloud_provider) + error_message = "Allowed values for input_parameter are \"aws\"." + } +} + +variable "create_ssh_keypair" { + description = "Controls if SSH Keypair should be created." + type = bool + default = true +} + +variable "ssh_public_key" { + description = "If \"create_ssh_keypair\" is set to true, use this variable if you want to use a pre-existing SSH Public Key. If not specified a new one will be created." + type = string + default = "" + validation { + condition = length(var.ssh_public_key) == 0 || can(regex("(AAAAB3NzaC1yc2EA|AAAAC3NzaC1lZDI1NTE5)", var.ssh_public_key)) + error_message = "An invalid SSH key has been specified in \"var.ssh_public_key\". Please check https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html for instructions." + } +} + +variable "ssh_key_pair" { + description = "If \"create_ssh_keypair\" is set to false, use this variable to specify a pre-existing cloud key-pair. Mutually exclusive with \"create_ssh_keypair\"." + type = string + default = "" +} + +variable "tags" { + description = "A map of tags to add to all resources" + type = map(string) + default = {} +} + +variable "key_pair_tags" { + description = "Additional tags for the Key Pair" + type = map(string) + default = {} +} + +variable "cloudinit_packages" { + description = "A list of packages required by cloud-init to perform the software launch." + type = list(string) + default = [ + "awscli", + "jq", + "unzip", + "python3-pip", + "python3-venv", + "python3-docker", + ] +} diff --git a/versions.tf b/versions.tf new file mode 100644 index 0000000..5a9fd0f --- /dev/null +++ b/versions.tf @@ -0,0 +1,10 @@ +terraform { + required_version = ">= 0.13.1" + + required_providers { + aws = { + source = "hashicorp/aws" + version = ">= 3.63" + } + } +}