Infrastructure as Code with Azure Bicep

Book description

The ultimate guide to becoming proficient in Azure Bicep while having a better development experience and making your Infrastructure as Code seamless and error free

Key Features

  • Learn Azure Bicep from an official Microsoft trainer
  • Master the authoring experience to make your Infrastructure as Code journey seamless
  • Go beyond writing good templates with the help of advanced tips and tricks

Book Description

It’s no secret that developers don’t like using JSON files to declare their resources in Azure because of issues such as parameter duplication and not being able to use comments in templates. Azure Bicep helps resolve these issues, and this book will guide you, as a developer or DevOps engineer, to get the most out of the Bicep language.

The book takes you on a journey from understanding Azure Resource Manager (ARM) templates and what their drawbacks are to how you can use Bicep to overcome them. You will get familiar with tools such as Visual Studio Code, the Bicep extension, the Azure CLI, PowerShell, Azure DevOps, and GitHub for writing reusable, maintainable templates. After that, you’ll test the templates and deploy them to an Azure environment either from your own system or via a continuous integration and continuous delivery (CI/CD) pipeline. The book features a detailed overview of all the Bicep features, when to use what, and how to write great templates that fit well into your existing pipelines or in a new one. The chapters progress from easy to advanced topics and every effort has been put into making them easy to follow with examples, all of which are accessible via GitHub.

By the end of this book, you’ll have developed a solid understanding of Azure Bicep and will be able to create, test, and deploy your resources locally or in your CI/CD pipelines.

What you will learn

  • Get started with Azure Bicep and install the necessary tools
  • Understand the details of how to define resources with Bicep
  • Use modules to create templates for different teams in your company
  • Optimize templates using expressions, conditions, and loops
  • Make customizable templates using parameters, variables, and functions
  • Deploy templates locally or from Azure DevOps or GitHub
  • Stay on top of your IaC with best practices and industry standards

Who this book is for

This book is for cloud engineers, developers, and DevOps engineers who are responsible for writing templates to deploy resources in Microsoft Azure and contributing to CI/CD pipelines. Professionals who want to get started with DevOps and Infrastructure as Code when it comes to working with Microsoft Azure will also benefit from reading this book. Readers are expected to have a basic understanding of CI/CD concepts, must have worked with ARM templates to deploy resources to Azure, and must have used or be familiar with Azure DevOps or GitHub Actions for their CI/CD pipelines.

Table of contents

  1. Infrastructure as Code with Azure Bicep
  2. Foreword
  3. Contributors
  4. About the author
  5. About the reviewer
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Share Your Thoughts
  7. Section 1: Getting Started with Azure Bicep
  8. Chapter 1: An Introduction to Azure Bicep
    1. Technical requirements
    2. What is Azure Bicep?
      1. IaC
      2. ARM templates
      3. Azure Bicep
    3. Why was it created?
      1. Why create a new revision?
      2. What about current third-party tools?
    4. How does it work?
      1. Authoring experience
      2. What happens to my ARM templates?
      3. Bicep CLI
    5. Summary
  9. Chapter 2: Installing Azure Bicep
    1. Technical requirements
    2. Cross-platform installation
      1. Installing the Azure CLI
      2. Installing Bicep using the Azure CLI
      3. Installing Bicep using Azure PowerShell
    3. Installation on Windows
      1. Installation using Windows Installer
      2. Installation using Chocolatey
      3. Installation using winget
      4. Manual installation using PowerShell
    4. Manual installation on macOS
      1. Installing Bicep via Homebrew
      2. Installing Bicep via Bash
    5. Manual installation on Linux
    6. Getting nightly builds
    7. Working with Bicep in a Docker container
      1. Working with an Azure CLI Docker container
    8. Summary
  10. Chapter 3: Authoring Experience
    1. Technical requirements
    2. Bicep extension for Visual Studio Code
      1. Installing Visual Studio Code
      2. Installing the Bicep extension
      3. Snippets offered by the Bicep extension
      4. Refactoring
      5. Formatting
      6. Using quick fixes
    3. Configuring the Bicep linter
      1. Customizing the linter
      2. Using the Bicep CLI to use the linter
    4. Bicep Playground
      1. Compiling Bicep snippets
      2. Decompiling an ARM template
      3. Using sample templates in Bicep Playground
    5. Summary
  11. Chapter 4: Compiling and Decompiling Bicep Files
    1. Technical requirements
    2. Compiling Bicep files
      1. Compiling a Bicep file using the Bicep CLI
      2. Compiling and deploying a Bicep file using the Azure CLI
      3. Compiling and deploying a Bicep file using Azure PowerShell
    3. Troubleshooting possible errors and warnings
      1. An invalid template
      2. Linter errors
    4. Decompiling ARM templates into Bicep
      1. Using the Azure CLI
      2. Using the Bicep CLI
      3. Potential fixes after decompiling
      4. Exporting templates and decompiling
    5. Summary
  12. Section 2: Azure Bicep Core Concepts
  13. Chapter 5: Defining Resources
    1. Technical requirements
    2. Resource definition
      1. Common properties
      2. Resource-specific properties
      3. Adding additional properties
    3. Referencing existing resources
      1. Referencing a resource that is in the same file
      2. Referencing an already deployed resource
      3. Explicit dependencies
      4. Visualizing dependencies
    4. Resource scopes
      1. Resource group
      2. Subscription
      3. Management group
      4. Tenant
      5. Setting the scope at the resource level
      6. Multi-scope Bicep files
      7. Global functions
    5. Bicep language specification
      1. Structure
      2. Whitespaces
      3. Comments
    6. Bicep type system
      1. Supported data types
      2. Simple types
      3. Objects
      4. Arrays
      5. Union types
    7. ARM and Bicep syntax comparison
      1. Best practices for Bicep syntax
    8. Summary
  14. Chapter 6: Using Parameters, Variables, and Template Functions
    1. Technical requirements
    2. Parameters
      1. Minimalistic definition
      2. Setting default values
      3. Using decorators
      4. Using parameters within a Bicep template
      5. Passing parameters during deployment
    3. Variables
      1. Defining variables
      2. Using variables
      3. Configuration variables
    4. Template functions
      1. The any function
      2. Date functions
      3. The bool function
      4. Deployment functions
      5. Numeric functions
      6. Array functions
      7. Object functions
      8. Resource functions
      9. String functions
    5. Summary
  15. Chapter 7: Understanding Expressions, Symbolic Names, Conditions, and Loops
    1. Technical requirements
    2. Understanding expressions
      1. Unary operators
      2. Comparison operators
      3. Logical operators
      4. Numeric operators
      5. Accessor operators
      6. Operator precedence and associativity
    3. Exploring conditions
      1. Deploy condition
      2. New or existing resource
      3. Runtime checks
    4. Diving into resource iterations
      1. Syntax
      2. Loop index
      3. Loop array
      4. Loop array and index
      5. Nested loops with conditions
      6. Using batches
    5. Summary
  16. Chapter 8: Defining Modules and Utilizing Outputs
    1. Technical requirements
    2. Modules
      1. Defining modules
      2. Consuming a module
      3. Additional considerations
      4. Defining and configuring module scopes
    3. Using Bicep outputs
      1. Defining outputs
      2. Conditional outputs
      3. Returning multiple outputs with loops
      4. Module outputs
      5. Getting output values after deployment
    4. Summary
  17. Section 3: Deploying Azure Bicep Templates
  18. Chapter 9: Deploying a Local Template
    1. Technical requirements
    2. Deploying Bicep with Azure PowerShell
      1. Connecting to your Azure environment
      2. Previewing changes with the what-if operation
      3. Passing parameters
      4. Deployment scopes
    3. Deploying Bicep with the Azure CLI
      1. Connecting to your Azure environment
      2. Preview changes with what-if operation
      3. Passing parameters
      4. Deployment scopes
    4. Troubleshooting possible errors and warnings
      1. Compile-time errors and warnings
      2. Deployment-time errors
    5. Summary
  19. Chapter 10: Deploying Bicep Using Azure DevOps
    1. Technical requirements
    2. Creating the Azure DevOps pipeline
      1. Prerequisites
      2. Creating a pipeline in Azure DevOps
    3. Adding a validation step to our pipeline
      1. Validating the deployment
    4. Deploying the template
      1. Adding the deployment step
      2. Pushing the changes to your repository
    5. Accessing the deployment output in the pipeline
      1. Receiving outputs from a deployment
      2. An alternative approach to accessing deployment outputs
    6. Summary
  20. Chapter 11: Deploying Bicep Templates Using GitHub Actions
    1. Technical requirements
    2. Creating a GitHub action
      1. Prerequisites
      2. Creating the workflow from code
    3. Adding validation steps to the workflow
      1. Adding an Azure CLI action to the workflow
    4. Adding deployment steps to the workflow
    5. Using the official Azure ARM action
      1. Adding the Azure ARM action to the workflow
    6. Accessing deployment outputs for later use
      1. Accessing outputs from the Azure CLI action
      2. Accessing outputs from the Azure ARM action
    7. Summary
  21. Chapter 12: Exploring Best Practices for Future Maintenance
    1. Technical requirements
    2. Applying version control and code flows for your IaC
      1. Single pipeline approach
      2. Separate pipelines for code and infrastructure
      3. Source control everything
    3. Idempotency and its importance
      1. Idempotency
      2. Immutability
      3. Modularity and microservices
    4. Bicep best practices
      1. Parameters
      2. Variables
      3. Resource definitions
      4. Outputs
      5. Configuration set
      6. Shared variable file
    5. Managing service version updates
    6. Azure Bicep's known limitations
      1. Single-line objects and arrays
      2. Newline sensitivity
      3. No support for API profiles
      4. CLI limitations
    7. Summary
    8. Why subscribe?
  22. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts

Product information

  • Title: Infrastructure as Code with Azure Bicep
  • Author(s): Yaser Adel Mehraban
  • Release date: February 2022
  • Publisher(s): Packt Publishing
  • ISBN: 9781801813747