Part 1b – azd-ify an App Template with existing Bicep files
Published Mar 22 2023 09:22 AM 2,371 Views
Microsoft

If you've followed Part 1 and Part 2 of the azdev-ify or azd-ify series and are wondering whether you need to completely rework your Bicep files to `azd up` your app, you're in luck! The solution may be simpler than you think.

 

In this post, I'll walk you through the steps involved, which include:

  • creating an "azure.yaml" file via `azd init`
  • adding a "main.parameters.json" file
  • properly tagging your resource group and resources with `azd-env-name`
  • for deployment targets, add the corresponding `azd-service-name` tag as defined in "azure.yaml"

To demonstrate the process, I collaborated with my colleague Farhat Saleem in the Customer Success Team to azd-ify a sample integration app template that guides users through the process of setting up API Management policy for sending data to Azure Service Bus. This App Template already included all the necessary components (including Bicep files) and can be easily automated using GitHub Actions or other CI/CD services. We wanted to give customers an `azd up` option to get started quickly and easily.

 

architectureDiagram.png

Step 1 – run `azd init`

Just like starting a new app without any IaC files, run the `azd init` command to initialize the project. This command adds an “azure.yaml” file. (We are continuously working on enhancing the scaffolding of this file. If you have any suggestions or feedback, feel free to leave us a comment!)

 

We need to deploy a Function app to Azure Functions, so the "azure.yaml" file is edited as follows:

azureyaml.png

Additional note for 1.png: since the Function app host is defined in function.bicep for this app template, we made sure we used the same value for the `azd-service-name` tag here.

 

Step 2 – Create and add a main.parameters.json file

The "main.parameters.json" is required. However, if any required parameter is not defined (e.g., publisherEmail, publisherName), azd will prompt for input when you run `azd provision` or `azd up`.

 

mai_parameters_json.png

 

Step 3 - Edit the Bicep files

Don't be intimidated by this step. Let's dive in and explore together.

 

As all Bicep files are already located in the "/infra" directory, there's no need to go through the additional step of moving the files.

 

In “main.bicep” (note: left panel is the original file while the right panel is the desired file):

beforeandafter.png

1.png "name” was used as a suffix to all resources created. We decided to use environmentName.

2.png azd automatically creates a new resource group so we could just use the environment variable location.

3.png We declared a variable “tags” for tagging all resources with “azd-env-name: <environmentName>”.

 

For the rest of main.bicep:

4.png Tagged the resource: rg and modules: apim, serviced, cosmosdb, function.

 

Below screenshot show the edits for rg and apim:

rgAPIMBicep.png

5.png add “tags” as a parameter and setting to the files: "apim.bicep", "cosmosdb.bicep", "function.bicep", and "service-bus.bicep" in the subfolder “modules”.

 

A sample (service-bus.bicep):

 

servicebusBicep.png

 

Since the functionApp resource defined in function.bicep serves as a host for the function app,  we tag it with `azd-service-name: sbtocosmos`  here so that azd knows to deploy the function app to this Azure resource:

tag.png

 

As you can see, the service name is the same as what’s defined in "azure.yaml".

 

Finally, since we can now use `azd deploy` to deploy the function app, we can remove some lines in "configure-function.bicep":

configureFunctionBicep.png

To see all azd-related changes, refer to this commit.

 

Step 4: Make your repo Codespaces friendly (optional)

  1. Add an "Open in GitHub Codespaces" button to the README of the project. This step is a nice-to-have.
  2. To set up the development environment, create a  new ".devcontainer" directory and include a devcontainer.json and a Dockerfile file. You can use the .devcontainer directory from  the ToDo C# Azure SQL + SWA + Functions azd template as a reference since it has a similar technical stack.

To see the change made for Step 4, refer to this commit.

 

Conclusion

In summary, to convert a template with existing Bicep files, make sure you have the following in place:

  • An "azure.yaml" file.
  • A "main.parameters.json" file.
  • Add appropriate tagging of the resource group and resources with azd-env-name.
  • For the host of any application(s), include the corresponding azd-service-name tag as defined in "azure.yaml".

If you have azd-ified any templates and wish to share with the community, add the topic azd-templates to your GitHub repository and onboarding to Awesome azd, the community-contributed template gallery, by submitting an issue here.

 

We highly value your feedback and welcome any thoughts or suggestions you may have. Please feel free to share your input with us by leaving a comment or submitting an issue to the Azure Developer CLI Repo. Your feedback is crucial in helping us continuously improve and provide the best possible experience for our community. Thank you for your support!

Co-Authors
Version history
Last update:
‎Mar 22 2023 09:18 AM
Updated by: