Skip to content

Add dev environment setup for JavaBuilder Lambda deployment #406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dev-deployment/.ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.0.5
56 changes: 56 additions & 0 deletions dev-deployment/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Javabuilder Dev Environment Setup

This README provides a detailed guide for setting up, deploying, and understanding the JavaBuilder provisioning system for AWS Lambda.

## Overview

The JavaBuilder environment involves several components that need to be configured and deployed for development and testing purposes. This guide leads you through processing templates, configuring environments, and deploying the setup on AWS.

### Key Components
- **javabuilder-authorizer**: Handles authorization for API Gateway requests.
- **api-gateway-routes**: Contains the logic for interacting with API Gateway.
- **org-code-javabuilder**: The core logic constructed using Gradle and Java.

## Prerequisites

- **AWS CLI**: Ensure AWS CLI is configured with appropriate credentials.
- **Ruby**: Required for processing ERB templates.
- **AWS CloudFormation**: Utilized for deploying infrastructure as code.

## Setup Steps

### 1. Pre-Deployment Checks
Run the script to verify prerequisites:
```bash
./pre-deploy-check.sh
```
This script checks AWS CLI installations, required files, and IAM permissions.

### 2. Deployment
To deploy the JavaBuilder environment:
```bash
./deploy-javabuilder-dev.sh
```
This script:
- Packages and validates the CloudFormation template
- Deploys using AWS CloudFormation
- Displays stack outputs upon successful deployment

## Detailed Workflow

- **Process ERB Template**: Convert `template.yml.erb` to a CloudFormation template, tailored for the development environment.
- **Configuration**: Modify the config files under `config/` directory according to your deployment environment.
- **Artifact Management**: Use an S3 bucket to store deployment artifacts.

## Post-Deployment

- **Monitoring**: Leverage AWS CloudWatch for logs and metrics.
- **Cleanup**: Regularly maintain the S3 bucket and CloudFormation stacks to avoid unnecessary charges.

## Troubleshooting

- Ensure environment variables and parameters in `dev-deployment-params.json` are accurate.
- Check AWS IAM roles and permissions if access is denied during deployment.

For further assistance, consult internal documentation or reach out to the DevOps team.

111 changes: 111 additions & 0 deletions dev-deployment/deploy-javabuilder-dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#!/bin/bash

# Javabuilder Dev Environment Deployment Script
# This script follows the build process from buildspec.yml and deploys to dev account

set -e

# Configuration
AWS_PROFILE="codeorg-dev"
STACK_NAME="javabuilder-dev"
REGION="us-east-1"
ARTIFACT_BUCKET_NAME="javabuilder-dev-artifacts-$(date +%s)"
TEMPLATE_FILE="dev-app-template.yml"
PACKAGED_TEMPLATE_FILE="packaged-dev-app-template.yml"
PARAMS_FILE="dev-deployment-params.json"

echo "🚀 Starting Javabuilder Dev Environment Deployment..."

# Step 1: Check AWS CLI and credentials
echo "📋 Checking AWS CLI and credentials for profile: $AWS_PROFILE..."
if ! aws sts get-caller-identity --profile "$AWS_PROFILE" > /dev/null 2>&1; then
echo "❌ AWS CLI not configured or no valid credentials found for profile: $AWS_PROFILE"
echo "Please ensure the profile is configured correctly"
echo "You can check with: aws configure list --profile $AWS_PROFILE"
exit 1
fi

echo "✅ AWS credentials verified for profile: $AWS_PROFILE"

# Step 2: Create S3 bucket for artifacts if it doesn't exist
echo "📦 Creating S3 bucket for artifacts..."
if aws s3api head-bucket --bucket "$ARTIFACT_BUCKET_NAME" --profile "$AWS_PROFILE" 2>/dev/null; then
echo "✅ Bucket $ARTIFACT_BUCKET_NAME already exists"
else
aws s3api create-bucket --bucket "$ARTIFACT_BUCKET_NAME" --region "$REGION" --profile "$AWS_PROFILE"
echo "✅ Created bucket: $ARTIFACT_BUCKET_NAME"
fi

# Step 3: Process ERB template (already done, but let's ensure it's current)
echo "🔄 Processing ERB template..."
erb -T - ../cicd/3-app/javabuilder/template.yml.erb > "$TEMPLATE_FILE"
echo "✅ Generated CloudFormation template: $TEMPLATE_FILE"

# Step 4: Run environment config script
echo "⚙️ Running environment configuration script..."
../cicd/3-app/javabuilder/config/create-environment-config.sh
echo "✅ Environment configuration completed"

# Step 5: Validate CloudFormation template
echo "🔍 Validating CloudFormation template..."
aws cloudformation validate-template --template-body file://"$TEMPLATE_FILE" --profile "$AWS_PROFILE" 0> /dev/null
echo "✅ Template validation successful"

# Step 6: Package the CloudFormation template
echo "📦 Packaging CloudFormation template..."
aws cloudformation package \
--template-file "$TEMPLATE_FILE" \
--s3-bucket "$ARTIFACT_BUCKET_NAME" \
--s3-prefix "package" \
--output-template-file "$PACKAGED_TEMPLATE_FILE" \
--profile "$AWS_PROFILE"
echo "✅ Template packaged successfully"

# Step 7: Check if stack exists
echo "🔍 Checking if stack exists..."
STACK_EXISTS=$(aws cloudformation describe-stacks --stack-name "$STACK_NAME" --query 'Stacks[0].StackStatus' --output text --profile "$AWS_PROFILE" 2> /dev/null || echo "DOES_NOT_EXIST")

if [ "$STACK_EXISTS" = "DOES_NOT_EXIST" ]; then
echo "🆕 Creating new stack: $STACK_NAME"
OPERATION="create-stack"
WAIT_CONDITION="stack-create-complete"
else
echo "🔄 Updating existing stack: $STACK_NAME"
OPERATION="update-stack"
WAIT_CONDITION="stack-update-complete"
fi

# Step 8: Deploy the stack
echo "🚀 Deploying stack..."
aws cloudformation "$OPERATION" \
--stack-name "$STACK_NAME" \
--template-body file://"$PACKAGED_TEMPLATE_FILE" \
--parameters file://"$PARAMS_FILE" \
--capabilities CAPABILITY_IAM \
--region "$REGION" \
--tags Key=Environment,Value=development Key=Project,Value=javabuilder \
--profile "$AWS_PROFILE"

echo "⏳ Waiting for stack deployment to complete..."
aws cloudformation wait "$WAIT_CONDITION" --stack-name "$STACK_NAME" --profile "$AWS_PROFILE"

# Step 9: Get stack outputs
echo "📋 Stack deployment completed! Getting outputs..."
aws cloudformation describe-stacks \
--stack-name "$STACK_NAME" \
--query 'Stacks[0].Outputs' \
--output table \
--profile "$AWS_PROFILE"

echo "✅ Deployment completed successfully!"
echo "📝 Next steps:"
echo " 1. Verify the Lambda functions are working"
echo " 2. Test the API Gateway endpoints"
echo " 3. Monitor CloudWatch logs for any issues"
echo " 4. Update Route 53 DNS if needed"

# Cleanup reminder
echo "🧹 Cleanup reminder:"
echo " - S3 bucket: $ARTIFACT_BUCKET_NAME"
echo " - CloudFormation stack: $STACK_NAME"
echo " - Generated files: $TEMPLATE_FILE, $PACKAGED_TEMPLATE_FILE"
Loading