AymenMz
AymenMz

Reputation: 11

How to delete IPv4 from target group (AWS EC2) using buildspec script

I created a DevOps pipeline in AWS Console. I created a project in AWS Code Build and used that project in a stage in my pipeline.

The objective from this stage is to get the private IP of a service in ECS and put it in a specific target group (EC2) and before doing this I want to purge the existing IPs which should be unhealthy and then register the new IP.

The problem I'm facing right now, is that I only cannot deregister the IPs.

I'm fetching the old IPs and I verified that using an echo in a script but to deregister them I'm not able using the script. I have all the required permissions and everything else is working fine, I'm just not sure about what I wrote in the script. Maybe I missed something, so I'd like some help.

Here's my buildspec script:

version: 0.2

phases:
  build:
    commands:
      - echo Registering ECS Task IP with target groups...

      # Fetch ECS Task ARN
      - echo "Fetching ECS Task ARN..."
      - TASK_ARN=$(aws ecs list-tasks --cluster $ECS_CLUSTER --service-name $SERVICE_NAME --query 'taskArns[0]' --output text)


      # Fetch ENI ID from the task
      - echo "Fetching ENI ID..."
      - ENI_ID=$(aws ecs describe-tasks --cluster $ECS_CLUSTER --tasks $TASK_ARN --query 'tasks[0].attachments[0].details[?name==`networkInterfaceId`].value' --output text)


      # Fetch Private IP from the ENI
      - echo "Fetching Private IP..."
      - PRIVATE_IP=$(aws ec2 describe-network-interfaces --network-interface-ids $ENI_ID --query 'NetworkInterfaces[0].PrivateIpAddress' --output text)


      - echo "Setting Port..."
      - PORT=$TASK_PORT


      # Deregister all existing targets from the first target group
      - |
        echo "Deregistering all existing targets from the first target group..."
        ALL_TARGETS=$(aws elbv2 describe-target-health --target-group-arn $TARGET_GROUP_ARN_1 --query 'TargetHealthDescriptions[].Target' --output json)
        echo "All Targets from the first target group: $ALL_TARGETS"
        for target in $(echo "$ALL_TARGETS" | jq -c '.[]'); do
          TARGET_ID=$(echo "$target" | jq -r '.Id')
          echo "Deregistering target with ID $TARGET_ID from the first target group..."
          aws elbv2 deregister-targets --target-group-arn $TARGET_GROUP_ARN_1 --targets Id=$TARGET_ID
        done

      # Deregister all existing targets from the second target group if it exists
      - |
        if [ -n "$TARGET_GROUP_ARN_2" ]; then
          echo "Deregistering all existing targets from the second target group..."
          ALL_TARGETS=$(aws elbv2 describe-target-health --target-group-arn $TARGET_GROUP_ARN_2 --query 'TargetHealthDescriptions[].Target' --output json)
          echo "All Targets from the second target group: $ALL_TARGETS"
          for target in $(echo "$ALL_TARGETS" | jq -c '.[]'); do
            TARGET_ID=$(echo "$target" | jq -r '.Id')
            echo "Deregistering target with ID $TARGET_ID from the second target group..."
            aws elbv2 deregister-targets --target-group-arn $TARGET_GROUP_ARN_2 --targets Id=$TARGET_ID
          done
        else
          echo "Second target group ARN is empty, skipping..."
        fi

      # Register the new IP address in the first target group
      - aws elbv2 register-targets --target-group-arn $TARGET_GROUP_ARN_1 --targets Id=$PRIVATE_IP,Port=$PORT
      - echo "ECS Task IP $PRIVATE_IP and Port $PORT registered with the first target group"

      # Register the new IP address in the second target group if it exists
      - |
        if [ -n "$TARGET_GROUP_ARN_2" ]; then
          aws elbv2 register-targets --target-group-arn $TARGET_GROUP_ARN_2 --targets Id=$PRIVATE_IP,Port=$PORT
          echo "ECS Task IP $PRIVATE_IP and Port $PORT registered with the second target group"
        else
          echo "Second target group ARN is empty, skipping registration..."
        fi

Upvotes: 1

Views: 40

Answers (0)

Related Questions