-
-
Save ben-duffield/0312ab169ea616630675220a835e6369 to your computer and use it in GitHub Desktop.
ecs_definitions.tf
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
# Reading the github ref from provider | |
provider "github" { ... } | |
data "github_ref" "branch" { | |
repository = "MySampleRepo" | |
ref = "heads/main" | |
} | |
data "aws_ecs_task_definition" "latest" { | |
task_definition = "my_sample_task_definition" | |
} | |
resource "aws_ecs_task_definition" "my_sample_definition" { | |
container_definitions = jsonencode( | |
[{ | |
# ... | |
image = "AWS_ACCOUNT_ID.dkr.ecr.AWS_REGION.amazonaws.com/my_sample_repo_release:${data.github_ref.branch.sha}" | |
# ... | |
}] | |
) | |
family = "my_sample_task_definition" | |
} | |
data "aws_ecs_task_definition" "latest" { | |
task_definition = aws_ecs_task_definition.my_sample_definition.family | |
} | |
resource "aws_ecs_service" "my_sample_service" { | |
task_definition = data.aws_ecs_task_definition.latest.arn | |
} |
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
#!/bin/sh | |
set -e | |
refresh_taskdefs() | |
{ | |
for def in $taskdefs | |
do | |
echo "Refreshing $def" | |
state=$(terragrunt state show -no-color "$def") | |
current_arn=$(echo "$state" | grep -E "^\s+arn\s+=" | awk -F'"' '{print $2}') | |
family=$(echo "$state" | grep -E "^\s+family\s+=" | awk -F'"' '{print $2}') | |
echo "current_arn: $current_arn" | |
echo "family: $family" | |
latest_def=$(aws ecs describe-task-definition --task-definition "$family") | |
latest_arn=$(echo "$latest_def" | grep "taskDefinitionArn" | awk -F': "|",' '{print $2}') | |
echo "latest_arn: $latest_arn" | |
if [ "$current_arn" != "$latest_arn" ] | |
then | |
terragrunt state rm "$def" | |
terragrunt import "$def" "$latest_arn" | |
echo "Refreshed $def" | |
else | |
echo "Skipping $def, no change" | |
fi | |
done | |
} | |
if [ -z "$1" ] | |
then | |
taskdefs=$(terragrunt state list | grep aws_ecs_task_definition | grep -vE '(^|\.)data') | |
else | |
taskdefs=$1 | |
fi | |
refresh_taskdefs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment