Skip to content

Cloud - Update Key Pair Module Version Call #420

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
191 changes: 12 additions & 179 deletions README.md

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions data.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
data "aws_ami" "this" {
count = var.ami == null ? 0 : 1
filter {
name = "image-id"
values = [var.ami]
}
}

data "aws_partition" "current" {}

data "aws_ssm_parameter" "this" {
count = local.create && var.ami == null ? 1 : 0

name = var.ami_ssm_parameter
}

data "aws_ami" "selected" {
count = var.ami_os != "override" ? 1 : 0

most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = [local.os_search]
}
filter {
name = "root-device-type"
values = ["ebs"]
}

filter {
name = "virtualization-type"
values = ["hvm"]
}
}
30 changes: 30 additions & 0 deletions examples/customizations/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!-- BEGIN_TF_DOCS -->
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4.66 |

## Providers

No providers.

## Modules

| Name | Source | Version |
|------|--------|---------|
| <a name="module_instance"></a> [instance](#module\_instance) | ../../ | n/a |

## Resources

No resources.

## Inputs

No inputs.

## Outputs

No outputs.
<!-- END_TF_DOCS -->
18 changes: 18 additions & 0 deletions examples/customizations/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module "instance" {
source = "../../"

application = "exampleapp"
environment = "dev"
ami_os = "Amazon_Linux"
instance_type = "r5.large"
key_name = "example_key"
vpc_security_group_ids = ["sg-07b4edce8a1a6eb24"]
subnet_id = "subnet-067f45f707b2dc297"
root_block_device = [
{
encrypted = true
kms_key_id = "arn:aws:kms:us-east-1:521938783116:key/e3203821-6efd-4848-9a8c-50a9990e06cd"
}
]
}

14 changes: 14 additions & 0 deletions examples/customizations/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
terraform {
required_version = ">= 1.0"

required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 4.66"
}
}
}

provider "aws" {
region = "us-east-1"
}
6 changes: 6 additions & 0 deletions key-pair.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module "key-pair" {
source = "app.terraform.io/sccm/key-pair-creation/aws"
version = "0.0.4"

key_pair_name = var.key_name
}
40 changes: 40 additions & 0 deletions locals.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
locals {
create = var.create && var.putin_khuylo
is_t_instance_type = replace(var.instance_type, "/^t(2|3|3a|4g){1}\\..*$/", "1") == "1" ? true : false
ami = try(coalesce(var.ami, try(data.aws_ami.selected[0].id, null), try(nonsensitive(data.aws_ssm_parameter.this[0].value), null)), null)
name = "${var.org}_${var.application}_${local.os_abv}_${var.instance_number}${local.env_abv}"
windows_instance = var.ami != null && var.ami_os == "override" ? (data.aws_ami.this[0].platform != "" ? "WIN" : "LX") : "ovr"
env_abv = lookup(
{
dev = "D",
qa = "Q",
stg = "S",
uat = "U",
prod = "P"
},
var.environment,
var.environment
)
os_abv = lookup(
{
Windows = "WIN",
Amazon_Linux = "AL",
RHEL = "RHEL",
Ubuntu = "UB",
override = local.windows_instance
},
var.ami_os,
var.ami_os
)
os_search = lookup(
{
Windows = "Windows_Server-2025-English-Full-Base-*"
Amazon_Linux = "amzn2-ami-kernel-5.10-hvm-*",
RHEL = "RHEL-9.5.0_HVM-*",
Ubuntu = "ubuntu/images/hvm-ssd-gp3/ubuntu-noble-24.04-amd64-server-*",
override = ""
},
var.ami_os
)
}

36 changes: 10 additions & 26 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
data "aws_partition" "current" {}

locals {
create = var.create && var.putin_khuylo

is_t_instance_type = replace(var.instance_type, "/^t(2|3|3a|4g){1}\\..*$/", "1") == "1" ? true : false

ami = try(coalesce(var.ami, try(nonsensitive(data.aws_ssm_parameter.this[0].value), null)), null)
}

data "aws_ssm_parameter" "this" {
count = local.create && var.ami == null ? 1 : 0

name = var.ami_ssm_parameter
}

################################################################################
# Instance
################################################################################
Expand All @@ -35,7 +19,7 @@ resource "aws_instance" "this" {
subnet_id = var.subnet_id
vpc_security_group_ids = var.vpc_security_group_ids

key_name = var.key_name
key_name = module.key-pair.key_pair_name
monitoring = var.monitoring
get_password_data = var.get_password_data
iam_instance_profile = var.create_iam_instance_profile ? aws_iam_instance_profile.this[0].name : var.iam_instance_profile
Expand Down Expand Up @@ -188,8 +172,8 @@ resource "aws_instance" "this" {
delete = try(var.timeouts.delete, null)
}

tags = merge({ "Name" = var.name }, var.instance_tags, var.tags)
volume_tags = var.enable_volume_tags ? merge({ "Name" = var.name }, var.volume_tags) : null
tags = merge({ "Name" = local.name }, var.instance_tags, var.tags)
volume_tags = var.enable_volume_tags ? merge({ "Name" = local.name }, var.volume_tags) : null
}

################################################################################
Expand All @@ -213,7 +197,7 @@ resource "aws_instance" "ignore_ami" {
subnet_id = var.subnet_id
vpc_security_group_ids = var.vpc_security_group_ids

key_name = var.key_name
key_name = module.key-pair.key_pair_name
monitoring = var.monitoring
get_password_data = var.get_password_data
iam_instance_profile = var.create_iam_instance_profile ? aws_iam_instance_profile.this[0].name : var.iam_instance_profile
Expand Down Expand Up @@ -366,8 +350,8 @@ resource "aws_instance" "ignore_ami" {
delete = try(var.timeouts.delete, null)
}

tags = merge({ "Name" = var.name }, var.instance_tags, var.tags)
volume_tags = var.enable_volume_tags ? merge({ "Name" = var.name }, var.volume_tags) : null
tags = merge({ "Name" = local.name }, var.instance_tags, var.tags)
volume_tags = var.enable_volume_tags ? merge({ "Name" = local.name }, var.volume_tags) : null

lifecycle {
ignore_changes = [
Expand Down Expand Up @@ -397,7 +381,7 @@ resource "aws_spot_instance_request" "this" {
subnet_id = var.subnet_id
vpc_security_group_ids = var.vpc_security_group_ids

key_name = var.key_name
key_name = module.key-pair.key_pair_name
monitoring = var.monitoring
get_password_data = var.get_password_data
iam_instance_profile = var.create_iam_instance_profile ? aws_iam_instance_profile.this[0].name : var.iam_instance_profile
Expand Down Expand Up @@ -540,16 +524,16 @@ resource "aws_spot_instance_request" "this" {
delete = try(var.timeouts.delete, null)
}

tags = merge({ "Name" = var.name }, var.instance_tags, var.tags)
volume_tags = var.enable_volume_tags ? merge({ "Name" = var.name }, var.volume_tags) : null
tags = merge({ "Name" = local.name }, var.instance_tags, var.tags)
volume_tags = var.enable_volume_tags ? merge({ "Name" = local.name }, var.volume_tags) : null
}

################################################################################
# IAM Role / Instance Profile
################################################################################

locals {
iam_role_name = try(coalesce(var.iam_role_name, var.name), "")
iam_role_name = try(coalesce(var.iam_role_name, local.name), "")
}

data "aws_iam_policy_document" "assume_role_policy" {
Expand Down
38 changes: 34 additions & 4 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,29 @@ variable "create" {
default = true
}

variable "name" {
description = "Name to be used on EC2 instance created"
variable "instance_number" {
type = string
default = ""
default = "01"
description = "This is an identifier, not a count"
}

variable "org" {
type = string
default = "sccm"
}

variable "application" {
description = "Identifier to be added to the resources created which represents the application they belong to"
type = string
}

variable "environment" {
type = string
description = "Application environment (dev, qa, stg, uat, prod)"
validation {
condition = contains(["dev", "qa", "stg", "uat", "prod"], var.environment)
error_message = "Valid values for environment: dev, qa, stg, uat, prod"
}
}

variable "ami_ssm_parameter" {
Expand All @@ -16,6 +35,16 @@ variable "ami_ssm_parameter" {
default = "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2"
}

variable "ami_os" {
description = "value"
type = string
default = "override"
validation {
condition = contains(["Windows", "Amazon_Linux", "RHEL", "Ubuntu", "override"], var.ami_os)
error_message = "Valid values for ami_os: Windows, Amazon_Linux, RHEL, Ubuntu, override. If you select override, provide a value for ami variable"
}
}

variable "ami" {
description = "ID of AMI to use for the instance"
type = string
Expand Down Expand Up @@ -109,7 +138,7 @@ variable "host_id" {
variable "iam_instance_profile" {
description = "IAM Instance Profile to launch the instance with. Specified as the name of the Instance Profile"
type = string
default = null
default = "EC2DefaultProfile"
}

variable "instance_initiated_shutdown_behavior" {
Expand Down Expand Up @@ -430,3 +459,4 @@ variable "eip_tags" {
type = map(string)
default = {}
}

Loading