Skip to content

adding deployment steps to .NET Aspire + Redis to Azure #883

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 156 additions & 0 deletions docs/caching/caching-components-deployment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
---
title: Deploy a .NET Aspire app that connects to Redis Cache to Azure
description: Learn how to deploy a .NET Aspire app that connects to Redis Cache to Azure
ms.date: 05/12/2024
ms.topic: how-to
---

# Tutorial: Deploy a .NET Aspire app with a Redis Cache to Azure

In this tutorial, you learn to configure a .NET Aspire app with a Redis Cache for deployment to Azure. .NET Aspire provides multiple caching component configurations that provision different Redis services in Azure. You'll learn how to:

> [!div class="checklist"]
>
> - Configure the app to provision an Azure Cache for Redis
> - Configure the app to provision a containerized Redis Cache

> [!NOTE]
> This document focuses specifically on .NET Aspire configurations to provision and deploy Redis Cache resources in Azure. Visit the [Azure Container Apps deployment](/dotnet/aspire/deployment/azure/aca-deployment?branch=pr-en-us-532&tabs=visual-studio%2Clinux%2Cpowershell&pivots=azure-azd) tutorial to learn more about the full .NET Aspire deployment process.

[!INCLUDE [aspire-prereqs](../includes/aspire-prereqs.md)]

## Create the sample solution

Follow the [Tutorial: Implement caching with .NET Aspire components](./caching-components.md) to create the sample project.

## Configure the app for Redis cache deployment

.NET Aspire provides two built-in configuration options to streamline Redis Cache deployment on Azure:

- Provision a containerized Redis Cache using Azure Container Apps
- Provision an Azure Cache for Redis instance

### Add the .NET Aspire component to the app

Add the appropriate .NET Aspire component to the _AspireRedis.AppHost_ project for your desired hosting service.

# [Azure Cache for Redis](#tab/azure-redis)

Add the [Aspire.Hosting.Azure.Redis](https://www.nuget.org/packages/Aspire.Hosting.Azure.Redis) package to the _AspireRedis.AppHost_ project:

```dotnetcli
dotnet add package Aspire.Hosting.Azure.Redis --prerelease
```

## [Redis Container](#tab/redis-container)

Add the [Aspire.Hosting.Redis](https://www.nuget.org/packages/Aspire.Hosting.Redis) package to the _AspireRedis.AppHost_ project:

```dotnetcli
dotnet add package Aspire.Hosting.Redis --prerelease
```

---

### Configure the AppHost project

Configure the _AspireRedis.AppHost_ project for your desired Redis service.

# [Azure Cache for Redis](#tab/azure-redis)

Replace the contents of the _Program.cs_ file in the _AspireRedis.AppHost_ project with the following code:

```csharp
var builder = DistributedApplication.CreateBuilder(args);

var cache = builder.AddRedis("cache").PublishAsAzureRedis();

var apiService = builder.AddProject<Projects.AspireRedis_ApiService>("apiservice")
.WithReference(cache);

builder.AddProject<Projects.AspireRedis_Web>("webfrontend")
.WithExternalHttpEndpoints()
.WithReference(cache)
.WithReference(apiService);

builder.Build().Run();
```

The preceding code adds an Azure Cache for Redis resource to your app and configures a connection called `cache`. The `PublishAsAzureRedis` method ensures that tools such as the Azure Developer CLI or Visual Studio create an Azure Cache for Redis resource during the deployment process.

## [Redis Container](#tab/redis-container)

Replace the contents of the _Program.cs_ file in the _AspireRedis.AppHost_ project with the following code:

```csharp
var builder = DistributedApplication.CreateBuilder(args);

var cache = builder.AddRedis("cache");

var apiService = builder.AddProject<Projects.RedisSample_ApiService>("apiservice")
.WithReference(cache);

builder.AddProject<Projects.RedisSample_Web>("webfrontend")
.WithExternalHttpEndpoints()
.WithReference(cache)
.WithReference(apiService);

builder.Build().Run();
```

The preceding code adds a Redis Container resource to your app and configures a connection called `cache`. This configuration also ensures that tools such as the Azure Developer CLI or Visual Studio create a containerized Redis instance during the deployment process.

---

## Deploy the app

Tools such as the [Azure Developer CLI](/azure/developer/azure-developer-cli/overview) (`azd`) support .NET Aspire Redis component configurations to streamline deployments. `azd` consumes these settings and provisions properly configured resources for you.

> [!NOTE]
> You can also use the [Azure CLI](/dotnet/aspire/deployment/azure/aca-deployment?branch=pr-en-us-532&tabs=visual-studio%2Clinux%2Cpowershell&pivots=azure-cli) or [Bicep](/dotnet/aspire/deployment/azure/aca-deployment?branch=pr-en-us-532&tabs=visual-studio%2Clinux%2Cpowershell&pivots=azure-bicep) to provision and deploy .NET Aspire app resources. These options require more manual steps, but provide more granular control over your deployments. .NET Aspire apps can also connect to an existing Redis instance through manual configurations.

1. Open a terminal window in the root of your .NET Aspire project.

1. Run the `azd init` command to initialize the project with `azd`.

```azdeveloper
azd init
```

1. When prompted for an environment name, enter *docs-aspireredis*.

1. Run the `azd up` command to begin the deployment process:

```azdeveloper
azd up
```

1. Select the Azure subscription that should host your app resources.

1. Select the Azure location to use.

The Azure Developer CLI provisions and deploys your app resources. The process may take a few minutes to complete.

1. When the deployment finishes, click the resource group link in the output to view the created resources in the Azure portal.

## [Azure Cache for Redis](#tab/azure-redis)

The deployment process provisioned an Azure Cache for Redis resource due to the **.AppHost** configuration you provided.

:::image type="content" source="media/resources-azure-redis.png" alt-text="A screenshot showing the deployed Azure Cache for Redis.":::

## [Redis Container](#tab/redis-container)

The deployment process created a Redis app container due to the **.AppHost** configuration you provided.

:::image type="content" source="media/resources-azure-redis-container.png" alt-text="A screenshot showing the containerized Redis.":::

---

[!INCLUDE [clean-up-resources](../includes/clean-up-resources.md)]

## See also

- [.NET Aspire deployment via Azure Container Apps](../deployment/azure/aca-deployment.md)
- [.NET Aspire Azure Container Apps deployment deep dive](../deployment/azure/aca-deployment-azd-in-depth.md)
- [Deploy a .NET Aspire app using GitHub Actions](../deployment/azure/aca-deployment-github-actions.md)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/caching/media/resources-azure-redis.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions docs/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ items:
displayName: app insights
- name: Deploy .NET Aspire + SQL Database
href: database/sql-server-component-deployment.md
- name: Deploy .NET Aspire + Redis
hred: caching/caching-components-deployment.md
- name: Reference - Tool-builder manifest schemas
href: deployment/manifest-format.md

Expand Down
Loading