Skip to content

Commit

Permalink
Create empty service env file
Browse files Browse the repository at this point in the history
* When a new service is deployed, if the .env file doesn't exist in the
  environment files S3 bucket, the initial deployment will fail and
  terraform stops
* This adds a script to create an empty .env file for the service, only
  if the file doesn't exist (It won't overwrite existing files). The
  script is then ran when the bucklet or service name changes
  • Loading branch information
Stretch96 committed Jul 18, 2024
1 parent c44893c commit cd3601e
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ This project creates and manages resources within an AWS account for infrastruct
| <a name="provider_external"></a> [external](#provider\_external) | 2.3.3 |
| <a name="provider_null"></a> [null](#provider\_null) | >= 3.2.2 |
| <a name="provider_random"></a> [random](#provider\_random) | >= 3.6.0 |
| <a name="provider_terraform"></a> [terraform](#provider\_terraform) | n/a |

## Resources

Expand Down Expand Up @@ -347,6 +348,7 @@ This project creates and manages resources within an AWS account for infrastruct
| [null_resource.infrastructure_ecs_cluster_service_blue_green_create_codedeploy_deployment](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [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_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 "null_resource" "infrastructure_ecs_cluster_service_blue_green_create_c

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 @@ -151,6 +151,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 cd3601e

Please sign in to comment.