Skip to content

Commit b602e59

Browse files
committed
cf-deployment
1 parent 7b8cd65 commit b602e59

File tree

5 files changed

+184
-11
lines changed

5 files changed

+184
-11
lines changed

.github/workflows/cd.yml

+6-1
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,17 @@ jobs:
2929
python -m pip install --upgrade pip
3030
pip install -r requirements.txt
3131
32+
- name: Prepare Zappa and Terraform Configurations
33+
run: |
34+
sed -i "s/PLACEHOLDER_REGION/${{ secrets.AWS_REGION }}/g" zappa_settings.json
35+
sed -i "s/zappa-PLACEHOLDER_PROJECT_NAME/zappa-${{ secrets.PROJECT_NAME }}/g" zappa_settings.json
36+
3237
- name: Configure AWS credentials
3338
uses: aws-actions/configure-aws-credentials@v1
3439
with:
3540
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
3641
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
37-
aws-region: eu-central-1
42+
aws-region: ${{ secrets.AWS_REGION }}
3843

3944
- name: Setup up Requirements
4045
run: |

.github/workflows/cloudFormation.yml

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
name: Create AWS Services and Deploy
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
project_name:
7+
description: 'Project Name'
8+
required: true
9+
secret_key:
10+
description: 'Secret Key'
11+
required: true
12+
email_host:
13+
description: 'Email Host'
14+
required: true
15+
email_port:
16+
description: 'Email Port'
17+
required: true
18+
email_user:
19+
description: 'Email User'
20+
required: true
21+
email_password:
22+
description: 'Email Password'
23+
required: true
24+
frontend_url:
25+
description: 'Frontend URL'
26+
required: true
27+
28+
jobs:
29+
deploy:
30+
runs-on: ubuntu-latest
31+
32+
steps:
33+
- name: Checkout
34+
uses: actions/checkout@v2
35+
36+
- name: Setup up Python
37+
uses: actions/setup-python@v2
38+
with:
39+
python-version: 3.10.8
40+
41+
- name: Set up virtual environment
42+
run: |
43+
python -m venv venv
44+
source venv/bin/activate
45+
46+
- name: Install dependencies
47+
run: |
48+
source venv/bin/activate
49+
python -m pip install --upgrade pip
50+
pip install -r requirements.txt
51+
52+
- name: Prepare Zappa and Terraform Configurations
53+
run: |
54+
sed -i "s/PLACEHOLDER_REGION/${{ secrets.AWS_REGION }}/g" zappa_settings.json
55+
sed -i "s/zappa-PLACEHOLDER_PROJECT_NAME/zappa-${{ github.event.inputs.project_name }}/g" zappa_settings.json
56+
sed -i "s/PLACEHOLDER_REGION/${{ secrets.AWS_REGION }}/g" ./deployments/main.tf
57+
sed -i "s/zappa-PLACEHOLDER_PROJECT_NAME/zappa-${{ github.event.inputs.project_name }}/g" ./deployments/main.tf
58+
59+
- name: Configure AWS credentials
60+
uses: aws-actions/configure-aws-credentials@v1
61+
with:
62+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
63+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
64+
aws-region: ${{ secrets.AWS_REGION }}
65+
66+
67+
- name: Deploy CloudFormation Stack
68+
run: |
69+
aws cloudformation deploy \
70+
--template-file ./.github/workflows/stacks/backendStackCF.yaml \
71+
--stack-name ${{ github.event.inputs.project_name }}-backend \
72+
--parameter-overrides ProjectName=${{ github.event.inputs.project_name }} SecretKey=${{ github.event.inputs.secret_key }} EmailHost=${{ github.event.inputs.email_host }} EmailPort=${{ github.event.inputs.email_port }} EmailUser=${{ github.event.inputs.email_user }} EmailPassword=${{ github.event.inputs.email_password }} FrontendUrl=${{ github.event.inputs.frontend_url }}
73+
--capabilities CAPABILITY_IAM
74+
75+
- name: Setup up Requirements
76+
run: |
77+
source venv/bin/activate
78+
chmod +x ./getEnv.sh && ./getEnv.sh
79+
python load_env.py
80+
zappa package prod -o package.zip
81+
82+
- name: Deploy
83+
run: |
84+
cd deployments
85+
terraform init
86+
terraform apply -auto-approve
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Description: Backend Infrastructure
3+
Parameters:
4+
ProjectName:
5+
Type: String
6+
Description: The project name to use for resource naming
7+
SecretKey:
8+
Type: String
9+
Description: Application secret key
10+
EmailHost:
11+
Type: String
12+
EmailPort:
13+
Type: Number
14+
EmailUser:
15+
Type: String
16+
EmailPassword:
17+
Type: String
18+
FrontendUrl:
19+
Type: String
20+
Resources:
21+
RDSDatabase:
22+
Type: AWS::RDS::DBInstance
23+
Properties:
24+
# Specify other properties as required for your use case
25+
DBName: !Sub '${ProjectName}-db'
26+
MasterUsername: admin
27+
Engine: postgres # Example, adjust based on your requirements
28+
MasterUserPassword: !Ref DBPassword # This parameter will be auto-generated; see Outputs below
29+
DBInstanceClass: db.t3.micro # Example, adjust based on your requirements
30+
AllocatedStorage: 20 # Example, adjust based on your requirements
31+
Secrets:
32+
Type: AWS::SecretsManager::Secret
33+
Properties:
34+
Name: !Sub '${ProjectName}-backend-secrets'
35+
Description: 'Backend secrets'
36+
SecretString: !Sub |
37+
{
38+
"SECRET_KEY": "${SecretKey}",
39+
"DB_NAME": "${RDSDatabase.DBName}",
40+
"DB_HOST": "${RDSDatabase.Endpoint.Address}",
41+
"DB_USER": "${RDSDatabase.MasterUsername}",
42+
"DB_PORT": "5432", # Example, adjust based on your RDS engine
43+
"DB_PASSWORD": "${RDSDatabase.MasterUserPassword}",
44+
"EMAIL_HOST": "${EmailHost}",
45+
"EMAIL_PORT": "${EmailPort}",
46+
"EMAIL_USER": "${EmailUser}",
47+
"EMAIL_PASSWORD": "${EmailPassword}",
48+
"FRONTEND_URL": "${FrontendUrl}"
49+
}
50+
51+
BackendBucket:
52+
Type: 'AWS::S3::Bucket'
53+
Properties:
54+
BucketName: !Sub '${ProjectName}-backend'
55+
56+
TerraformStateBucket:
57+
Type: 'AWS::S3::Bucket'
58+
Properties:
59+
BucketName: !Sub '${ProjectName}-terraform'
60+
VersioningConfiguration:
61+
Status: Enabled
62+
63+
RedirectBucket:
64+
Type: 'AWS::S3::Bucket'
65+
Properties:
66+
BucketName: !Sub '${ProjectName}-redirect'
67+
WebsiteConfiguration:
68+
RedirectAllRequestsTo:
69+
HostName: "www.example.com"
70+
71+
ZappaBucket:
72+
Type: 'AWS::S3::Bucket'
73+
Properties:
74+
BucketName: !Sub 'zappa-${ProjectName}'
75+
Outputs:
76+
DBPassword:
77+
Description: "The RDS database master user password"
78+
Value: !Ref MasterUserPassword
79+
RDSEndpoint:
80+
Description: "The RDS database endpoint"
81+
Value: !GetAtt RDSDatabase.Endpoint.Address

deployments/main.tf

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ terraform {
66
}
77
}
88
backend "s3" {
9-
bucket = "attendunce-terraform"
9+
bucket = "PLACEHOLDER_PROJECT_NAME-terraform"
1010
key = "terraform.tfstate"
11-
region = "eu-central-1"
11+
region = "PLACEHOLDER_REGION"
1212
acl = "bucket-owner-full-control"
1313
}
1414
}
1515

1616
provider "aws" {
17-
region = "eu-central-1"
17+
region = "PLACEHOLDER_REGION"
1818
}
1919

2020
resource "aws_iam_role" "lambda_role" {
@@ -37,7 +37,7 @@ EOF
3737
}
3838

3939
resource "aws_lambda_function" "app" {
40-
function_name = "attendunce-tf"
40+
function_name = "PLACEHOLDER_PROJECT_NAME-tf"
4141
handler = "handler.lambda_handler"
4242
runtime = "python3.10"
4343
timeout = 30
@@ -151,10 +151,10 @@ EOF
151151
}
152152

153153
resource "aws_s3_bucket_website_configuration" "redirect_bucket" {
154-
bucket = "attendunce-redirect"
154+
bucket = "PLACEHOLDER_PROJECT_NAME-redirect"
155155

156156
redirect_all_requests_to {
157-
host_name = "${aws_api_gateway_rest_api.api.id}.execute-api.eu-central-1.amazonaws.com"
157+
host_name = "${aws_api_gateway_rest_api.api.id}.execute-api.PLACEHOLDER_REGION.amazonaws.com"
158158
protocol = "https"
159159
}
160160
}
@@ -165,6 +165,6 @@ resource "aws_iam_role_policy_attachment" "cloudwatch_logs_attachment" {
165165
}
166166

167167
output "api_url" {
168-
value = "https://${aws_api_gateway_rest_api.api.id}.execute-api.eu-central-1.amazonaws.com/${aws_api_gateway_deployment.deployment.stage_name}"
168+
value = "https://${aws_api_gateway_rest_api.api.id}.execute-api.PLACEHOLDER_REGION.amazonaws.com/${aws_api_gateway_deployment.deployment.stage_name}"
169169
description = "The URL of the API endpoint"
170170
}

zappa_settings.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
22
"prod": {
3-
"aws_region": "eu-central-1",
3+
"app_function": "your_application.app",
4+
"aws_region": "PLACEHOLDER_REGION",
5+
"s3_bucket": "zappa-PLACEHOLDER_PROJECT_NAME",
46
"django_settings": "api.settings",
57
"project_name": "api",
68
"runtime": "python3.10",
7-
"s3_bucket": "zappa-attendunce"
89
}
9-
}
10+
}

0 commit comments

Comments
 (0)