Skip to main content

Command Palette

Search for a command to run...

How to Enter into AWS Fargate Container 💡

Effortless Guide to Executing into a Fargate Container #getintokube

Updated
3 min read
How to Enter into AWS Fargate Container 💡
G

Gerlyn is a DevOps engineer with a strong passion for Kubernetes and automation. He is always eager to learn and continuously strives to enhance his skills, aiming to become an expert in the field. He loves to share his knowledge with the community.

This blog is for those who are tired of trying to exec into AWS Fargate containers. Even after referring to ChatGPT and various online blogs, you still couldn't find a solution to get inside a Fargate container. Here is the short and on-point solution you've been looking for.

Pre-requisites

  1. AWS CLI Installed and Configured:

    • Install AWS CLI v2 or later if you haven’t already.

    • Ensure your CLI is configured with the correct region and credentials (aws configure).

  2. IAM Permissions:

    • Add SSM permissions to the Task IAM role:

    • You should add the following policy to your existing ECS task IAM role. This grants permission for the ECS task to connect with the SSM Session Manager service.

      ECS Task Role we can find here.

    • Click ecsTaskExecutionRole > Add Permission > Create inline policy > Switch to JSON > Paste the below policy then save. Do this for both the policies.

        {
           "Version": "2012-10-17",
           "Statement": [
               {
               "Effect": "Allow",
               "Action": [
                    "ssmmessages:CreateControlChannel",
                    "ssmmessages:CreateDataChannel",
                    "ssmmessages:OpenControlChannel",
                    "ssmmessages:OpenDataChannel"
               ],
              "Resource": "*"
              }
           ]
        }
      
    • Add ECS Execute Command permission to your Task IAM role:

      Make sure your IAM role contains a policy that allows the action ecs:ExecuteCommand. Otherwise, you’re not able to run aws ecs execute-command in the AWS CLI in order to access the running container.

    • ✍️ Alter “Resource” value with ECS cluster arn in the below policy⬇️.

        {
          "Version": "2012-10-17",
          "Statement": [
            {
              "Effect": "Allow",
              "Action": "ecs:ExecuteCommand",
              "Resource": "arn:aws:ecs:example-region:example-arn:cluster/example-cluster/*"
            }
          ]
        }
      
  3. AWS Session Manager Plugin Installed:


Steps to Execute into a Container

1. Identify Your Cluster and Task

  • Find the ECS cluster name and the task running your container:

      aws ecs list-clusters
    
      aws ecs list-tasks --cluster <your-cluster-name>
    

2. Describe the Task

  • Get details about the task, including the container name:

      aws ecs describe-tasks --cluster <your-cluster-name> --tasks <task-id>
    

3. Enable Execute Command on the Task

  • Now you need to enable the ECS Exec feature on existing ECS service and deploy the new task by using the below command.

      aws ecs update-service \
          --cluster <cluster-name> \
          --task-definition <task-definition-name> \
          --service <service-name> \
          --enable-execute-command \
          --force-new-deployment
    
  • After executing the above command, wait for the new task to deploy successfully.

4. Execute the Command

  • To open an interactive shell inside the container, replace /bin/bash with /bin/sh if bash is not available in your container.

      aws ecs execute-command --cluster <cluster-name> \
          --task <task-id> \
          --container <container-name> \
          --interactive \
          --command "/bin/sh"
    
  • This is the output you’ll see when you’re executing aws ecs execute-command on an actual running container.

      aws ecs execute-command --cluster <cluster-name> \
          --task <task-id> \
          --container <container-name> \
          --interactive \
          --command "/bin/sh"
    
      The Session Manager plugin was installed successfully. Use the AWS CLI to start a session.
    
      Starting session with SessionId: ecs-execute-command-5tap5jrfpg8g5p2o5z8opsfqxe
      #
    

By following these steps, you can 🤩 successfully enable and use the ECS Exec feature to open an interactive shell inside a running container.


If you have any suggestions, ideas, or thoughts to add, feel free to drop them in the comments. 👇📩

Your feedback means a lot! Don’t forget to hit that like❤️ button to show your support and stay tuned for more content. 🔔

⭐Thanks again!

#ecs #aws #ecs_fargate #getintokube #getintokube_blogs #aws #ecs #ecs_fargate #How_to_Enter_into_ AWS_Fargate_Container #How_to_exec_into_AWS_Fargate_Container

How to - AWS

Part 7 of 7

This blog series is all about hands-on AWS. No theory, no jargon - just practical tasks you can apply immediately. Whether you're deploying, automating, or troubleshooting, these step-by-step guides help you get things done in AWS, the right way. 🚀

Start from the beginning

How to Configure ExternalDNS with Cross-Account Route53

AWS cross account access with OIDC provider in EKS With ExternalDNS

More from this blog

G

GetintoKube

12 posts

We believe in the power of learning by doing and the importance of sharing knowledge. By documenting and sharing our experiences, we hope to inspire and help others who are on a similar path.