Skip to content

Commit

Permalink
add ci check for ecs script, dynamically create port mapping for mesh
Browse files Browse the repository at this point in the history
  • Loading branch information
alismx committed Jun 25, 2024
1 parent ba79925 commit e664bf4
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 60 deletions.
18 changes: 6 additions & 12 deletions .github/workflows/ecr_deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ jobs:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

# - name: Login to Amazon ECR
# id: login-ecr
# uses: aws-actions/amazon-ecr-login@v1
# with:
# region: us-east-1
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
with:
region: us-east-1

# - name: Authenticate Docker Registry for ECR
# run: aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 339712971032.dkr.ecr.us-east-1.amazonaws.com
Expand All @@ -44,13 +44,7 @@ jobs:
echo "owner = \"$OWNER\""
echo "project = \"$PROJECT\""
echo "region = \"$REGION\""
terraform init \
-migrate-state \
-var-file="$ENVIRONMENT.tfvars" \
-backend-config "bucket=$BUCKET" \
-backend-config "dynamodb_table=$DYNAMODB_TABLE" \
-backend-config "region=$REGION"
terraform apply -var-file="$ENVIRONMENT.tfvars"
./ecs.sh -e dev --ci
# - name: Apply Terraform
# working-directory: ./terraform/implementation/ecs
Expand Down
86 changes: 50 additions & 36 deletions terraform/implementation/ecs/ecs.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#!/bin/bash

# Load environment variables from .env file
if [ -f ../.env ]; then
export $(cat ../.env | xargs)
if [ -f .env ]; then
export $(cat .env | xargs)
fi

# set default values
ENVIRONMENT="${ENVIRONMENT:-}"
BUCKET="${BUCKET:-}"
DYNAMODB_TABLE="${DYNAMODB_TABLE:-}"
REGION="${REGION:-}"
CI=false

# parse command line arguments
while [[ $# -gt 0 ]]
Expand Down Expand Up @@ -37,13 +38,18 @@ do
shift
shift
;;
-ci|--ci)
CI=true
shift
;;
-h|--help)
echo "Usage: ./ecs.sh [OPTIONS]"
echo "Options:"
echo " -e, --env | Set the environment (e.g., production, staging) [REQUIRED]"
echo " -b, --bucket | Set the bucket name [REQUIRED]"
echo " -d, --dynamodb-table | Set the DynamoDB table name [REQUIRED]"
echo " -r, --region | Set the AWS region [REQUIRED]"
echo " -ci, --ci | Skip creating files and assume all arguments have values"
echo " -h, --help | Show help"
exit 0
;;
Expand All @@ -54,38 +60,44 @@ do
esac
done

if [ -z "$ENVIRONMENT" ] || [ -z "$BUCKET" ] || [ -z "$DYNAMODB_TABLE" ] || [ -z "$REGION" ]; then
echo "Missing required arguments. Please provide all the required arguments."
./ecs.sh -h
exit 1
fi

if ! command -v terraform &> /dev/null; then
echo "Terraform is not installed. Please install Terraform and try again."
exit 1
fi

if [ ! -f "$ENVIRONMENT.tfvars" ]; then
echo "Creating $ENVIRONMENT.tfvars"
touch "$ENVIRONMENT.tfvars"
if [ -z "$ENVIRONMENT" ] || [ -z "$BUCKET" ] || [ -z "$DYNAMODB_TABLE" ] || [ -z "$REGION" ]; then
echo "Missing required arguments. Please provide all the required arguments."
echo "ENVIRONMENT: $ENVIRONMENT"
echo "BUCKET: $BUCKET"
echo "DYNAMODB_TABLE: $DYNAMODB_TABLE"
echo "REGION: $REGION"
./ecs.sh -h
exit 1
fi

if ! grep -q "owner" "$ENVIRONMENT.tfvars"; then
read -p "Who is the owner of this infrastructure? ( default=skylight ): " owner_choice
owner_choice=${owner_choice:-skylight}
echo "owner = \"$owner_choice\"" >> "$ENVIRONMENT.tfvars"
fi
if [ "$CI" = false ]; then
if [ ! -f "$ENVIRONMENT.tfvars" ]; then
echo "Creating $ENVIRONMENT.tfvars"
touch "$ENVIRONMENT.tfvars"
fi

if ! grep -q "project" "$ENVIRONMENT.tfvars"; then
read -p "What is this project called? ( default=dibbs ): " project_choice
project_choice=${project_choice:-dibbs}
echo "project = \"$project_choice\"" >> "$ENVIRONMENT.tfvars"
fi
if ! grep -q "owner" "$ENVIRONMENT.tfvars"; then
read -p "Who is the owner of this infrastructure? ( default=skylight ): " owner_choice
owner_choice=${owner_choice:-skylight}
echo "owner = \"$owner_choice\"" >> "$ENVIRONMENT.tfvars"
fi

if ! grep -q "project" "$ENVIRONMENT.tfvars"; then
read -p "What is this project called? ( default=dibbs ): " project_choice
project_choice=${project_choice:-dibbs}
echo "project = \"$project_choice\"" >> "$ENVIRONMENT.tfvars"
fi

if ! grep -q "region" "$ENVIRONMENT.tfvars"; then
read -p "What aws region are you setting up in? ( default=us-east-1 ): " region_choice
region_choice=${region_choice:-us-east-1}
echo "region = \"$region_choice\"" >> "$ENVIRONMENT.tfvars"
if ! grep -q "region" "$ENVIRONMENT.tfvars"; then
read -p "What aws region are you setting up in? ( default=us-east-1 ): " region_choice
region_choice=${region_choice:-us-east-1}
echo "region = \"$region_choice\"" >> "$ENVIRONMENT.tfvars"
fi
fi

echo "Running Terraform with the following variables:"
Expand All @@ -104,18 +116,20 @@ terraform init \
-backend-config "region=$REGION" \
|| (echo "terraform init failed, exiting..." && exit 1)

# Check if workspace exists
if terraform workspace list | grep -q "$ENVIRONMENT"; then
echo "Selecting $ENVIRONMENT terraform workspace"
terraform workspace select "$ENVIRONMENT"
else
read -p "Workspace '$ENVIRONMENT' does not exist. Do you want to create it? (y/n): " choice
if [[ $choice =~ ^[Yy]$ ]]; then
echo "Creating '$ENVIRONMENT' terraform workspace"
terraform workspace new "$ENVIRONMENT"
if [ "$CI" = false ]; then
# Check if workspace exists
if terraform workspace list | grep -q "$ENVIRONMENT"; then
echo "Selecting $ENVIRONMENT terraform workspace"
terraform workspace select "$ENVIRONMENT"
else
echo "Workspace creation cancelled."
exit 1
read -p "Workspace '$ENVIRONMENT' does not exist. Do you want to create it? (y/n): " choice
if [[ $choice =~ ^[Yy]$ ]]; then
echo "Creating '$ENVIRONMENT' terraform workspace"
terraform workspace new "$ENVIRONMENT"
else
echo "Workspace creation cancelled."
exit 1
fi
fi
fi

Expand Down
13 changes: 11 additions & 2 deletions terraform/implementation/setup/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,21 @@ resource "aws_dynamodb_table" "tfstate_lock" {
}
}

resource "local_file" "env" {
resource "local_file" "setup_env" {
content = <<-EOT
ENVIRONMENT=${terraform.workspace}
BUCKET=${aws_s3_bucket.tfstate.bucket}
DYNAMODB_TABLE=${aws_dynamodb_table.tfstate_lock.id}
REGION=${var.region}
EOT
filename = "../.env"
filename = ".env"
}

resource "local_file" "ecs_env" {
content = <<-EOT
BUCKET=${aws_s3_bucket.tfstate.bucket}
DYNAMODB_TABLE=${aws_dynamodb_table.tfstate_lock.id}
REGION=${var.region}
EOT
filename = "../ecs/.env"
}
20 changes: 10 additions & 10 deletions terraform/modules/ecs/mesh.tf
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ resource "aws_appmesh_mesh" "this" {
}

resource "aws_appmesh_virtual_node" "this" {
for_each = aws_ecs_service.this
for_each = var.service_data
name = each.key
mesh_name = aws_appmesh_mesh.this.name

Expand Down Expand Up @@ -52,15 +52,15 @@ resource "aws_appmesh_virtual_node" "this" {

spec {
listener {
port_mapping {
port = 8080
protocol = "http"
}
}
listener {
port_mapping {
port = 3000
protocol = "http"
dynamic "port_mapping" {
for_each = {
for key, value in var.service_data : key => value
if each.key == key
}
content {
port = port_mapping.value.container_port
protocol = "http"
}
}
}

Expand Down

0 comments on commit e664bf4

Please sign in to comment.