sticker-covered laptop featuring GitHub and AWS stickers

Dark Theme | Category: Code, Educational

Working with ECS Fargate Containers

Hello Gentlefriends! Today, we’re talking AWS ECS – Fargate. I’m willing to concede that this post might be pretty niche, but bear with me. The more I dig into AWS, the more I realize that figuring out how to do what I want to do requires backing up and figuring out the step or two before that. I’ll give you an example: I needed to edit a file inside of a Fargate container, but instead of searching for “how to edit a file inside a Fargate container” I first needed to find “how to exec into a Fargate container” and then “how to install vim on a Fargate container”. I collected the knowledge that took me five or so StackOverflow posts to acquire and put it into a single Confluence page, and now I’m going to share it here as well for whomever it may help. Note: The Fargate containers I have worked with thus fare are running flavors of Linux,

Exec in to a Fargate container:

aws ecs execute-command \
--region region \
--cluster cluster-name \
--task task-id \
--container container-name \
--command "/bin/bash" \
--interactive

Note: if you get an error that bash isn’t found or couldn’t be accessed, try using –command “bash” or –command “/bin/sh” instead of “/bin/bash”

Installing anything on a Fargate container:

apt-get install
apt-get apt-file
apt-file update
<command to install the thing you want to install, for example: apt-get install vim>

Note: The next time the pipeline runs the task/container will get replaced with a different task/container (new task id) and you will have to re-install anything you previously installed

Downloading a File from a Fargate container:

  1. Go to IAM, create a User, choose access keys, attach the AdministratorAccess policy, and make note of the user’s key and secret key
  2. Go to s3 and create a bucket
  3. Open CloudShell and exec into the Fargate container
  4. Inside the Fargate container, first install zip (as you will need it later) using the four steps above for installing something on a Fargate container
  5. Install the AWS CLI (note: the docs say to use sudo ./aws/install, but sudo won’t be recognized and you also don’t need it because you will be acting as root by default inside the Fargate container):
    curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
    unzip awscliv2.zip
    ./aws/install
  6. Run aws configure and enter your user’s keys that you saved. Make sure to use the correct region for the default region, and you can just use json for default format
  7. Use the following command to copy the file to your s3 bucket:
    aws s3 cp /path/to/file.json s3://your-bucket-name
  8. Go to your S3 bucket and download the file

Hopefully you had some of the same burning Fargate questions I had, and now they have been answered. May all your containers run smoothly without errors!

</ XOXO>

Enjoy my content and want to show your appreciation? You can share this post, pay it forward by teaching someone else, or buy me a coffee!

[Photo credit:  Mehmet Ali Peker via Unsplash]

Back to the Blog