How to connect to ECR from Fargate task in the VPC Build Docker image and push to ECR on Terraform apply

Hands-on

How to get current task ID/ARN from within the container?

Use Amazon ECS task metadata endpoint (Fargate). Response examples for Fargate here.

import requests
import boto3
...
ecs_client = boto3.client("ecs")
 
def enable_current_task_protection() -> None:
    task_metadata = requests.get(
        f"{os.environ['ECS_CONTAINER_METADATA_URI_V4']}/task"
    ).json()
    logger.debug(f"Task metadata: {task_metadata}")
 
    task_arn = task_metadata["TaskARN"]
 
    logger.info(f"Enabling protection for task {task_arn}")
    ecs_client.update_task_protection(
        cluster=task_metadata["Cluster"],
        tasks=[task_metadata["TaskARN"]],
        protectionEnabled=True,
    )

Fargate autoscaling

todo Draft references: