0%
AWS Developer Associate (DVA-C03) Exam Prep: Development, Security, Testing, Deployment and Troubleshooting
How the Exam Works
Domain 1: Development with AWS Services
Domain 2: Security
Domain 4: Troubleshooting and Optimization
New in DVA-C03: Containers and AI
Final Review and Mock Exams
Infrastructure as Code: CloudFormation, SAM, CDK and Beanstalk
Infrastructure as code means you describe your AWS resources in files, keep those files in version control next to the application, and let a service create and update the real resources to match. On the exam this is mostly CloudFormation, plus three tools built on or around it: the Serverless Application Model, the Cloud Development Kit and Elastic Beanstalk. The Deployment domain asks you to read templates, choose the right intrinsic function, predict what an update will do and debug a failed stack. This lesson covers those mechanics from a developer's seat. The previous lesson covered the pipelines that run these deployments, and the next lesson covers rollout strategies such as canary and blue green, including Beanstalk's deployment policies.
Why templates beat clicking
A template is repeatable. The same file creates a development stack this morning and an identical production stack this afternoon, in another Region if you like. It is reviewable: a pull request shows that someone is opening port 22 to the world before it happens. It is reversible: a stack can be deleted as a unit, and a failed update rolls back. And it is documented by construction, because the template is the description of what exists. The exam reflects all of this. When a question asks how to recreate an environment consistently across accounts or Regions, or how to review infrastructure changes before they happen, the answer involves CloudFormation.
Anatomy of a template
A template is a YAML or JSON document with a set of top level sections. Only Resources is required. The others are optional, and the exam expects you to know what each is for.
Parameters are inputs supplied at stack creation or update. Mappings are static lookup tables inside the template. Conditions are booleans computed from parameters that decide whether a resource is created or which value a property gets. Resources are the things to build. Outputs are values the stack publishes, optionally exported for other stacks. Two more sections appear less often: Metadata, which holds extra information such as cfn-init configuration or console parameter grouping, and Transform, which names a macro such as the SAM transform. Rules validate combinations of parameters before the stack is created.
Resources, logical IDs and physical IDs
Each resource has a logical ID, the key you write in the template, such as OrdersQueue, and a Type, such as AWS::SQS::Queue. When CloudFormation creates it, the resource gets a physical ID, the real name or ARN in your account. If you do not set a name property, CloudFormation generates one from the stack name, the logical ID and a random suffix. That is usually what you want. Naming resources explicitly, such as setting TableName, makes them easier to find but prevents CloudFormation from replacing them during an update, because it cannot create a second table with the same name before deleting the first. An update that needs replacement then fails, and a stack cannot be deployed twice in one account and Region.
CloudFormation works out the order of creation from dependencies. When a function's Role property uses the role's ARN through GetAtt, CloudFormation knows the role must exist first. Resources without dependencies are created in parallel. When there is a dependency that the template does not show, such as an EC2 instance that needs an internet gateway attachment to complete its user data, you add DependsOn to make it explicit.
Parameters in practice
Parameters have types. String, Number, a comma delimited list and lists of numbers are the generic ones. The AWS specific types are more useful: AWS::EC2::KeyPair::KeyName, AWS::EC2::VPC::Id, AWS::EC2::Subnet::Id and similar make the console show a drop down of real values and make CloudFormation reject values that do not exist. SSM parameter types, such as AWS::SSM::Parameter::Value with a String or an AMI ID type, take a Parameter Store name and resolve its current value at deploy time, which is the standard way to always use the latest Amazon Linux AMI without editing the template.
Constraints catch mistakes early: AllowedValues, AllowedPattern, MinLength, MaxLength, MinValue and MaxValue. NoEcho masks a parameter's value in the console and API output, but it is not a secret store, because the value can still end up in places such as resource properties that are visible. For secrets, use dynamic references to Secrets Manager or Parameter Store, covered in the next section.
Pseudo parameters
CloudFormation provides a few values without you declaring them. AWS::Region and AWS::AccountId are the most used, along with AWS::StackName, AWS::StackId, AWS::Partition, AWS::URLSuffix and AWS::NoValue. They let one template work in any account and Region: building an ARN with the account ID and Region pseudo parameters instead of hard coding them is a habit the exam rewards. AWS::NoValue is special: returned from an If function, it removes the property entirely, which is how you make a property conditional.
Mappings and conditions
Mappings suit values that differ by a known key, such as instance sizes per environment or AMI IDs per Region. You read them with FindInMap, giving the map name, the top level key and the second level key. They are fixed in the template, so they cannot be changed at deploy time except by changing which key you look up, usually with a parameter or a pseudo parameter as the key.
Conditions let one template serve several environments. A condition is declared once, using Equals, And, Or and Not, and then used in three places: a Condition attribute on a resource or output to create it only when true, and the If function to choose between two property values. The classic exam example is a template that creates a read replica or enables point in time recovery only when the environment parameter is prod.
Stack lifecycle and status values
A stack moves through status values that the exam expects you to read. CREATE_IN_PROGRESS becomes CREATE_COMPLETE on success. If creation fails, the default is to roll back and delete what was created, leaving ROLLBACK_COMPLETE, a stack that can only be deleted, not updated. Updates go through UPDATE_IN_PROGRESS and UPDATE_COMPLETE, or on failure UPDATE_ROLLBACK_IN_PROGRESS and UPDATE_ROLLBACK_COMPLETE, which leaves the stack working at its previous configuration. The rare and awkward state is UPDATE_ROLLBACK_FAILED, where even the rollback could not finish, often because a resource was changed or deleted outside CloudFormation. The fix is to correct the underlying problem and use continue update rollback, optionally skipping the resources that cannot roll back.
The stack events tab is the debugging tool. It lists every resource status change with a reason. When a stack fails, scroll to the first FAILED event in time order; later failures are usually resources cancelled because of the first one. The reason text usually names the problem directly: a permission the deploying role lacks, an invalid property value, a name that already exists or a service quota.
For development, you can disable rollback on failure so failed resources are left in place for inspection, then fix and retry. That speeds up iteration but should not be used for production stacks.
Who CloudFormation acts as
By default CloudFormation creates resources with the permissions of the person or role that called it. You can instead pass a service role, which CloudFormation assumes for all operations on the stack. That lets a developer deploy a stack that creates IAM roles without having IAM permissions themselves, as long as they have iam:PassRole for the service role. This is a common least privilege answer: give developers permission to use CloudFormation with a specific service role, and give that role the permissions to build the resources.
When a template creates IAM resources, you must acknowledge it with a capability: CAPABILITY_IAM for unnamed IAM resources, CAPABILITY_NAMED_IAM when the template names them, and CAPABILITY_AUTO_EXPAND when it uses macros or nested stacks with transforms. Without the right capability, the create fails immediately with an InsufficientCapabilities error.
Validating templates before you deploy
Catching a template error at deploy time costs a few minutes per attempt, so check templates earlier. aws cloudformation validate-template checks syntax and structure but not whether property values are valid for a resource. cfn-lint, an open source linter, checks property names, types and allowed values against the resource specification, flags unused parameters and warns about patterns such as hard coded Regions. Running cfn-lint in the build stage of a pipeline, next to unit tests, stops most broken templates before they reach CloudFormation. For policy checks, such as no public buckets or encryption required on every volume, tools that evaluate templates against rules can run in the same stage, and CloudFormation Hooks can enforce such rules at deploy time across an account.
When a stack fails, sort the events by time and find the first resource that failed. The reason on that event is the real cause. The later failures are usually other resources that were cancelled because of it.