Skip to content

Commit

Permalink
update for new key pair method
Browse files Browse the repository at this point in the history
  • Loading branch information
badra001 committed Aug 8, 2023
1 parent 4a31aae commit c3e262e
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 59 deletions.
8 changes: 4 additions & 4 deletions examples/full-cluster-tf-upgrade/1.25/dns-zone.tf
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ provider "aws" {
# dummy vpc, so we can associate the zone to this account
#---
data "aws_vpc" "dummy_vpc" {
count = var.shared_vpc_label != null ? 1 : 0
count = ! (var.shared_vpc_label == null || var.shared_vpc_label == "") ? 1 : 0
filter {
name = "tag:Name"
values = ["vpc0-dummy"]
Expand All @@ -44,7 +44,7 @@ resource "aws_route53_zone" "cluster_domain" {
force_destroy = false

vpc {
vpc_id = var.shared_vpc_label != null ? try(data.aws_vpc.dummy_vpc[0].id, null) : data.aws_vpc.eks_vpc.id
vpc_id = ! (var.shared_vpc_label == null || var.shared_vpc_label == "") ? try(data.aws_vpc.dummy_vpc[0].id, null) : data.aws_vpc.eks_vpc.id
vpc_region = local.region
}

Expand All @@ -65,7 +65,7 @@ resource "aws_route53_zone" "cluster_domain" {
# need to also associate with network-prod account and this vpc
#---
module "route53_cluster_domain_east" {
count = local.region == "us-gov-east-1" && var.shared_vpc_label != null ? 1 : 0
count = local.region == "us-gov-east-1" && ! (var.shared_vpc_label == null || var.shared_vpc_label == "") ? 1 : 0
providers = {
aws.self = aws
aws.peer = aws.route53_main_east
Expand All @@ -83,7 +83,7 @@ module "route53_cluster_domain_east" {
}

module "route53_cluster_domain_west" {
count = local.region == "us-gov-west-1" && var.shared_vpc_label != null ? 1 : 0
count = local.region == "us-gov-west-1" && ! (var.shared_vpc_label == null || var.shared_vpc_label == "") ? 1 : 0
providers = {
aws.self = aws
aws.peer = aws.route53_main_west
Expand Down
111 changes: 84 additions & 27 deletions examples/full-cluster-tf-upgrade/1.25/ec2-keypair.tf
Original file line number Diff line number Diff line change
@@ -1,36 +1,93 @@
locals {
keypair_name = format("ec2-ssh-%v%v", local._prefixes["eks"], var.cluster_name)
timestamp = formatdate("YYYYMMDD", time_static.timestamp.rfc3339)
}

# two-step process to create
# terraform apply -target=null_resource.generate_keypair
# terraform apply
# when done, add to git
# cd setup
# echo inf-ec2-keypair >> .gitignore
# git-secret add inf-ec2-keypair
# git-secret hide
# git add inf-ec2-keypair.{pub,secret}
# git commit -m'add ec2-keypair: inf-ec2-keypair' inf-ec2-keypair.{pub,secret} .gitignore

# inf-keypair
resource "null_resource" "generate_keypair" {
provisioner "local-exec" {
command = "test -d setup || mkdir setup"
}
provisioner "local-exec" {
working_dir = "./setup"
command = "ssh-keygen -f ${local.keypair_name} -N '' -t rsa -b 2048 -C '${local.keypair_name}@${var.cluster_name}.${local.vpc_domain_name}'"
}
}
resource "time_static" "timestamp" {}

## # two-step process to create
## # terraform apply -target=null_resource.generate_keypair
## # terraform apply
## # when done, add to git
## # cd setup
## # echo inf-ec2-keypair >> .gitignore
## # git-secret add inf-ec2-keypair
## # git-secret hide
## # git add inf-ec2-keypair.{pub,secret}
## # git commit -m'add ec2-keypair: inf-ec2-keypair' inf-ec2-keypair.{pub,secret} .gitignore
##
## # inf-keypair
## resource "null_resource" "generate_keypair" {
## provisioner "local-exec" {
## command = "test -d setup || mkdir setup"
## }
## provisioner "local-exec" {
## working_dir = "./setup"
## command = "ssh-keygen -f ${local.keypair_name} -N '' -t rsa -b 2048 -C '${local.keypair_name}@${var.cluster_name}.${local.vpc_domain_name}'"
## }
## }
##
## resource "aws_key_pair" "cluster_keypair" {
## key_name = local.keypair_name
## public_key = file("setup/${local.keypair_name}.pub")
## depends_on = [null_resource.generate_keypair]
## }
##
## output "cluster_keypair" {
## description = "EC2 keypair for EKS Cluster"
## value = aws_key_pair.cluster_keypair.key_name
## }

module "key_pair" {
source = "terraform-aws-modules/key-pair/aws"

key_name = local.keypair_name
create_private_key = true

resource "aws_key_pair" "cluster_keypair" {
key_name = local.keypair_name
public_key = file("setup/${local.keypair_name}.pub")
depends_on = [null_resource.generate_keypair]
tags = merge(
var.tags,
{
"Name" = local.keypair_name
"launch_time" = time_static.timestamp.rfc3339
"launch_vpc" = local.vpc_label
}
)
}

output "cluster_keypair" {
description = "EC2 keypair for EKS Cluster"
value = aws_key_pair.cluster_keypair.key_name
description = "EC2 Key Pair Name"
value = module.key_pair.key_pair_name
}

resource "local_sensitive_file" "ssh_private_key" {
content = format("%v\n", module.key_pair.private_key_openssh)
directory_permission = "0700"
file_permission = "0600"
filename = format("%v/%v", null_resource.setup_directory.triggers.directory, null_resource.setup_directory.triggers.keypair_name)
}

resource "local_sensitive_file" "ssh_public_key" {
content = format("%v\n", module.key_pair.public_key_openssh)
directory_permission = "0700"
file_permission = "0600"
filename = format("%v/%v.pub", null_resource.setup_directory.triggers.directory, null_resource.setup_directory.triggers.keypair_name)
}

resource "local_file" "gitignore" {
content = format("%v\n", basename(local_sensitive_file.ssh_private_key.filename))
directory_permission = "0700"
file_permission = "0600"
filename = format("%v/%v", null_resource.setup_directory.triggers.directory, ".gitignore")
}

resource "local_sensitive_file" "gitsecret_script" {
content = templatefile("${path.module}/templates/manage-git-secret.sh.tpl", {
ssh_key_directory = null_resource.setup_directory.triggers.directory
ssh_private_key_filename = local_sensitive_file.ssh_private_key[0].filename
ssh_public_key_filename = local_sensitive_file.ssh_public_key[0].filename
})
directory_permission = "0700"
file_permission = "0755"
filename = format("%v/%v", null_resource.setup_directory.triggers.directory, "manage-git-secret.sh")
}

3 changes: 2 additions & 1 deletion examples/full-cluster-tf-upgrade/1.25/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ resource "aws_launch_template" "eks-nodegroup" {
instance_type = var.eks_instance_type
name = format("%v%v-launch-template", local._prefixes["eks"], var.cluster_name)
update_default_version = true
key_name = aws_key_pair.cluster_keypair.key_name
# key_name = aws_key_pair.cluster_keypair.key_name
key_name = module.key_pair.key_pair_name
# vpc_security_group_ids = [aws_security_group.additional_eks_cluster_sg.id]
vpc_security_group_ids = [aws_security_group.extra_cluster_sg.id]

Expand Down
11 changes: 6 additions & 5 deletions examples/full-cluster-tf-upgrade/1.25/tf-run.data
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION 1.4.4
VERSION 1.4.5
REMOTE-STATE
COMMENT make sure the private-lb subnet and container subnets are tagged properly (see README.md)
STOP then continue with at step %%NEXT%% (tag:subnets-verified)
Expand Down Expand Up @@ -31,11 +31,12 @@ TAG setup-complete
POLICY

TAG ec2-key
COMMENT EC2 key pairs
null_resource.generate_keypair
aws_key_pair.cluster_keypair
module.cluster_key_pair time_static.timestamp local_sensitive_file.ssh_private_key local_sensitive_file.ssh_public_key local_file.gitignore local_sensitive_file.gitsecret_script
## null_resource.generate_keypair
## aws_key_pair.cluster_keypair

COMMAND tf-directory-setup.py -l s3
COMMENT be sure to add the setup/ec2-ssh-eks-{cluster} to git-secret, git-secret hide, add the setup/*secret and setup/*pub got git, and commit the entirety of the change
## COMMENT be sure to add the setup/ec2-ssh-eks-{cluster} to git-secret, git-secret hide, add the setup/*secret and setup/*pub got git, and commit the entirety of the change

TAG dns-zone
aws_route53_zone.cluster_domain
Expand Down
20 changes: 16 additions & 4 deletions examples/full-cluster-tf-upgrade/1.25/tf-run.destroy.data
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
VERSION 1.0.1
VERSION 1.0.2
BACKUP-STATE
COMMAND tf-init
COMMAND tf-state list

aws_route53_zone.cluster_domain

aws_eks_node_group.eks-nodegroup aws_eks_cluster.eks_cluster aws_iam_openid_connect_provider.oidc aws_launch_template.eks-nodegroup
aws_key_pair.cluster_keypair
null_resource.generate_keypair

## aws_key_pair.cluster_keypair
## null_resource.generate_keypair

module.cluster_key_pair time_static.timestamp local_sensitive_file.ssh_private_key local_sensitive_file.ssh_public_key local_file.gitignore local_sensitive_file.gitsecret_script

module.role_cluster-admin module.role_eks-cluster module.role_eks-nodegroup
module.group_cluster-admin.aws_iam_group.this

POLICY
aws_security_group.additional_eks_cluster_sg aws_security_group.all_worker_mgmt

aws_security_group.additional_eks_cluster_sg aws_security_group.all_worker_mgmt aws_security_group.extra_cluster_sg
null_resource.cluster_roles["eks-console-full-access"] null_resource.cluster_roles["eks-console-restricted-access"]

ALL
Expand All @@ -23,3 +30,8 @@ ALL
## NO ./aws-auth/tf-run.destroy.data
## ./tf-run.destroy.data



TAG ec2-key
## null_resource.generate_keypair
## aws_key_pair.cluster_keypair

This file was deleted.

This file was deleted.

0 comments on commit c3e262e

Please sign in to comment.