Skip to content

Commit bbe8ef4

Browse files
feat: aws deployment with terraform
1 parent 4b3366e commit bbe8ef4

File tree

7 files changed

+443
-0
lines changed

7 files changed

+443
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
name: Terraform Deploy
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
aws_access_key_id:
7+
description: "AWS Access Key ID from Learner Lab"
8+
required: true
9+
type: string
10+
aws_secret_access_key:
11+
description: "AWS Secret Access Key from Learner Lab"
12+
required: true
13+
type: string
14+
aws_session_token:
15+
description: "AWS Session Token from Learner Lab (if required)"
16+
required: false
17+
type: string
18+
push:
19+
branches:
20+
- feature/deploy-images-aws
21+
22+
jobs:
23+
deploy:
24+
runs-on: ubuntu-latest
25+
environment: AWS
26+
27+
steps:
28+
- name: Checkout Code
29+
uses: actions/checkout@v4
30+
31+
- name: Setup Terraform
32+
uses: hashicorp/setup-terraform@v2
33+
34+
- name: Terraform Init
35+
run: |
36+
cd infrastructure/terraform
37+
terraform init
38+
39+
- name: Terraform Apply
40+
run: |
41+
cd infrastructure/terraform
42+
terraform apply -auto-approve
43+
env:
44+
AWS_ACCESS_KEY_ID: ${{ github.event.inputs.aws_access_key_id }}
45+
AWS_SECRET_ACCESS_KEY: ${{ github.event.inputs.aws_secret_access_key }}
46+
AWS_SESSION_TOKEN: ${{ github.event.inputs.aws_session_token }}
47+
48+
- name: Get EC2 Public IP
49+
id: get-ip
50+
run: |
51+
cd infrastructure/terraform
52+
echo "EC2_PUBLIC_IP=$(terraform output -raw public_ip)" >> $GITHUB_ENV
53+
54+
- name: Copy Docker Compose File From Repo to VM Host
55+
uses: appleboy/scp-action@v0.1.7
56+
with:
57+
host: ${{ env.EC2_PUBLIC_IP }}
58+
username: ubuntu
59+
key: ${{ secrets.AWS_EC2_PRIVATE_KEY }}
60+
source: "deployment/compose.aws.yml"
61+
target: /home/ubuntu/deployment
62+
strip_components: 1
63+
overwrite: true
64+
65+
- name: SSH to VM and Create .env.prod
66+
uses: appleboy/ssh-action@v1.0.3
67+
with:
68+
host: ${{ env.EC2_PUBLIC_IP }}
69+
username: ubuntu
70+
key: ${{ secrets.AWS_EC2_PRIVATE_KEY }}
71+
script: |
72+
cd /home/ubuntu
73+
rm -f .env.prod
74+
touch .env.prod
75+
echo "EC2_PUBLIC_IP=${{ env.EC2_PUBLIC_IP }}" >> .env.prod
76+
echo "CLIENT_HOST=client.${{ env.EC2_PUBLIC_IP }}.nip.io" >> .env.prod
77+
echo "SERVER_HOST=api.${{ env.EC2_PUBLIC_IP }}.nip.io" >> .env.prod
78+
echo "PUBLIC_API_URL=https://api.${{ env.EC2_PUBLIC_IP }}.nip.io" >> .env.prod
79+
80+
- name: SSH to VM and Execute Docker-Compose Up
81+
uses: appleboy/ssh-action@v1.0.3
82+
with:
83+
host: ${{ env.EC2_PUBLIC_IP }}
84+
username: ubuntu
85+
key: ${{ secrets.AWS_EC2_PRIVATE_KEY }}
86+
script: |
87+
cd /home/ubuntu
88+
echo ${{ secrets.GITHUB_TOKEN }} | docker login ghcr.io -u ${{ github.actor }} --password-stdin
89+
docker compose -f deployment/compose.aws.yml --env-file=/home/ubuntu/.env.prod up -d

infrastructure/terraform/.terraform.lock.hcl

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
100.26.202.164

infrastructure/terraform/main.tf

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
provider "aws" {
2+
region = var.aws_region
3+
}
4+
5+
# Create a security group
6+
resource "aws_security_group" "app" {
7+
name = "app-sg"
8+
description = "Security group for application"
9+
10+
ingress {
11+
from_port = 22
12+
to_port = 22
13+
protocol = "tcp"
14+
cidr_blocks = ["0.0.0.0/0"]
15+
}
16+
17+
ingress {
18+
from_port = 80
19+
to_port = 80
20+
protocol = "tcp"
21+
cidr_blocks = ["0.0.0.0/0"]
22+
}
23+
24+
ingress {
25+
from_port = 443
26+
to_port = 443
27+
protocol = "tcp"
28+
cidr_blocks = ["0.0.0.0/0"]
29+
}
30+
31+
egress {
32+
from_port = 0
33+
to_port = 0
34+
protocol = "-1"
35+
cidr_blocks = ["0.0.0.0/0"]
36+
}
37+
38+
tags = {
39+
Name = "app-sg"
40+
}
41+
}
42+
43+
# Create an EC2 instance
44+
resource "aws_instance" "app" {
45+
ami = var.ami_id
46+
instance_type = var.instance_type
47+
48+
vpc_security_group_ids = [aws_security_group.app.id]
49+
50+
tags = {
51+
Name = "app-instance"
52+
}
53+
54+
# This will output the public IP after creation
55+
provisioner "local-exec" {
56+
command = "echo ${self.public_ip} > ec2_public_ip.txt"
57+
}
58+
}
59+
60+
# Output the public IP
61+
output "public_ip" {
62+
value = aws_instance.app.public_ip
63+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"version": 4,
3+
"terraform_version": "1.12.2",
4+
"serial": 6,
5+
"lineage": "829d69c0-90b4-5caf-5f55-c55d57d83916",
6+
"outputs": {},
7+
"resources": [],
8+
"check_results": null
9+
}

0 commit comments

Comments
 (0)