AWS Amazon ECS Service
This page shows how to write Terraform and CloudFormation for Amazon ECS Service and write them securely.
aws_ecs_service (Terraform)
The Service in Amazon ECS can be configured in Terraform with the resource name aws_ecs_service
. The following sections describe 4 examples of how to use the resource and its parameters.
Example Usage from GitHub
resource "aws_ecs_service" "nginx-service" {
count = 1
name = "nginx"
cluster = aws_ecs_cluster.platform.id
task_definition = aws_ecs_task_definition.nginx-task-definition.arn
scheduling_strategy = "REPLICA"
resource "aws_ecs_service" "kad-kafka-schema-registry" {
cluster = aws_ecs_cluster.kad-ecs.id
desired_count = 1
launch_type = "EC2"
name = "kad-kafka-schema-registry"
scheduling_strategy = "REPLICA"
resource "aws_ecs_service" "es_usw2a" {
name = "es_usw2a"
cluster = aws_ecs_cluster.es_cluster.id
task_definition = aws_ecs_task_definition.es_usw2a.arn
desired_count = 1
launch_type = "FARGATE"
resource "aws_ecs_service" "ecs_fargate1" {
name = "ecs_fargate1"
launch_type = "FARGATE"
cluster = aws_ecs_cluster.ecs1.id
task_definition = aws_ecs_task_definition.ecs_task1.arn
desired_count = 2
Parameters
-
cluster
optional computed - string -
deployment_maximum_percent
optional - number -
deployment_minimum_healthy_percent
optional - number -
desired_count
optional - number -
enable_ecs_managed_tags
optional - bool -
enable_execute_command
optional - bool -
force_new_deployment
optional - bool -
health_check_grace_period_seconds
optional - number -
iam_role
optional computed - string -
id
optional computed - string -
launch_type
optional computed - string -
name
required - string -
platform_version
optional computed - string -
propagate_tags
optional - string -
scheduling_strategy
optional - string -
tags
optional - map from string to string -
task_definition
optional - string -
wait_for_steady_state
optional - bool -
capacity_provider_strategy
set block-
base
optional - number -
capacity_provider
required - string -
weight
optional - number
-
-
deployment_circuit_breaker
list block -
deployment_controller
list block-
type
optional - string
-
-
load_balancer
set block-
container_name
required - string -
container_port
required - number -
elb_name
optional - string -
target_group_arn
optional - string
-
-
network_configuration
list block-
assign_public_ip
optional - bool -
security_groups
optional - set of string -
subnets
required - set of string
-
-
ordered_placement_strategy
list block -
placement_constraints
set block-
expression
optional - string -
type
required - string
-
-
service_registries
list block-
container_name
optional - string -
container_port
optional - number -
port
optional - number -
registry_arn
required - string
-
-
timeouts
single block-
delete
optional - string
-
Explanation in Terraform Registry
-> Note: To prevent a race condition during service deletion, make sure to set
depends_on
to the relatedaws_iam_role_policy
; otherwise, the policy may be destroyed too soon and the ECS service will then get stuck in theDRAINING
state. Provides an ECS service - effectively a task that is expected to run until an error occurs or a user terminates it (typically a webserver or a database). See ECS Services section in AWS developer guide.
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_task_definition
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.
AWS::ECS::Service (CloudFormation)
The Service in ECS can be configured in CloudFormation with the resource name AWS::ECS::Service
. The following sections describe 10 examples of how to use the resource and its parameters.
Example Usage from GitHub
Type: AWS::ECS::Service
Properties:
Cluster:
Fn::ImportValue: !Sub ${VPCName}-FrontendEcsCluster-${EnvType}
DesiredCount: !FindInMap [FrontendWebAppMap, !Ref EnvType, DesiredCount]
HealthCheckGracePeriodSeconds: 60
Type: AWS::ECS::Service
Properties:
ServiceName: TOKEN-SERVICE
Cluster:
Fn::ImportValue: !Join [':', [!Ref 'EcsClusterStackName', 'ClusterName']]
LaunchType: FARGATE
Type: AWS::ECS::Service
Properties:
TaskDefinition: "arn:aws:ecs:ap-northeast-2:085853160041:task-definition/game-math:12"
LoadBalancerInfo:
ContainerName: "game-math"
ContainerPort: "7001"
Type: AWS::ECS::Service
DependsOn:
- Exim4relayService
- Exim4localService
- DovecotService
- EmailauthService
Type: AWS::ECS::Service
Properties:
Cluster: default
DesiredCount: !Ref DesiredCount
TaskDefinition: !Ref ConfigTaskDefinition
DeploymentConfiguration:
"Type": "AWS::ECS::Service",
"Properties": {
"Cluster": {
"Ref": "EcsCluster"
},
"DesiredCount": 1,
"Type": "AWS::ECS::Service",
"Properties": {
"Cluster": {
"Ref": "EcsCluster"
},
"DesiredCount": 1,
"Type": "AWS::ECS::Service",
"Properties": {
"Cluster": {
"Ref": "EcsCluster"
},
"DesiredCount": 1,
"Type": "AWS::ECS::Service",
"Properties": {
"Cluster": {
"Ref": "EcsCluster"
},
"DesiredCount": 1,
"Type": "AWS::ECS::Service",
"Properties": {
"Cluster": {
"Ref": "EcsCluster"
},
"DesiredCount": 1,
Parameters
-
CapacityProviderStrategy
optional - List of CapacityProviderStrategyItem -
Cluster
optional - String -
DeploymentConfiguration
optional - DeploymentConfiguration -
DeploymentController
optional - DeploymentController -
DesiredCount
optional - Integer -
EnableECSManagedTags
optional - Boolean -
EnableExecuteCommand
optional - Boolean -
HealthCheckGracePeriodSeconds
optional - Integer -
LaunchType
optional - String -
LoadBalancers
optional - List of LoadBalancer -
NetworkConfiguration
optional - NetworkConfiguration -
PlacementConstraints
optional - List of PlacementConstraint -
PlacementStrategies
optional - List of PlacementStrategy -
PlatformVersion
optional - String -
PropagateTags
optional - String -
Role
optional - String -
SchedulingStrategy
optional - String -
ServiceName
optional - String -
ServiceRegistries
optional - List of ServiceRegistry -
Tags
optional - List of Tag -
TaskDefinition
optional - String
Explanation in CloudFormation Registry
The
AWS::ECS::Service
resource creates an Amazon Elastic Container Service (Amazon ECS) service that runs and maintains the requested number of tasks and associated load balancers.
Frequently asked questions
What is AWS Amazon ECS Service?
AWS Amazon ECS Service 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 Service?
For Terraform, the jorgechato/platform-tf, maikelpenz/kafka-aws-deployment and exNihlio/terraform source code examples are useful. See the Terraform Example section for further details.
For CloudFormation, the debugroom/mynavi-sample-aws-cloudformation, asakchris/api-gateway and bgpark82/game source code examples are useful. See the CloudFormation Example section for further details.