Skip to content

Commit

Permalink
fix: attach ssh public key to ec2 instance
Browse files Browse the repository at this point in the history
Update `ssh` module to provide `key_name` to be appended to ec2 instance
  • Loading branch information
sydrawat01 authored and karanwadhwa committed Oct 21, 2023
1 parent ab424d1 commit 26ad717
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 8 deletions.
1 change: 1 addition & 0 deletions modules/ec2/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ resource "aws_eip_association" "jenkins_server_eip_association" {
resource "aws_instance" "jenkins_server" {
ami = data.aws_ami.jenkins_ami.id
instance_type = var.instance_type
key_name = var.ssh_key_name

network_interface {
network_interface_id = aws_network_interface.jenkins_server_nic.id
Expand Down
9 changes: 6 additions & 3 deletions modules/ec2/userdata.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
cd /etc/caddy/ || exit
sudo mv Caddyfile Caddyfile.backup
touch Caddyfile
echo "${DOMAIN_NAME}" > domain.txt
echo "${DOMAIN_NAME}" >domain.txt

tee -a ./Caddyfile << END
tee -a ./Caddyfile <<END
{
acme_ca https://acme-staging-v02.api.letsencrypt.org/directory
}
${DOMAIN_NAME} {
root * /usr/share/caddy
reverse_proxy localhost:8080
}
END

sudo systemctl restart caddy.service
sudo systemctl restart caddy.service
1 change: 1 addition & 0 deletions modules/ec2/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ variable "public_subnets" {}
variable "domain_name" {}
variable "igw_id" {}
variable "vpc_id" {}
variable "ssh_key_name" {}
3 changes: 3 additions & 0 deletions modules/ssh/output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "ssh_key_name" {
value = aws_key_pair.jenkins_key.key_name
}
11 changes: 6 additions & 5 deletions root/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ module "vpc" {
private_subnets = var.private_subnets
}

module "ssh" {
source = "../modules/ssh"
ssh_key_file = var.ssh_key_file
}

module "ec2" {
source = "../modules/ec2"
root_account_ids = var.root_account_ids
Expand All @@ -17,9 +22,5 @@ module "ec2" {
domain_name = var.domain_name
igw_id = module.vpc.igw_id
vpc_id = module.vpc.vpc_id
}

module "ssh" {
source = "../modules/ssh"
ssh_key_file = var.ssh_key_file
ssh_key_name = module.ssh.ssh_key_name
}

0 comments on commit 26ad717

Please sign in to comment.