Hello, I am fairly new to ECS and I am wondering if someone can help explain to me what the difference between a Task role, Service role, and EC2 role. If a task runs on an ec2 instance then does that just mean that it has more granular permissions at the task level? Does it work the same is Service Role? If someone could help explain this to me I would greatly appreciate it. Thanks!
Clusters are made up of services. Services are made up of tasks. You then deploy the Clusters with Launch Configuration’s, tied to an auto scaling group.
EC2 roles define what the EC2 instances your Launch Definitions have permissions to do.
EC2 Role - If I’m guessing what you’re referring to, this is the role attached to the EC2 instance. This gives the EC2 instance permissions to access the AWS API dependent on what you allow. In an ECS environment, you don’t need much permissions.
Task Role is attached to your ECS task. This gives ECS Task the permissions assigned in the policy. Without a Task Role, your ECS tasks would default to the EC2 instance. This is generally a bad idea because if you have multiple apps that require different access, there’s no segmentation. By creating Task Role’s per ECS Service/Task, then you can segment what permissions each app has. For instance, you have a frontend and a backend. Only the backend needs permissions to write to S3 whereas the frontend only serves static assets.
Service Role - This role is attached to an ECS service and is typically used when you want to map your ECS Tasks to an Load Balancer. The ECS Service by default does not have permissions to do so, so you need to give it access. I generally have a single ECS Service Role for all LB actions and share that.
And lastly, there’s also the Execution Role. This is passed to the ECS agent to make API calls on your behalf. If you want to use Cloudwatch Logs as your docker log driver, I believe this is where the permissions are set to push your logs to Cloudwatch Logs.
The execution role also enables things like private registry secrets (stored in Secrets Manager), valuesFrom parameters for task secrets, and pulling images from ECR too.