Lesson 2 of 4 AWS CodePipeline

Using Variables, Approvals and Stage Conditions

Real release processes need more than a straight line of actions: values must flow between actions, people must sometimes authorise a release, and a stage must sometimes be blocked, skipped or rolled back based on the health of the system. This lesson explains how CodePipeline variables, manual approvals, stage conditions and automatic rollback provide these controls.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain the difference between pipeline-level variables and action output variables, and how namespaces are used.
  • Describe how manual approval actions work, including notifications, timeouts and auditing.
  • Explain the entry, on-success and on-failure stage conditions and the rule types they use.
  • Describe how automatic stage rollback works and when to use it instead of a deployment service rollback.

Variables

Variables let an action use a value that is only known at run time, such as the commit ID, a version number computed by a build or a value supplied when the pipeline is started. There are two kinds.

Action output variables

Many actions emit output variables. Source actions emit values such as CommitId, BranchName, CommitMessage and the repository name; CodeBuild actions emit the variables listed under exported-variables in the buildspec; CloudFormation actions emit the stack outputs; and Lambda invoke actions can return output variables. To reference an action's variables, the action is given a namespace, and other actions reference #{Namespace.VariableName} in their configuration. For example, a deploy action can set a CloudFormation parameter to #{BuildVariables.IMAGE_TAG}.

Pipeline-level variables

V2 pipelines can declare variables at the pipeline level with default values. A value can be overridden when an execution is started manually or by the API, and actions reference them as #{variables.name}. They suit values such as a target environment name or a flag that decides whether to run optional tests. The variables used by each execution are recorded in the execution history.

Built-in execution variables are also available, such as #{codepipeline.PipelineExecutionId}, which is commonly used to make resource names or deployment descriptions unique per execution.

Manual Approval Actions

A manual approval action pauses an execution until an authorised person approves or rejects it. The action can publish a message to an Amazon SNS topic, so that approvers receive an email or a chat message, and it can include a URL for the approver to review, such as the staging environment or a change ticket, and custom comments. The approver uses the console, the CLI or the API PutApprovalResult, and the IAM principal needs codepipeline:PutApprovalResult on the action. Approval can therefore be restricted to a specific group or role through IAM.

If no response is received within seven days, the approval action fails and the execution stops. Rejection also fails the action. Approval and rejection, with the approver identity and comments, are recorded in the execution history, and the API call is recorded in AWS CloudTrail, which provides the audit trail that regulated organisations need.

Approval or condition?

A manual approval is the defining feature of continuous delivery. Use it when a scenario requires human sign-off. When a scenario wants to block deployment only while the system is unhealthy or outside permitted hours, a stage condition is more appropriate, because it needs no human action.

Scenario questions

Variables and approvals

Exam skills 1.1.5

Practise variable syntax and approval controls.

Log in to try this activity Interactive activities are free with an account.

Stage Conditions

Stage conditions, available in V2 pipelines, evaluate rules at defined points of a stage and take an action based on the result. A condition contains one or more rules, and each rule uses a rule provider. The main rule types are described first and then the three condition points.

  • CloudWatch alarm rule: checks that specified Amazon CloudWatch alarms are not in the ALARM state, optionally waiting for a period. It blocks or rolls back a release when the system is unhealthy.
  • Deployment window rule: allows the stage only within a time window defined by a cron expression and time zone, for example weekdays between 09:00 and 16:00.
  • Lambda invoke rule: invokes a Lambda function that returns success or failure, for custom checks such as querying a change calendar or a ticketing system.
  • Variable check rule: compares a pipeline variable with a value, for example running a stage only when the branch name is main.
  • Commands rule: runs shell commands whose exit code decides the result.

Entry conditions

Evaluated before an execution enters the stage. If the rules fail, the result is either FAIL, which fails the stage, or SKIP, which skips the stage and lets the execution continue. A deployment window rule as an entry condition with the FAIL result prevents deployments outside permitted hours.

On-success conditions

Evaluated after all actions in the stage succeed. If a rule fails, for example because an alarm entered the ALARM state during a bake period after deployment, the result is FAIL: the stage fails even though its actions succeeded. Combined with an on-failure condition that rolls back, this lets a stage watch the system for a period after deploying and reverse the change if the system degrades.

On-failure conditions

Evaluated when the stage fails. The result can be ROLLBACK, which redeploys the last successful revision, or RETRY, which retries failed actions or all actions automatically. Retry suits transient failures; rollback suits failed deployments.

The table below summarises the condition points and their possible results.

ConditionEvaluatedPossible results when rules fail
EntryBefore the stage startsFAIL or SKIP
On successAfter all actions succeedFAIL (pair with an on-failure ROLLBACK)
On failureAfter the stage failsROLLBACK or RETRY

The following diagram shows where each condition is evaluated in a stage.

Where the three stage conditions are evaluated. An entry condition runs before the stage starts and, if its rules fail, fails or skips the stage. An on-success condition runs after all actions succeed and can fail the stage, for example when an alarm fires during a bake period. An on-failure condition runs when the stage fails and either rolls back to the last successful revision or retries. Rules include CloudWatch alarm, deployment window, Lambda invoke, variable check and commands rules.
An on-success FAIL paired with an on-failure ROLLBACK reverses a change that degrades the system.
Sort into groups

Which stage condition?

Exam skills 1.1.5 · 1.4.4

Decide which kind of stage condition produces each result.

Log in to try this activity Interactive activities are free with an account.

Automatic Stage Rollback

Stage rollback in CodePipeline starts a new execution of the stage using the source revisions of the most recent execution that completed the stage successfully. It is configured as the ROLLBACK result of an on-failure condition and can also be started manually. Rollback requires that the pipeline is type V2 and that a previous successful execution exists for the stage.

Pipeline rollback is different from the rollback that deployment services provide. CodeDeploy rolls back a single deployment when its alarms fire or a deployment fails, and CloudFormation rolls back a stack update that fails. Those rollbacks happen inside one action. A pipeline stage rollback redeploys every action in the stage with the previous revisions, which is useful when a stage contains several actions, such as an infrastructure stack and an application deployment that must stay consistent.

Worked example

Guarding a Production Stage

A company requires that production deployments happen only between 08:00 and 17:00 on weekdays, that deployments never start while a production alarm is active, and that a release is reversed if the error-rate alarm fires within 30 minutes of deployment. A release manager must also authorise each release.

  1. Add a manual approval action as the first action of the Production stage, publishing to an SNS topic that the release managers subscribe to, and restrict PutApprovalResult to their role.
  2. Add an entry condition with a deployment window rule for weekdays 08:00 to 17:00 and a CloudWatch alarm rule for the production alarms, with the FAIL result.
  3. Add an on-success condition with a CloudWatch alarm rule that monitors the error-rate alarm for 30 minutes, with the FAIL result, so that an alarm during the bake period fails the stage.
  4. Add an on-failure condition with the ROLLBACK result, so that a stage failed by the bake-period alarm or by a failed deployment action returns to the last good revision.
Check yourself

A build action computes a semantic version that must become the Version parameter of a CloudFormation stack in a later stage. What must be configured?

Show answer

Export the variable in the buildspec, set a namespace on the CodeBuild action, and reference #{Namespace.VERSION} in the CloudFormation action's parameter overrides.

Exam Tips

Remember for the exam

  • Reference action output variables as #{Namespace.Variable}; pipeline-level variables (V2) as #{variables.name}, which can be overridden at start time.
  • Manual approvals time out and fail after 7 days; restrict who can approve with codepipeline:PutApprovalResult.
  • Stage conditions (V2): entry conditions FAIL or SKIP; on-success conditions FAIL; on-failure conditions ROLLBACK or RETRY.
  • Use a deployment window rule to block releases outside permitted hours and a CloudWatch alarm rule to block or roll back releases while the system is unhealthy.
  • Stage rollback redeploys the last successful source revisions for the whole stage; CodeDeploy and CloudFormation rollbacks act within a single action.