# How to Enter into AWS Fargate Container 💡

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 <mark>Task IAM role</mark>:**
        
    * 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.](https://cdn.hashnode.com/res/hashnode/image/upload/v1737029633071/59d972da-bd25-4940-af2b-c5a480b28ada.png align="center")
        
    * Click `ecsTaskExecutionRole` &gt; Add Permission &gt; Create inline policy &gt; Switch to JSON &gt; Paste the below policy then save. Do this for both the policies.
        
        ```bash
        {
           "Version": "2012-10-17",
           "Statement": [
               {
               "Effect": "Allow",
               "Action": [
                    "ssmmessages:CreateControlChannel",
                    "ssmmessages:CreateDataChannel",
                    "ssmmessages:OpenControlChannel",
                    "ssmmessages:OpenDataChannel"
               ],
              "Resource": "*"
              }
           ]
        }
        ```
        
    * **Add ECS Execute Command permission to your <mark>Task IAM role:</mark>**
        
        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⬇️.
        
        ```bash
        {
          "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:**
    
    * [Install the Session Manager Plugin for AWS CLI.](https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-working-with-install-plugin.html)
        

---

### **Steps to Execute into a Container**

#### **1\. Identify Your Cluster and Task**

* Find the ECS cluster name and the task running your container:
    
    ```bash
    aws ecs list-clusters
    ```
    
    ```bash
    aws ecs list-tasks --cluster <your-cluster-name>
    ```
    

#### **2\. Describe the Task**

* Get details about the task, including the container name:
    
    ```bash
    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.
    
    ```bash
    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.
    
    ```bash
    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.
    
    ```bash
    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
