Skip to content

Commit

Permalink
Merge pull request #132 from dxw/create-empty-service-env-file
Browse files Browse the repository at this point in the history
Create empty service env file
  • Loading branch information
Stretch96 authored Jul 19, 2024
2 parents ebc4e09 + 5237c7f commit 58b14bc
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ This project creates and manages resources within an AWS account for infrastruct
| [random_password.infrastructure_ecs_cluster_service_cloudfront_bypass_protection_secret](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/password) | resource |
| [random_password.infrastructure_rds_root](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/password) | resource |
| [terraform_data.infrastructure_ecs_cluster_service_blue_green_create_codedeploy_deployment](https://registry.terraform.io/providers/hashicorp/terraform/latest/docs/resources/data) | resource |
| [terraform_data.infrastructure_ecs_cluster_service_env_file](https://registry.terraform.io/providers/hashicorp/terraform/latest/docs/resources/data) | resource |
| [archive_file.ecs_cluster_infrastructure_draining_lambda](https://registry.terraform.io/providers/hashicorp/archive/latest/docs/data-sources/file) | data source |
| [archive_file.ecs_cluster_infrastructure_ecs_asg_diff_metric_lambda](https://registry.terraform.io/providers/hashicorp/archive/latest/docs/data-sources/file) | data source |
| [archive_file.ecs_cluster_infrastructure_pending_task_metric_lambda](https://registry.terraform.io/providers/hashicorp/archive/latest/docs/data-sources/file) | data source |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,5 +173,6 @@ resource "terraform_data" "infrastructure_ecs_cluster_service_blue_green_create_

depends_on = [
aws_codepipeline.infrastructure_ecs_cluster_service,
terraform_data.infrastructure_ecs_cluster_service_env_file,
]
}
19 changes: 19 additions & 0 deletions ecs-cluster-infrastructure-service.tf
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,25 @@ resource "aws_iam_role_policy_attachment" "infrastructure_ecs_cluster_service_ta
policy_arn = aws_iam_policy.infrastructure_ecs_cluster_service_task_custom[each.key].arn
}


resource "terraform_data" "infrastructure_ecs_cluster_service_env_file" {
for_each = local.infrastructure_ecs_cluster_services

triggers_replace = [
aws_ecs_service.infrastructure_ecs_cluster_service[each.key].name,
aws_s3_bucket.infrastructure_ecs_cluster_service_environment_files[0].bucket,
]

provisioner "local-exec" {
interpreter = ["/bin/bash", "-c"]
command = <<EOF
${path.root}/local-exec-scripts/create-empty-s3-object.sh \
-b "${aws_s3_bucket.infrastructure_ecs_cluster_service_environment_files[0].bucket}" \
-k "${each.key}.env"
EOF
}
}

resource "aws_ecs_task_definition" "infrastructure_ecs_cluster_service" {
for_each = local.infrastructure_ecs_cluster_services

Expand Down
49 changes: 49 additions & 0 deletions local-exec-scripts/create-empty-s3-object.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash

# exit on failures
set -e
set -o pipefail

usage() {
echo "Usage: $(basename "$0") [OPTIONS]" 1>&2
echo " -h - help"
echo " -b - Bucket"
echo " -k - Key"
exit 1
}

while getopts "b:k:h" opt; do
case $opt in
b)
BUCKET=$OPTARG
;;
k)
KEY=$OPTARG
;;
h)
usage
;;
*)
usage
;;
esac
done

if [[
-z "$BUCKET" ||
-z "$KEY"
]]
then
usage
fi

if ! aws s3api head-object --bucket "$BUCKET" --key "$KEY" 2>/dev/null
then
# If the file does not exist, create an empty file
touch /tmp/empty_file.txt
aws s3api put-object --bucket "$BUCKET" --key "$KEY" --body /tmp/empty_file.txt
rm /tmp/empty_file.txt
echo "==> Empty file created in S3 bucket"
else
echo "==> File already exists in S3 bucket, skipping creation"
fi

0 comments on commit 58b14bc

Please sign in to comment.