Adding config to AWS ECS tasks (2024)

When deploying Docker containers to AWS ECS, you can encounter a situation where you want to run an image that requires some configuration. For example, let's say you wanted to run Vector1 as a sidecar to your main application so you can ship your application's metrics to a service like Honeybadger Insights. To run Vector, you only need to provide one configuration file (/etc/vector/vector.yaml) to the image available on Docker Hub. However, creating your own image that just adds one file would be a hassle. It would be easier if you could pull the public image, add your config, and deploy that. But ECS doesn't allow you to mount a file when running the container like you can when running Docker on your laptop or a VM. There is a way to do it on ECS, though — let's check it out.

Services and Tasks

But first, a little terminology. Running a Docker container on ECS requires you to create a task definition that specifies what image(s) you want to run, what the command should be, what the environment variables are, etc. Continuing our example, a task definition that runs Vector looks like this:

{ "containerDefinitions":[ { "name": "vector", "image": "timberio/vector:0.38.0-alpine", "essential": true, "environment": [] } ]}

Of course, this configuration won't do us much good as-is — it will run Vector, but there won't be any Vector configuration, so Vector won't be doing anything at all. We'll fix that in a bit. :)

An ECS service runs your tasks (made up of one or more images) on your own EC2 instances or instances managed by AWS (known as Fargate). We'll assume you're using Fargate for this tutorial. Each service definition specifies how many copies of the task definition you want to run (e.g., two or more for redundancy), what security group to use, the ports to forward to the containers, and so on. In other words, your task definition specifies the Docker-specific stuff like the image to use, and the service specifies how to run it in the AWS environment.

With that out of the way, we can return to the task at hand (pun intended).

Configuring a container

You might have a container that's configured entirely by environment variables. If that's the case, then you can use the environment section of the task definition to handle that:

 "environment": [ { "name": "ENVIRONMENT", "value": "production" }, { "name": "LOG_LEVEL", "value": "info" } ]

But you have to do a bit more work to get a configuration file to show up. I'll drop a task definition on you, then walk through the key points.

{ "containerDefinitions":[ { "name": "vector", "image": "timberio/vector:0.38.0-alpine", "mountPoints": [ { "sourceVolume": "vector-config", "containerPath": "/etc/vector" } ], "dependsOn": [ { "containerName": "vector-config", "condition": "COMPLETE" } ], }, { "name": "vector-config", "image": "bash", "essential": false, "command": [ "sh", "-c", "echo $VECTOR_CONFIG | base64 -d - | tee /etc/vector/vector.yaml" ], "environment": [ { "name": "VECTOR_CONFIG", "value": "Contents of a config file go here" } ], "mountPoints": [ { "sourceVolume": "vector-config", "containerPath": "/etc/vector" } ] } ]}

There are a few things to notice here:

  • There are two containers instead of just one. This is how you run a sidecar (running an app container and a logging container side by side) or, in this case, bootstrapping one container with another one.
  • Both containers share a mountpoint (vector-config) at the same location (/etc/vector). The containerPath doesn't have to be the same, but the sourceVolume does. This allows one container to write to a file and the other container to be able to read that same file.
  • The vector container depends on the vector-config container and waits to boot until the vector-config container has run its command.
  • The command for the vector-config container populates a configuration file with the contents of an environment variable called VECTOR_CONFIG.

That's the bones of getting a file mounted for the Docker container. An initializer container creates the file on a shared volume; then, another container can read the file. But how do we get the contents of our config file into that environment variable, and what's with the base64 -d - thing?

Terraform it

Terraform is a handy tool for automating the deployment of cloud infrastructure. It works with all kinds of clouds and is great for documenting and tracking your infrastructure changes. For this tutorial, we'll focus on just one Terraform resource — the one that can create our task definition and populate the configuration:

resource "aws_ecs_task_definition" "vector" { family = "vector" network_mode = "awsvpc" requires_compatibilities = ["FARGATE"] cpu = "256" memory = "512" volume { name = "vector-config" } container_definitions = jsonencode([ { name = "vector" image = "timberio/vector:0.38.0-alpine" essential = true mountPoints = [ { sourceVolume = "vector-config" containerPath = "/etc/vector" } ], dependsOn = [ { containerName = "vector-config" condition = "COMPLETE" } ] }, { name = "vector-config" image = "bash" essential = false command = [ "sh", "-c", "echo $VECTOR_CONFIG | base64 -d - | tee /etc/vector/vector.yaml" ], environment = [ { name = "VECTOR_CONFIG" value = base64encode(file("vector.yaml")) } ], mountPoints = [ { sourceVolume = "vector-config" containerPath = "/etc/vector" } ], } ])}

That looks pretty familiar, right? Terraform does a good job of sticking closely to the formats used by the various cloud providers. In this case, the aws_ecs_task_definition resource looks like the JSON used in task definitions. Note how the VECTOR_CONFIG environment variable is populated. Terraform provides file and base64encode helpers to read a file's contents and encode it, respectively2.

Our actual Vector config (that ends up at /etc/vector/vector.yaml) is stored in a file next to our Terraform config. It could look something like this:

sources: app_metrics: type: prometheus_scrape endpoints: - http://localhost:9090/metricssinks: honeybadger_insights: type: "http" inputs: ["app_metrics"] uri: "https://api.honeybadger.io/v1/events" request: headers: X-API-Key: "hbp_123" encoding: codec: "json" framing: method: "newline_delimited"

Diving into how Vector works could be a whole 'nother blog post, but here's a quick run-down on what we're configuring our Vector sidecar to do. We first define a source, or in other words, something that emits some data for Vector to process. Vector supports many sources, like S3 buckets, Kafka topics, etc. We're telling Vector to scrape Prometheus metrics served by our application on port 90903. The sink configuration sends data from Vector to someplace else — in this case, to Honeybadger Insights.

That's a wrap

So, that's how you can deploy a Docker image to AWS ECS with a custom configuration without having to build and host a custom image. All it takes is a little bit of Terraform!

  1. Vector is an open-source, high-performance observability data platform for collecting, transforming, and shipping logs, metrics, and traces from various sources to a wide array of destinations.

  2. Using Base64 encoding via the base64encode Terraform helper and decoding via the base64 -d - command allows us to avoid problems with quotes and other characters breaking the task definition's JSON configuration.

  3. For example, you can use a Prometheus exporter in your Rails app to get metrics that look like this to be served on port 9090.

Adding config to AWS ECS tasks (2024)

FAQs

How do I update AWS ECS task definition? ›

To update your task definition

Open the console at https://console.aws.amazon.com/ecs/v2 . In the navigation pane, choose Task Definitions. Choose the task definition used by your Amazon ECS service. Select the task definition revision, and then choose Create new revision, Create new revision.

How do I add a task to ECS? ›

Open the console at https://console.aws.amazon.com/ecs/v2 .
  1. In the navigation pane, choose Task definitions.
  2. On the Create new task definition menu, choose Create new task definition.
  3. For Task definition family, specify a unique name for the task definition.
  4. For Launch type, choose the application environment.

How do you create a new task revision in ECS? ›

Open the console at https://console.aws.amazon.com/ecs/v2 .
  1. From the navigation bar, choose the Region that contains your task definition.
  2. In the navigation pane, choose Task definitions.
  3. Choose the task definition.
  4. Select the task definition revision, and then choose Create new revision, Create new revision.

How to run an ECS task manually? ›

Running an application as an Amazon ECS task
  1. On the Task definitions page, choose the task definition family to display the revisions for that family.
  2. Select the revision you want to use.
  3. From the Deploy menu, choose Run task.

What is the task definition in AWS ECS? ›

A task definition is a blueprint for your application. It is a text file in JSON format that describes the parameters and one or more containers that form your application.

How do I remove ECS task definition? ›

To delete task definitions (Amazon ECS console)

From the navigation bar, choose the region that contains your task definition. In the navigation pane, choose Task definitions. On the Task definitions page, choose the task definition family that contains one or more revisions that you want to delete.

How to create task definition in ECS using AWS CLI? ›

Creating an Amazon ECS Linux task for the Fargate launch type with the AWS CLI
  1. Prerequisites.
  2. Step 1: Create a Cluster.
  3. Step 2: Register a Linux Task Definition.
  4. Step 3: List Task Definitions.
  5. Step 4: Create a Service.
  6. Step 5: List Services.
  7. Step 6: Describe the Running Service.
  8. Step 7: Test.

What is ECS task scheduler? ›

The Amazon ECS schedulers use the same cluster state information as the Amazon ECS API to make appropriate placement decisions. Amazon ECS provides a service scheduler for long-running tasks and applications. It also provides the ability to run standalone tasks or scheduled tasks for batch jobs or single run tasks.

Can an ECS task have multiple containers? ›

Creating multiple containers for ECS Services using a Task Definition. You can create up to five (5) containers for ECS services by defining a Task Definition.

What is the first step in configuring ECS? ›

Step 1: In the first step of the ECS Console, you will be able to specify task definition family name, image URI if you would like to setup monitoring, container port mappings, and the necessary environment variables to setup your container 's environment (sample application).

How do I restart all tasks in ECS? ›

Select the service you want to restart in the ECS console. Go to Update => check Force new deployment and do not make any other changes. Continue through the screens until you complete the process.

How do I start and stop an ECS task? ›

On the Clusters page, choose the cluster to navigate to the cluster details page. On the cluster detail page, choose the Tasks tab. You can filter tasks by launch type using the Filter launch type list. Select the tasks, and then choose Stop, Stop selected.

How to trigger an ECS task? ›

Run an AWS ECS task
  1. Modify the terraform file to enable the task and set the scheduled execution time for five minutes from now.
  2. Deploy the terraform and wait for the task to run.
  3. Undo the terraform changes and redeploy.
Mar 15, 2022

How do ECS tasks communicate? ›

Using service discovery, Amazon ECS syncs the list of launched tasks to AWS Cloud Map, which maintains a DNS hostname that resolves to the internal IP addresses of one or more tasks from that particular service.

How do I set up ECS exec? ›

Enabling Amazon ECS Exec
  1. From the AWS Explorer, expand the Amazon ECS menu.
  2. Expand the Clusters section, and choose the cluster your want to modify.
  3. Open the context menu for (right-click) the service you want to modify and choose Enable Command Execution. Note.

How to inactive task definition in AWS? ›

A task definition is ACTIVE after it is registered with Amazon ECS. You can use task definitions in the ACTIVE state to run tasks, or create services. A task definition transitions from the ACTIVE state to the INACTIVE state when you deregister a task definition.

How do you stop an AWS ECS task after it has completed its task? ›

To stop a standalone task (AWS Management Console)

On the Clusters page, choose the cluster to navigate to the cluster details page. On the cluster detail page, choose the Tasks tab. You can filter tasks by launch type using the Filter launch type list. Select the tasks, and then choose Stop, Stop selected.

References

Top Articles
BEST Keto Fat Bombs! Low Carb Keto 3 Musketeer Candy Fat Bombs Idea – No Bake – Sugar Free – Quick & Easy Ketogenic Diet Recipe – Completely Keto Friendly
Keto Blueberry Scones Recipe | Low Carb Recipes by That's Low Carb?!
Funny Roblox Id Codes 2023
Golden Abyss - Chapter 5 - Lunar_Angel
Www.paystubportal.com/7-11 Login
Joi Databas
DPhil Research - List of thesis titles
Shs Games 1V1 Lol
Evil Dead Rise Showtimes Near Massena Movieplex
Steamy Afternoon With Handsome Fernando
Which aspects are important in sales |#1 Prospection
Top Hat Trailer Wiring Diagram
World History Kazwire
R/Altfeet
George The Animal Steele Gif
Red Tomatoes Farmers Market Menu
Nalley Tartar Sauce
Chile Crunch Original
Immortal Ink Waxahachie
Craigslist Free Stuff Santa Cruz
Mflwer
Spergo Net Worth 2022
Costco Gas Foster City
Obsidian Guard's Cutlass
Marvon McCray Update: Did He Pass Away Or Is He Still Alive?
Mccain Agportal
Amih Stocktwits
Fort Mccoy Fire Map
Uta Kinesiology Advising
Kcwi Tv Schedule
What Time Does Walmart Auto Center Open
Nesb Routing Number
Olivia Maeday
Random Bibleizer
10 Best Places to Go and Things to Know for a Trip to the Hickory M...
Black Lion Backpack And Glider Voucher
Gopher Carts Pensacola Beach
Duke University Transcript Request
Lincoln Financial Field, section 110, row 4, home of Philadelphia Eagles, Temple Owls, page 1
Jambus - Definition, Beispiele, Merkmale, Wirkung
Ark Unlock All Skins Command
Craigslist Red Wing Mn
D3 Boards
Jail View Sumter
Nancy Pazelt Obituary
Birmingham City Schools Clever Login
Thotsbook Com
Funkin' on the Heights
Vci Classified Paducah
Www Pig11 Net
Ty Glass Sentenced
Latest Posts
Article information

Author: Sen. Emmett Berge

Last Updated:

Views: 5523

Rating: 5 / 5 (60 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Sen. Emmett Berge

Birthday: 1993-06-17

Address: 787 Elvis Divide, Port Brice, OH 24507-6802

Phone: +9779049645255

Job: Senior Healthcare Specialist

Hobby: Cycling, Model building, Kitesurfing, Origami, Lapidary, Dance, Basketball

Introduction: My name is Sen. Emmett Berge, I am a funny, vast, charming, courageous, enthusiastic, jolly, famous person who loves writing and wants to share my knowledge and understanding with you.