Testing and Deployment · Topic 4 of 6

Containers and Infrastructure as Code

Deploy containers with ECS task definitions, Fargate and EKS manifests, and define stacks with CloudFormation and AWS SAM templates for DVA-C03.

  • 5 min
In a nutshell
  • A task definition is versioned in revisions, and the service points at one revision.
  • The task role is for application code, and the task execution role is for ECS agent actions.
  • Fargate needs awsvpc networking and task level CPU and memory.

What it is

Container orchestration services run your images for you, keep the desired number of copies alive and replace them during updates. Infrastructure as code (IaC) describes the surrounding resources in a template, so the same environment can be created again and reviewed like source code.

The two ideas combine: a template creates the cluster, task definition and service, and a pipeline updates the image tag.

Image in ECRreferenced byTask definitionrun byECS serviceFargate or EC2created byCloudFormation or SAM template

How it works

Amazon ECS task definitions

A task definition is a JSON blueprint for your containers. It sets the image, CPU and memory, port mappings, environment variables, secrets, log configuration and the roles. Each change registers a new numbered revision of the task definition family.

Two roles are often confused:

RoleUsed byTypical permissions
Task execution roleThe ECS agentPull the image from ECR, write logs to CloudWatch, read secrets referenced in the definition
Task roleYour application codeCalls your code makes, such as DynamoDB or S3 access

The secrets field injects a value from Secrets Manager or Parameter Store into a container as an environment variable, using the valueFrom ARN. This keeps the secret out of the image.

Launch types

With the Fargate launch type you set task level CPU and memory and AWS manages the servers. Fargate tasks use the awsvpc network mode, so each task gets its own network interface. With the EC2 launch type you run and patch the container instances yourself, which gives more control over instance types and daemon workloads.

ECS services and rolling updates

An ECS service keeps a desired count of tasks running and can register them with a load balancer. The default rolling update replaces tasks gradually, controlled by two values:

  • minimumHealthyPercent is how much capacity must stay running during the update.
  • maximumPercent is how far above the desired count the service may go.

The deployment circuit breaker stops a deployment that keeps failing and can roll back to the last completed deployment. ECS also supports blue/green deployments, where the new revision runs beside the old one and traffic shifts after validation.

Amazon EKS manifests

On EKS you describe the application in Kubernetes YAML manifests. A Deployment declares the image, replicas and update strategy, and a Service exposes the pods. You apply them with kubectl apply -f. The default RollingUpdate strategy uses maxSurge and maxUnavailable, and kubectl rollout undo returns to the previous revision. Pods get AWS permissions through EKS Pod Identity or IAM roles for service accounts, not through the node role.

CloudFormation templates

A template is JSON or YAML. Only the Resources section is required. The others are common:

  • Parameters accept values at deploy time.
  • Mappings hold fixed lookup tables.
  • Conditions switch resources on or off.
  • Outputs return values and can be exported for other stacks.

Useful intrinsic functions are !Ref, !GetAtt, !Sub, !Join, !If and !ImportValue. A change set previews an update, and drift detection finds resources changed outside the stack.

AWS SAM

AWS SAM is a shorthand on top of CloudFormation. The line Transform: AWS::Serverless-2016-10-31 makes CloudFormation expand SAM resource types such as AWS::Serverless::Function, AWS::Serverless::Api, AWS::Serverless::HttpApi and AWS::Serverless::SimpleTable.

A short Events block on a function creates the API route or queue mapping for you, and AutoPublishAlias publishes a version and alias on each deploy.

sam buildsam deployuploads packageCloudFormationexpands the transformStack resources

The AWS CDK is the code based alternative. You write constructs in a programming language, run cdk synth to produce a CloudFormation template, and cdk deploy to create the stack. A one time cdk bootstrap prepares each account and Region.

How the exam asks about this

  • "A container must read a database password without storing it in the image." -> Reference the secret in the task definition secrets field.
  • "The ECS agent cannot pull from ECR." -> The task execution role lacks ECR permissions.
  • "Define a Lambda function and API with the least template code." -> An AWS SAM template.

What to remember for the exam

  • A task definition is versioned in revisions, and the service points at one revision.
  • The task role is for application code, and the task execution role is for ECS agent actions.
  • Fargate needs awsvpc networking and task level CPU and memory.
  • Rolling updates are tuned with minimumHealthyPercent and maximumPercent.
  • Only Resources is mandatory in a CloudFormation template.
  • SAM needs the AWS::Serverless-2016-10-31 transform, and CDK synthesizes to CloudFormation.
Watch out

Do not give the application permissions to the task execution role. The code uses the task role, so permissions placed on the wrong role are never applied to your calls.

Common mistakes

Mixing up the two ECS roles. An image pull failure points to the execution role, and an AccessDenied from your own code points to the task role.

Hand editing resources that CloudFormation owns. The change creates drift and may be overwritten at the next update.

Using Fargate settings with the EC2 launch type assumptions. On Fargate you cannot choose instances or run daemons on a host, because there is no host to manage.

Check yourself

  • Which ECS role pulls the image from ECR?

    The task execution role.

  • Which CloudFormation template section is required?

    Resources.

  • What does the SAM transform do?

    It expands the serverless shorthand resource types into standard CloudFormation resources.

Topic 4 of 6 in Testing and Deployment

Test yourself on Testing and Deployment

The full AWS DVA-C03 question bank has 850 questions, with timed practice exams and an explanation for every answer.

This study guide is independent and is not affiliated with, endorsed by, or sponsored by AWS. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.