-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #211 from jonfairbanks/develop
Initial Lambda Migration
- Loading branch information
Showing
21 changed files
with
20,779 additions
and
36,211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
environment = "development" | ||
root_domain = "fairbanks.dev" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
Oops, something went wrong.