AWS Amazon ECS Task Definition
This page shows how to write Terraform and CloudFormation for Amazon ECS Task Definition and write them securely.
aws_ecs_task_definition (Terraform)
The Task Definition in Amazon ECS can be configured in Terraform with the resource name aws_ecs_task_definition
. The following sections describe 4 examples of how to use the resource and its parameters.
Example Usage from GitHub
resource "aws_ecs_task_definition" "container_definitions_environment_not_set" {
family = "foo"
container_definitions = <<EOF
[
{
"name": "bar",
resource "aws_ecs_task_definition" "container_definitions_environment_not_set" {
family = "foo"
container_definitions = <<EOF
[
{
"name": "bar",
resource "aws_ecs_task_definition" "gadget-front-task" {
family = "gadget-front-task"
cpu = "512"
memory = "1024"
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE"]
resource "aws_ecs_task_definition" "front-task" {
family = "webapp-service"
container_definitions = file("./container_definitions/service.json.tpl")
execution_role_arn = data.terraform_remote_state.aws_iam.outputs.ecs_task_role_arn
network_mode = "bridge"
Security Best Practices for aws_ecs_task_definition
There is 1 setting in aws_ecs_task_definition that should be taken care of for security reasons. The following section explain an overview and example code.
Ensure to enable in-transit encryption of EFS volume
It is better to enable in-transit encryption of EFS volume for the protection of data in transit more.
Parameters
-
arn
optional computed - string -
container_definitions
required - string -
cpu
optional - string -
execution_role_arn
optional - string -
family
required - string -
id
optional computed - string -
ipc_mode
optional - string -
memory
optional - string -
network_mode
optional computed - string -
pid_mode
optional - string -
requires_compatibilities
optional - set of string -
revision
optional computed - number -
tags
optional - map from string to string -
task_role_arn
optional - string -
inference_accelerator
set block-
device_name
required - string -
device_type
required - string
-
-
placement_constraints
set block-
expression
optional - string -
type
required - string
-
-
proxy_configuration
list block-
container_name
required - string -
properties
optional - map from string to string -
type
optional - string
-
-
volume
set block-
host_path
optional - string -
name
required - string -
docker_volume_configuration
list block-
autoprovision
optional - bool -
driver
optional - string -
driver_opts
optional - map from string to string -
labels
optional - map from string to string -
scope
optional computed - string
-
-
efs_volume_configuration
list block-
file_system_id
required - string -
root_directory
optional - string -
transit_encryption
optional - string -
transit_encryption_port
optional - number -
authorization_config
list block-
access_point_id
optional - string -
iam
optional - string
-
-
-
Explanation in Terraform Registry
Manages a revision of an ECS task definition to be used in
aws_ecs_service
.
Tips: Best Practices for The Other AWS Amazon ECS Resources
In addition to the aws_ecs_cluster, AWS Amazon ECS has the other resources that should be configured for security reasons. Please check some examples of those resources and precautions.
aws_ecs_cluster
Ensure to enable CloudWatch Container Insights
It's better to enable CloudWatch Container Insights to gain a better perspective on cluster applications. CloudWatch Container Insights collects, aggregates, and summarizes ECS metrics and logs.
AWS::ECS::TaskDefinition (CloudFormation)
The TaskDefinition in ECS can be configured in CloudFormation with the resource name AWS::ECS::TaskDefinition
. The following sections describe 10 examples of how to use the resource and its parameters.
Example Usage from GitHub
Type: AWS::ECS::TaskDefinition
Properties:
ContainerDefinitions:
- Name: "busybox"
Image: "busybox"
Cpu: 256
Type: AWS::ECS::TaskDefinition
Properties:
Family: !Ref 'MongoDbServiceName'
Cpu: !Ref 'ContainerCpu'
Memory: !Ref 'ContainerMemory'
TaskRoleArn:
Type: 'AWS::ECS::TaskDefinition'
Properties:
TaskRoleArn: 'aws:arn'
expectations:
rules:
all_ecs_tasks_must_have_task_end_execution_roles: FAIL
Type: 'AWS::ECS::TaskDefinition'
Properties:
TaskRoleArn: 'aws:arn'
expectations:
rules:
all_ecs_tasks_must_have_task_end_execution_roles: FAIL
Type: 'AWS::ECS::TaskDefinition'
Properties:
TaskRoleArn: 'aws:arn'
expectations:
rules:
all_ecs_tasks_must_have_task_end_execution_roles: FAIL
"resourceType" : "AWS::ECS::TaskDefinition",
"properties" : [ {
"propertyName" : "ContainerDefinitions",
"propertyType" : "List<AWS::ECS::TaskDefinition::ContainerDefinition>",
"required" : true,
"propertyHref" : "aws-properties-ecs-taskdefinition-containerdefinitions.html"
"Type": "AWS::ECS::TaskDefinition",
"Properties": {
"ContainerDefinitions": [
{
"Essential": true,
"Image": "mongo",
"rule":"$.Resources.*[?(@.Type=='AWS::ECS::TaskDefinition')].Properties.Cpu any null or $.Resources.*[?(@.Type=='AWS::ECS::TaskDefinition')].Properties.ContainerDefinitions[*].Cpu any null or $.Resources.*[?(@.Type=='AWS::ECS::TaskDefinition')].Properties.Cpu any equal 0 or $.Resources.*[?(@.Type=='AWS::ECS::TaskDefinition')].Properties.ContainerDefinitions[*].Cpu any equal 0 or ($.Resources.*[?(@.Type=='AWS::ECS::TaskDefinition')].Properties.Memory any null and $.Resources.*[?(@.Type=='AWS::ECS::TaskDefinition')].Properties.ContainerDefinitions[*].Memory any null) or ($.Resources.*[?(@.Type=='AWS::ECS::TaskDefinition')].Properties.Memory any equal 0 and $.Resources.*[?(@.Type=='AWS::ECS::TaskDefinition')].Properties.ContainerDefinitions[*].Memory any equal 0)",
"id":"44a82298-64d1-4b4b-a9ad-eeda02448975",
"enabled":true,
"resourceType":"ecs",
"policy":"AWS ECS task definition resource limits not set",
"description": "Check if AWS ECS task definition resource limits are set",
"Type": "AWS::ECS::TaskDefinition",
"Properties": {
"ContainerDefinitions": [{
"Essential": true,
"Image": "mongo",
"Name": "carts-db",
"Type": "AWS::ECS::TaskDefinition",
"Properties": {
"ContainerDefinitions": [
{
"Essential": true,
"Image": "mongo",
Parameters
-
Family
optional - String -
ContainerDefinitions
optional - List of ContainerDefinition -
Cpu
optional - String -
ExecutionRoleArn
optional - String -
EphemeralStorage
optional - EphemeralStorage -
InferenceAccelerators
optional - List of InferenceAccelerator -
Memory
optional - String -
NetworkMode
optional - String -
PlacementConstraints
optional - List of TaskDefinitionPlacementConstraint -
ProxyConfiguration
optional - ProxyConfiguration -
RequiresCompatibilities
optional - List -
TaskRoleArn
optional - String -
Volumes
optional - List of Volume -
PidMode
optional - String -
RuntimePlatform
optional - RuntimePlatform -
IpcMode
optional - String -
Tags
optional - List of Tag
Explanation in CloudFormation Registry
The
AWS::ECS::TaskDefinition
resource describes the container and volume definitions of an Amazon Elastic Container Service (Amazon ECS) task. You can specify which Docker images to use, the required resources, and other configurations related to launching the task definition through an Amazon ECS service or task.
Frequently asked questions
What is AWS Amazon ECS Task Definition?
AWS Amazon ECS Task Definition is a resource for Amazon ECS of Amazon Web Service. Settings can be wrote in Terraform and CloudFormation.
Where can I find the example code for the AWS Amazon ECS Task Definition?
For Terraform, the stelligent/config-lint, stelligent/config-lint and kuromitsu0104/gadget-app source code examples are useful. See the Terraform Example section for further details.
For CloudFormation, the melscoop-test/check, sdlombardo2/robot-shop and nizamawscloud/aws-cloudformation-cloudformation-guard source code examples are useful. See the CloudFormation Example section for further details.