service
View SourceECS services in AWS provide a managed way to run containerized applications. Stackattack creates ECS services with task definitions, load balancer integration, health checks, and service discovery.
import * as saws from "@stackattack/aws";
const ctx = saws.context();const vpc = saws.vpc(ctx);const cluster = saws.cluster(ctx, { network: vpc.network("private") });const app = saws.service(ctx, { name: "my-app", image: "nginx:latest", network: vpc.network("private"), cluster});
export const appUrl = app.internalUrl;
After deploying a service, you can manage it using:
AWS CLI:
# View service status and tasksaws ecs describe-services --cluster your-cluster-name --services your-service-name
# View service logsaws logs tail /aws/ecs/your-service-name --follow
# Scale the serviceaws ecs update-service --cluster your-cluster-name --service your-service-name --desired-count 3
Related Components
Section titled “Related Components”Services work together with other Stackattack components:
- cluster - Provides compute capacity for running services
- vpc - Provides networking foundation with private/public subnets
- load-balancer - Routes external traffic to services
ECS service costs depend on the underlying compute resources and are usage-based:
-
EC2 instances - If using EC2 capacity providers, you pay for the underlying EC2 instances (~$0.0116/hour for t3.micro). The cluster component manages auto-scaling groups that can scale to zero when no tasks are running.
-
Fargate - If using Fargate capacity providers, you pay per vCPU-hour (
$0.04048/vCPU/hour) and per GB-hour ($0.004445/GB/hour). A 0.5 vCPU, 1GB task running 24/7 costs ~$15/month. -
Data transfer - Minimal costs for service-to-service communication within the same VPC (typically free). External data transfer follows standard AWS rates.
-
CloudWatch Logs - Log storage is ~$0.50/GB/month. Use the
logRetention
parameter to automatically delete old logs and control costs.
Cost optimization strategies:
- Use the cluster component’s auto-scaling features to scale EC2 instances to zero during low usage
- Set appropriate
logRetention
periods (default: 30 days) - Consider spot instances for non-critical workloads through capacity provider configuration
See ECS Pricing for current rates.
service
Section titled “service”Creates an ECS service with task definition, load balancer integration, and service discovery.
function service(ctx: Context, args: ServiceArgs): ServiceOutput
Parameters
Section titled “Parameters”ctx
(Context
) - The context for resource naming and taggingargs
(ServiceArgs
) - Configuration arguments for the service
Returns
Section titled “Returns”- (
ServiceOutput
) - Creates an ECS service with task definition, load balancer integration, and service discovery.
Functions
Section titled “Functions”checkEcsDeployment
Section titled “checkEcsDeployment”Validates that an ECS service deployment matches the expected task definition.
function checkEcsDeployment(service: Service, taskDefinition: TaskDefinition): Output<string>
Parameters
Section titled “Parameters”service
(Service
) - The ECS service to checktaskDefinition
(TaskDefinition
) - The expected task definition
Returns
Section titled “Returns”- (
Output<string>
) - Validates that an ECS service deployment matches the expected task definition.
getServiceAttributes
Section titled “getServiceAttributes”function getServiceAttributes(service: Input<ServiceInput>): Output<Service | GetServiceResult>
Parameters
Section titled “Parameters”service
(Input<
ServiceInput
>
) -
Returns
Section titled “Returns”- (
Output<Service | GetServiceResult>
) -
getServiceId
Section titled “getServiceId”function getServiceId(service: Input<ServiceInput>): Output<string>
Parameters
Section titled “Parameters”service
(Input<
ServiceInput
>
) -
Returns
Section titled “Returns”- (
Output<string>
) -
taskDefinition
Section titled “taskDefinition”Creates an ECS task definition with container configuration, logging, and optional init container.
function taskDefinition(ctx: Context, args: TaskDefinitionArgs): TaskDefinition
Parameters
Section titled “Parameters”ctx
(Context
) - The context for resource naming and taggingargs
(TaskDefinitionArgs
) - Configuration arguments for the task definition
Returns
Section titled “Returns”- (
TaskDefinition
) - Creates an ECS task definition with container configuration, logging, and optional init container.
Interfaces
Section titled “Interfaces”ServiceArgs
Section titled “ServiceArgs”Configuration arguments for creating an ECS service, extending TaskDefinitionArgs.
Properties
Section titled “Properties”autoScaling?
(Omit<
ServiceAutoScalingArgs
, "service">
) - Specify an auto-scaling configuration for your service. Cannot be used withreplicas
. See the serviceAutoScaling component for argument documentation.cluster
(Input<
ClusterResourcesInput
>
) - The ECS cluster to run the service in.command?
(Input<string[]>
) - Optional command to override the container’s default command.cpu?
(Input<number>
) - CPU limit in CPU units, where 1024 = 1 vCPU (defaults to 512).domain?
(Input<string>
) - Custom domain name for external access (requires loadBalancer).env?
(Record<string, Input<string>>
) - Environment variables to pass to the container.healthcheck?
({ command?: Input<string>; interval?: Input<number>; path?: Input<string>; retries?: Input<number>; startPeriod?: Input<number> }
) - Health check configuration for the container.image
(Input<string>
) - The Docker image to run (e.g., “nginx:latest”, “my-registry/my-app:v1.0”).init?
({ command: Input<string[]>; env?: Record<string, Input<string>>; image?: Input<string>; stopTimeout?: Input<number> }
) - Configuration for an init container that runs before the main container.loadBalancer?
(LoadBalancerWithListener
) - Load balancer configuration for external traffic routing. This must be passed ifdomain
is specified.logGroup?
(Input<string>
) - The name of a log group to write logs to. If not specified, a new log group will be created with a 30 day retention period.memory?
(Input<number>
) - Memory limit in MB (defaults to 512).name
(Input<string>
) - The name of the container and ECS task family.network
(NetworkInput
) - The VPC network configuration for the service.noPrefix?
(boolean
) - Whether to skip adding a prefix to resource names.orderedPlacementStrategies?
(Input<Input<ServiceOrderedPlacementStrategy>[]>
) - Service level strategy rules that are taken into consideration during task placement. List from top to bottom in order of precedence. Default behavior is to use thebinpack
strategy oncpu
.port?
(Input<number>
) - Port the container exposes (required for load balancer integration).replicas?
(Input<number>
) - Number of tasks to run (cannot be used with autoScaling).role?
(Input<string>
) - IAM role ARN for the task to assume (for AWS API access).securityGroups?
(Input<Input<string>[]>
) - Custom security groups for the service (uses VPC default if not specified).zone?
(Input<string>
) - Route53 hosted zone ID for the domain (auto-detected if not specified).
ServiceOutput
Section titled “ServiceOutput”Output from creating an ECS service, containing the service resource and URLs.
Properties
Section titled “Properties”internalUrl?
(Output<string>
) - Internal service discovery URL for VPC communication (only available ifport
is configured)service
(Service
) - The ECS service resource.url?
(Output<string>
) - External URL for the service (only available ifdomain
is configured).
TaskDefinitionArgs
Section titled “TaskDefinitionArgs”Configuration arguments for creating an ECS task definition.
Properties
Section titled “Properties”command?
(Input<string[]>
) - Optional command to override the container’s default command.cpu?
(Input<number>
) - CPU limit in CPU units, where 1024 = 1 vCPU (defaults to 512).env?
(Record<string, Input<string>>
) - Environment variables to pass to the container.healthcheck?
({ command?: Input<string>; interval?: Input<number>; path?: Input<string>; retries?: Input<number>; startPeriod?: Input<number> }
) - Health check configuration for the container.image
(Input<string>
) - The Docker image to run (e.g., “nginx:latest”, “my-registry/my-app:v1.0”).init?
({ command: Input<string[]>; env?: Record<string, Input<string>>; image?: Input<string>; stopTimeout?: Input<number> }
) - Configuration for an init container that runs before the main container.logGroup?
(Input<string>
) - The name of a log group to write logs to. If not specified, a new log group will be created with a 30 day retention period.memory?
(Input<number>
) - Memory limit in MB (defaults to 512).name
(Input<string>
) - The name of the container and ECS task family.noPrefix?
(boolean
) - Whether to skip adding a prefix to resource names.port?
(Input<number>
) - Port the container exposes (required for load balancer integration).role?
(Input<string>
) - IAM role ARN for the task to assume (for AWS API access).
ServiceInput
Section titled “ServiceInput”type ServiceInput = string | aws.ecs.Service | ServiceOutput