Introduction
Your container needs configuration and access to application data. Give each operation the right identity: the ECS execution role injects a Secrets Manager value before startup, while the task role lets application code read one S3 object. You will test the permitted read and three denied operations through the running application, then remove your task and added permissions.
Complete the earlier ECS task lab and IAM policy basics first. This lab starts independently in a fresh VM. A supplied image, network, log group, two basic roles, one Secret and one S3 bucket are ready. No earlier VM resources or personal AWS account are needed. Use Terminal for commands and AWS View for the application and logs. Keep this terminal open so its saved variables remain available.
Certification Relevance
| Certification | Exam task | Practice |
|---|---|---|
| Developer – Associate (DVA-C02) | Task 2.1, Skill 2.1.6 | Define separate, resource-scoped permissions for IAM principals. |
| Developer – Associate (DVA-C02) | Task 2.3, Skill 2.3.3 | Use Secrets Manager to supply application configuration without putting its value in a task definition. |
Lab Overview
The execution role reads the Secret during startup. The application receives that value and uses a different task role for signed AWS API requests. The task role can read allowed.txt; it cannot read reference.txt, retrieve the Secret through its API or obtain an ECR authorization token.

Inspect the Two Container Identities
In this step, inspect the supplied roles and inputs, then create a cluster for your task.
An IAM role provides temporary permissions to a service or application. Both supplied roles trust ECS tasks, but their jobs differ. The execution role is used by the task infrastructure to pull the image, deliver logs and inject secrets. The task role is used by the application SDK when its code calls AWS APIs. Giving a permission to one role does not give it to the other.
Move to the workspace and save the role ARNs. An ARN identifies a particular AWS resource. $(...) captures command output, and --query with --output text selects the value without JSON quotes:
cd /home/labex/project
EXECUTION_ROLE=$(aws iam get-role --role-name ct04-execution \
--query 'Role.Arn' --output text)
echo "$EXECUTION_ROLE"
Save and display TASK_ROLE:
TASK_ROLE=$(aws iam get-role --role-name ct04-task \
--query 'Role.Arn' --output text)
echo "$TASK_ROLE"
Inspect existing inline policies. An inline policy is a permissions document attached to one identity:
aws iam list-role-policies --role-name ct04-execution
Inspect the named inline policy:
aws iam get-role-policy --role-name ct04-execution \
--policy-name execution --query PolicyDocument
Inspect the selected role’s inline policies:
aws iam list-role-policies --role-name ct04-task
The execution role has execution, containing image-pull and log-write permissions. The task role has no inline policies yet. You will add only the two permissions needed for this application.
Secrets Manager stores a named sensitive value. Read its metadata and save its ARN; describe-secret does not return the value. Never paste the value into your terminal, task definition or logs:
SECRET_ARN=$(aws secretsmanager describe-secret --secret-id ct04-config \
--query ARN --output text)
Inspect the Secret’s metadata without its value:
aws secretsmanager describe-secret --secret-id "$SECRET_ARN" \
--query '{Name:Name,ARN:ARN}'
Save the supplied image URI and digest. The digest fixes the exact image bytes that this task will run:
REPOSITORY_URI=$(aws ecr describe-repositories \
--repository-names ct04-app \
--query 'repositories[0].repositoryUri' --output text)
echo "$REPOSITORY_URI"
Save and display IMAGE_DIGEST:
IMAGE_DIGEST=$(aws ecr describe-images \
--repository-name ct04-app --image-ids imageTag=release-1 \
--query 'imageDetails[0].imageDigest' --output text)
echo "$IMAGE_DIGEST"
The supplied network file selects the subnet and security group for awsvpc networking. Inspect it, then create your cluster:
cat network.json
Create the exercise ECS cluster:
aws ecs create-cluster --cluster-name ct04-demo \
--query 'cluster.{Name:clusterName,Status:status}'
Expect ct04-demo with ACTIVE status. Preserve ct04-reference, the supplied roles and data. Your cleanup inventory is ct04-demo, its task definition and task, plus the two inline policies you will add.
Grant Only the Required Permissions
In this step, allow the execution role to retrieve the selected Secret and the task role to read one S3 object.
IAM policy statements connect an Action, such as s3:GetObject, to a Resource, such as an object ARN. An Allow statement grants the specified operation. Operations without an applicable allow are denied by default. Avoid Action: "*" or Resource: "*" for these two resource-specific permissions.
Write the secret-injection policy. cat > filename <<EOF is a here-document: it writes the following lines to a file until EOF. The shell expands $SECRET_ARN, so this file contains the selected ARN, never the Secret value:
cat > inject-config.json <<EOF
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": "secretsmanager:GetSecretValue",
"Resource": "$SECRET_ARN"
}]
}
EOF
Attach this policy to the execution role. file:// tells the CLI to read the JSON document from a local file:
aws iam put-role-policy --role-name ct04-execution \
--policy-name inject-config --policy-document file://inject-config.json
S3 stores objects in buckets. Here, ct04-data contains allowed.txt and an unrelated reference.txt. An object ARN ends with its key. Grant the application read access to exactly allowed.txt:
cat > read-data.json <<'EOF'
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::ct04-data/allowed.txt"
}]
}
EOF
The quoted 'EOF' keeps this document literal; it has no shell variables to expand. Attach it to the task role:
aws iam put-role-policy --role-name ct04-task \
--policy-name read-data --policy-document file://read-data.json
Read back both saved policies:
aws iam get-role-policy --role-name ct04-execution \
--policy-name inject-config --query PolicyDocument
aws iam get-role-policy --role-name ct04-task \
--policy-name read-data --query PolicyDocument
Check the single action and exact resource in each document. reference.txt is outside the allowed S3 object ARN. The task role has no Secrets Manager or ECR permission. You will test these boundaries through actual application requests.
Start a Task with a Secret and a Task Role
In this step, register a task definition with both roles and launch the configured container.
A task definition describes the container, image, compute, network and logs. executionRoleArn supplies infrastructure permissions; taskRoleArn supplies application permissions. The secrets entry names the environment variable APP_CONFIG and points to the Secret ARN. ECS resolves its value during startup. Ordinary environment entries below contain only a bucket name and an ARN.
Write the task definition using the saved variables. This application listens on port 8000, needs 256 CPU units and 512 MiB of task memory, and sends stdout through awslogs to the supplied log group:
cat > task-definition.json <<EOF
{
"family": "ct04-demo",
"networkMode": "awsvpc",
"requiresCompatibilities": ["FARGATE"],
"cpu": "256",
"memory": "512",
"executionRoleArn": "$EXECUTION_ROLE",
"taskRoleArn": "$TASK_ROLE",
"containerDefinitions": [{
"name": "app",
"image": "$REPOSITORY_URI@$IMAGE_DIGEST",
"essential": true,
"memory": 256,
"portMappings": [{"containerPort": 8000, "protocol": "tcp"}],
"environment": [
{"name": "DATA_BUCKET", "value": "ct04-data"},
{"name": "SECRET_ARN", "value": "$SECRET_ARN"}
],
"secrets": [{"name": "APP_CONFIG", "valueFrom": "$SECRET_ARN"}],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/ecs/ct04",
"awslogs-region": "us-east-1",
"awslogs-stream-prefix": "ecs"
}
}
}]
}
EOF
Do not add access keys or the Secret value. The application SDK obtains temporary task-role credentials automatically. The execution-role credentials are used by the task infrastructure and are not supplied to application code.
Register the definition and save its full ARN:
DEFINITION=$(aws ecs register-task-definition \
--cli-input-json file://task-definition.json \
--query 'taskDefinition.taskDefinitionArn' --output text)
echo "$DEFINITION"
Launch one task using the supplied network. file://network.json provides the JSON network configuration:
TASK_ARN=$(aws ecs run-task --cluster ct04-demo \
--launch-type FARGATE --task-definition "$DEFINITION" \
--network-configuration file://network.json \
--query 'tasks[0].taskArn' --output text)
echo "$TASK_ARN"
Wait for running status and inspect the two role references in the registered definition:
aws ecs wait tasks-running --cluster ct04-demo --tasks "$TASK_ARN"
Inspect the selected ECS tasks:
aws ecs describe-tasks --cluster ct04-demo --tasks "$TASK_ARN" \
--query 'tasks[0].{Status:lastStatus,Image:containers[0].imageDigest}'
Inspect the task-definition revision:
aws ecs describe-task-definition --task-definition "$DEFINITION" \
--query 'taskDefinition.{ExecutionRole:executionRoleArn,TaskRole:taskRoleArn,Secrets:containerDefinitions[0].secrets}'
Expect RUNNING, the selected image digest, and the APP_CONFIG Secret ARN. AWS View shows the running task. A missing secret-read permission on the execution role prevents startup; an S3 permission on the task role cannot substitute for it.
Test the Application Permission Boundary
In this step, request the application and compare its successful data read with its denied API calls.
In AWS View, select Request application. The supplied application makes signed SDK requests using its task role. Read these fields in the actual response:
| Response field | Expected observation | Meaning |
|---|---|---|
config_loaded |
true |
The startup-injected configuration is available, without exposing its value. |
data |
container-role-data |
The task role can read allowed.txt. |
caller_arn |
Contains assumed-role/ct04-task/ |
The application uses the task role. |
reference_denial |
An access-denied code | Reading reference.txt is outside the policy. |
secret_api_denial |
An access-denied code | Receiving an injected value does not grant the application GetSecretValue. |
ecr_denial |
An access-denied code | The application cannot use the execution role's image-pull permission. |
An UNEXPECTED_ALLOW value means the corresponding permission is too broad. A connection error is not proof of a denied permission. This application reports actual API authorization results and returns a successful response only when it can read its required data.
Select Read CloudWatch Logs. Look for served scoped application request. You can also read the same task's stream from Terminal. ${TASK_ARN##*/} keeps the text after the final slash, producing the task ID:
TASK_ID=${TASK_ARN##*/}
echo "$TASK_ID"
Inspect the actual application request logs:
aws logs get-log-events --log-group-name /ecs/ct04 \
--log-stream-name "ecs/app/$TASK_ID" \
--query 'events[].message'
The request and its HTTP 200 entry should appear after a short delivery delay. Neither the response nor logs should show the Secret value or credentials. Click Request application again to make another real request if needed.
Temporary credentials are refreshed automatically by the SDK and task infrastructure. You do not distribute permanent keys to this container. Secret injection, however, happens at startup: changing a stored Secret does not update an already-running container's environment. A new task is needed to load an updated value. This lab uses the supplied value throughout.

Example from AWS View: configuration is loaded, the permitted object is read, and the application receives real authorization denials. The public response contains no Secret value or credentials.
Remove Your Task and Added Permissions
In this step, stop and remove your task resources, then remove only the two inline policies you added.
Stop the task before removing its permissions. This preserves a clear cleanup order and lets the infrastructure finish delivering logs:
aws ecs stop-task --cluster ct04-demo --task "$TASK_ARN" \
--reason "Roles and secrets exercise complete" \
--query 'task.{Status:lastStatus,Reason:stoppedReason}'
Wait for ecs tasks-stopped:
aws ecs wait tasks-stopped --cluster ct04-demo --tasks "$TASK_ARN"
Deregister your definition and delete your now-empty cluster:
aws ecs deregister-task-definition --task-definition "$DEFINITION" \
--query 'taskDefinition.status'
Remove only the exercise ECS cluster:
aws ecs delete-cluster --cluster ct04-demo \
--query 'cluster.{Name:clusterName,Status:status}'
Expect INACTIVE for both. Stopped tasks and inactive definitions can remain as historical metadata; they are not running compute.
Remove the two policies by their exact names. Preserve the supplied execution policy and both basic roles:
aws iam delete-role-policy --role-name ct04-execution \
--policy-name inject-config
aws iam delete-role-policy --role-name ct04-task \
--policy-name read-data
Check that no task is running, the original policy state is restored and supplied reference inputs still exist:
aws ecs list-tasks --cluster ct04-demo --desired-status RUNNING
Inspect the selected role’s inline policies:
aws iam list-role-policies --role-name ct04-execution
Inspect the selected role’s inline policies:
aws iam list-role-policies --role-name ct04-task
Inspect the Secret’s metadata without its value:
aws secretsmanager describe-secret --secret-id ct04-config --query Name
Inspect the exercise and reference cluster states:
aws ecs describe-clusters --clusters ct04-reference \
--query 'clusters[0].{Name:clusterName,Status:status}'
Expect an empty taskArns array, only execution on the execution role, no inline policies on the task role, Secret name ct04-config, and an ACTIVE reference cluster. Preserve the bucket, both objects, Secret, ECR image, network and log group. AWS View no longer offers a running application response.
Summary
You separated infrastructure permissions from application permissions, granted access to a specific Secret and S3 object, and configured an ECS task with both roles. The running application confirmed secret injection, its task-role identity, a successful object read and three denied operations. You then stopped the task, removed your task definition and cluster, and restored the supplied roles' original policy state.
For further reading, see the AWS reference on Application permissions with task roles; Startup secret injection and refresh limits.



