Skip to content

Commit

Permalink
Merge pull request #211 from jonfairbanks/develop
Browse files Browse the repository at this point in the history
Initial Lambda Migration
  • Loading branch information
jonfairbanks authored Oct 7, 2024
2 parents a9a2331 + f2256ec commit 8416fb8
Show file tree
Hide file tree
Showing 21 changed files with 20,779 additions and 36,211 deletions.
54 changes: 54 additions & 0 deletions .github/workflows/aws-lambda-terraform.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: Deploy AWS Lambda via Terraform

on:
pull_request:
types:
- closed
branches:
- develop
- main

jobs:
deploy:
environment: ${{ github.ref == 'refs/heads/main' && 'production' || github.ref == 'refs/heads/development' && 'development' }}
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v4

- name: Setup deployment variables
id: setup-vars
run: |
BRANCH_NAME=$(echo "${{ github.ref }}" | sed 's|refs/heads/||')
echo "Branch: $BRANCH_NAME"
if [ "$BRANCH_NAME" = "develop" ]; then
echo "Using Development"
echo "environment=development" >> $GITHUB_OUTPUT
echo "aws_key=${{ secrets.aws_access_key_id }}" >> $GITHUB_OUTPUT
echo "aws_secret=${{ secrets.aws_secret_access_key }}" >> $GITHUB_OUTPUT
elif [ "$BRANCH_NAME" = "main" ]; then
echo "Using Production"
echo "environment=production" >> $GITHUB_OUTPUT
echo "aws_key=${{ secrets.aws_access_key_id }}" >> $GITHUB_OUTPUT
echo "aws_secret=${{ secrets.aws_secret_access_key }}" >> $GITHUB_OUTPUT
fi
- name: Verify credentials
run: |
echo "Selected Environment: ${{ steps.setup-vars.outputs.environment }}"
echo "Selected Key: ${{ steps.setup-vars.outputs.aws_key }}"
echo "Selected Secret: ${{ steps.setup-vars.outputs.aws_secret }}"
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ steps.setup-vars.outputs.aws_key }}
aws-secret-access-key: ${{ steps.setup-vars.outputs.aws_secret }}
aws-region: us-east-1

- name: Run Deploy Script
run: |
cd .tf/
echo "Deploying to environment: ${{ steps.setup-vars.outputs.environment }}"
./deploy.sh "${{ steps.setup-vars.outputs.environment }}"
15 changes: 14 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,17 @@ client/src/config/config.js
.vscode/settings.json
server/.env
client/.env
build/
build/
server/yo-api.log
server/yo.log
.tf/.terraform.lock.hcl
.tf/.terraform/providers/registry.terraform.io/hashicorp/aws/4.47.0/darwin_arm64/terraform-provider-aws_v4.47.0_x5
.tf/.terraform/terraform.tfstate
.tf/setup-s3-state-buckets/.terraform/providers/registry.terraform.io/hashicorp/aws/5.70.0/darwin_arm64/LICENSE.txt
.tf/setup-s3-state-buckets/.terraform/providers/registry.terraform.io/hashicorp/aws/5.70.0/darwin_arm64/terraform-provider-aws_v5.70.0_x5
.tf/terraform.tfstate
.tf/tfplan
*.zip
/server/dist
.tf/.terraform/providers/registry.terraform.io/hashicorp/aws/5.70.0/darwin_arm64/terraform-provider-aws_v5.70.0_x5
.tf/.terraform/providers/registry.terraform.io/hashicorp/aws/5.70.0/darwin_arm64/LICENSE.txt
46 changes: 46 additions & 0 deletions .tf/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
function deploy {
# Capture the tfvars file from the argument
ENVIRONMENT=$1

# Generate a version number based on a date timestamp so that it's unique
TIMESTAMP=$(date +%Y%m%d%H%M%S)

# Set the appropriate bucket based on environment
if [ "$ENVIRONMENT" = "development" ]; then
S3_BUCKET="yo-api-state-development"
elif [ "$ENVIRONMENT" = "production" ]; then
S3_BUCKET="yo-api-state-production"
fi

# Initialize terraform with backend config specific to the environment's bucket
terraform init \
-backend-config="bucket=${S3_BUCKET}" \
-backend-config="key=yo-api/${ENVIRONMENT}/terraform.tfstate" \
-backend-config="region=us-east-1" && \

# Import the pre-existing Route 53 hosted zone
# terraform import -var-file="$ENVIRONMENT.tfvars" -var lambdasVersion="$TIMESTAMP" aws_route53_zone.existing_zone Z01722783FY79QJ88LI8B && \

# Run the npm commands to transpile the TypeScript to JavaScript
cd ../server/ && \
npm i && \
npm run build && \
npm prune --omit=dev && \

# Create a dist folder and copy only the js files to dist.
# Note: AWS Lambda does not have a use for a package.json or typescript files on runtime.
mkdir -p dist/ && \
cp -r *.js dist/ && \
cd dist && \

# Zip everything in the dist folder and
find . -name "*.zip" -type f -delete && \
zip -r ./yo-api-"$TIMESTAMP".zip . --exclude "./node_modules/*" --exclude "./.DS_Store" && \
cd ../../.tf && \

# Run Terraform
terraform plan -input=false -var-file="$ENVIRONMENT.tfvars" -var lambdasVersion="$TIMESTAMP" -out=./tfplan && \
terraform apply -input=false ./tfplan
}

deploy "$@"
2 changes: 2 additions & 0 deletions .tf/development.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
environment = "development"
root_domain = "fairbanks.dev"
16 changes: 16 additions & 0 deletions .tf/input.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
variable "lambdasVersion" {
type = string
description = "version of the lambdas zip on S3"
}

variable "environment" {
description = "The environment for the deployment (e.g., development, production)"
type = string
default = "development"
}

variable "root_domain" {
description = "The root domain name to be used when creating domain names"
type = string
default = "fbnks.io"
}
Loading

0 comments on commit 8416fb8

Please sign in to comment.