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
-
arnoptional computed - string -
container_definitionsrequired - string -
cpuoptional - string -
execution_role_arnoptional - string -
familyrequired - string -
idoptional computed - string -
ipc_modeoptional - string -
memoryoptional - string -
network_modeoptional computed - string -
pid_modeoptional - string -
requires_compatibilitiesoptional - set of string -
revisionoptional computed - number -
tagsoptional - map from string to string -
task_role_arnoptional - string -
inference_acceleratorset block-
device_namerequired - string -
device_typerequired - string
-
-
placement_constraintsset block-
expressionoptional - string -
typerequired - string
-
-
proxy_configurationlist block-
container_namerequired - string -
propertiesoptional - map from string to string -
typeoptional - string
-
-
volumeset block-
host_pathoptional - string -
namerequired - string -
docker_volume_configurationlist block-
autoprovisionoptional - bool -
driveroptional - string -
driver_optsoptional - map from string to string -
labelsoptional - map from string to string -
scopeoptional computed - string
-
-
efs_volume_configurationlist block-
file_system_idrequired - string -
root_directoryoptional - string -
transit_encryptionoptional - string -
transit_encryption_portoptional - number -
authorization_configlist block-
access_point_idoptional - string -
iamoptional - 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
-
Familyoptional - String -
ContainerDefinitionsoptional - List of ContainerDefinition -
Cpuoptional - String -
ExecutionRoleArnoptional - String -
EphemeralStorageoptional - EphemeralStorage -
InferenceAcceleratorsoptional - List of InferenceAccelerator -
Memoryoptional - String -
NetworkModeoptional - String -
PlacementConstraintsoptional - List of TaskDefinitionPlacementConstraint -
ProxyConfigurationoptional - ProxyConfiguration -
RequiresCompatibilitiesoptional - List -
TaskRoleArnoptional - String -
Volumesoptional - List of Volume -
PidModeoptional - String -
RuntimePlatformoptional - RuntimePlatform -
IpcModeoptional - String -
Tagsoptional - List of Tag
Explanation in CloudFormation Registry
The
AWS::ECS::TaskDefinitionresource 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.