- Newest
- Most votes
- Most comments
Hello.
You can specify subnets and security groups by specifying RunTask API parameters in the following format:
If the subnet is a private subnet and a route to the NAT Gateway is configured, it will still work even if you remove "AssignPublicIp".
https://docs.aws.amazon.com/ja_jp/AmazonECS/latest/APIReference/API_RunTask.html
{
"Comment": "execute an ECS task",
"StartAt": "Run an ECS Task",
"States": {
"Run an ECS Task": {
"Type": "Task",
"Resource": "arn:aws:states:::ecs:runTask.sync",
"Parameters": {
"LaunchType": "FARGATE",
"Cluster": "arn:aws:ecs:ap-northeast-1:<AWS Account ID>:cluster/<ECS Cluster Name>",
"TaskDefinition": "arn:aws:ecs:ap-northeast-1:<AWS Account ID>:task-definition/<Task Definition Name>:<Revision Number>",
"NetworkConfiguration": {
"AwsvpcConfiguration": {
"Subnets": [
"<Subnet ID>"
],
"SecurityGroups": [
"<Security Group ID>"
],
"AssignPublicIp": "ENABLED"
}
}
},
"Overrides": {
"ContainerOverrides": [
{
"Name": "<Container Name>",
"Command.$": "$.commands"
}
]
}
},
"Retry": [
{
"ErrorEquals": [
"States.ALL"
],
"IntervalSeconds": 1,
"MaxAttempts": 2,
"BackoffRate": 3
}
],
"End": true
}
}
}
When orchestrating ECS tasks using AWS Step Functions, you can configure VPC and security group settings through the NetworkConfiguration parameter in your state machine definition. This is not configured in the task definition itself, but rather when you invoke the ECS RunTask action from Step Functions.
In your Step Functions state machine, you can specify the network configuration using the NetworkConfiguration field with the following structure:
- Use
AwsvpcConfigurationto specify your VPC settings - Include
Subnetsto define which subnets your Fargate task should run in - Include
SecurityGroupsto attach the appropriate security groups - Optionally set
AssignPublicIpto "ENABLED" or "DISABLED" depending on whether your task needs a public IP address
This network configuration is passed as part of the Overrides or directly in the ECS RunTask parameters when Step Functions calls the ECS API. The VPC and security group configuration is applied at task runtime rather than being defined in the task definition itself, which allows you to use the same task definition across different network environments.
For Fargate tasks specifically, the awsvpc network mode is required, and you must specify the network configuration when running the task through Step Functions.
Sources
Run Amazon ECS or Fargate tasks with Step Functions - AWS Step Functions
aws-fargate-stepfunctions - AWS Solutions Constructs
Relevant content
- asked 2 years ago
- AWS OFFICIALUpdated 2 years ago
