File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -128,12 +128,26 @@ jobs:
128128 steps :
129129 - name : Checkout repo
130130 uses : actions/checkout@v4
131+
132+ - name : Install SSH tools
133+ run : sudo apt-get install -y openssh-client
131134
132- - name : Setup SSH private key
135+ - name : Write private key to file
133136 run : |
134- mkdir -p ~/.ssh
135- echo "${{ secrets.AWS_EC2_SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
136- chmod 600 ~/.ssh/id_rsa
137+ echo "${{ secrets.AWS_EC2_SSH_PRIVATE_KEY }}" > private-key.pem
138+ chmod 600 private-key.pem
139+
140+
141+ - name : Generate public key
142+ run : |
143+ ssh-keygen -y -f private-key.pem > public-key.pub
144+ cat public-key.pub # optional, for debug
145+
146+ # - name: Setup SSH private key
147+ # run: |
148+ # mkdir -p ~/.ssh
149+ # echo "${{ secrets.AWS_EC2_SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
150+ # chmod 600 ~/.ssh/id_rsa
137151
138152 - name : Setup Terraform
139153 uses : hashicorp/setup-terraform@v2
@@ -154,7 +168,7 @@ jobs:
154168 AWS_ACCESS_KEY_ID : ${{ secrets.AWS_ACCESS_KEY_ID }}
155169 AWS_SECRET_ACCESS_KEY : ${{ secrets.AWS_SECRET_ACCESS_KEY }}
156170 AWS_REGION : us-east-1
157- run : terraform plan -out=tfplan
171+ run : terraform plan -var="public_key=$(cat public-key.pub)" - out=tfplan
158172
159173 - name : Terraform Apply
160174 working-directory : ./terraform
Original file line number Diff line number Diff line change @@ -3,8 +3,8 @@ provider "aws" {
33}
44
55resource "aws_key_pair" "deployer" {
6- key_name = " client-app "
7- public_key = file ( var. public_key_path )
6+ key_name = " ec2 "
7+ public_key = var. public_key
88}
99
1010resource "aws_instance" "react_app" {
Original file line number Diff line number Diff line change 1- variable "public_key_path" {
2- description = " Path to your public SSH key"
3- # default = "~/.ssh/id_rsa.pub"
4- default = " /home/runner/.ssh/id_rsa.pub"
1+ variable "public_key" {
2+ type = string
3+ description = " public SSH key"
54}
65
76variable "ami_id" {
You can’t perform that action at this time.
0 commit comments