Skip to content

Commit 45e5804

Browse files
authored
Merge pull request #3 from seesharprun/implement-azd
AZD implementation
2 parents c812e0b + 1665138 commit 45e5804

File tree

7 files changed

+237
-11
lines changed

7 files changed

+237
-11
lines changed

.github/workflows/azure-dev.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Deploy to Azure
2+
on:
3+
workflow_dispatch:
4+
permissions:
5+
id-token: write
6+
contents: read
7+
jobs:
8+
build:
9+
runs-on: ubuntu-latest
10+
env:
11+
AZURE_CLIENT_ID: ${{ vars.AZURE_CLIENT_ID }}
12+
AZURE_TENANT_ID: ${{ vars.AZURE_TENANT_ID }}
13+
AZURE_SUBSCRIPTION_ID: ${{ vars.AZURE_SUBSCRIPTION_ID }}
14+
AZURE_ENV_NAME: ${{ vars.AZURE_ENV_NAME }}
15+
AZURE_LOCATION: ${{ vars.AZURE_LOCATION }}
16+
steps:
17+
- name: Checkout
18+
uses: actions/checkout@v4
19+
- name: Install azd
20+
uses: Azure/setup-azd@v1.0.0
21+
- name: Log in with Azure (Federated Credentials)
22+
run: |
23+
azd auth login `
24+
--client-id "$Env:AZURE_CLIENT_ID" `
25+
--federated-credential-provider "github" `
26+
--tenant-id "$Env:AZURE_TENANT_ID"
27+
shell: pwsh
28+
- name: Provision Infrastructure
29+
run: azd provision --no-prompt
30+
env:
31+
AZD_INITIAL_ENVIRONMENT_CONFIG: ${{ secrets.AZD_INITIAL_ENVIRONMENT_CONFIG }}
32+
AZURE_RESOURCE_GROUP: ${{ vars.AZURE_RESOURCE_GROUP }}
33+
- name: Deploy Application
34+
run: azd deploy --no-prompt
35+
env:
36+
AZURE_RESOURCE_GROUP: ${{ vars.AZURE_RESOURCE_GROUP }}

azure.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@
33
name: cosmos-db-nosql-rust-quickstart
44
metadata:
55
template: cosmos-db-nosql-rust-quickstart
6+
services:
7+
web:
8+
project: ./src
9+
language: js
10+
host: containerapp
11+
docker:
12+
path: ./Dockerfile
13+
context: ./
14+
pipeline:
15+
provider: github
616
hooks:
717
postprovision:
818
posix:

infra/main.bicep

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ param location string
1414
@description('Id of the principal to assign database and application roles.')
1515
param deploymentUserPrincipalId string = ''
1616

17+
// serviceName is used as value for the tag (azd-service-name) azd uses to identify deployment host
18+
param serviceName string = 'web'
19+
1720
var resourceToken = toLower(uniqueString(resourceGroup().id, environmentName, location))
1821
var tags = {
1922
'azd-env-name': environmentName
@@ -87,7 +90,125 @@ module cosmosDbAccount 'br/public:avm/res/document-db/database-account:0.8.1' =
8790
}
8891
}
8992

93+
module containerRegistry 'br/public:avm/res/container-registry/registry:0.5.1' = {
94+
name: 'container-registry'
95+
params: {
96+
name: 'containerreg${resourceToken}'
97+
location: location
98+
tags: tags
99+
acrAdminUserEnabled: false
100+
anonymousPullEnabled: true
101+
publicNetworkAccess: 'Enabled'
102+
acrSku: 'Standard'
103+
}
104+
}
105+
106+
var containerRegistryRole = subscriptionResourceId(
107+
'Microsoft.Authorization/roleDefinitions',
108+
'8311e382-0749-4cb8-b61a-304f252e45ec'
109+
) // AcrPush built-in role
110+
111+
module registryUserAssignment 'br/public:avm/ptn/authorization/resource-role-assignment:0.1.1' = if (!empty(deploymentUserPrincipalId)) {
112+
name: 'container-registry-role-assignment-push-user'
113+
params: {
114+
principalId: deploymentUserPrincipalId
115+
resourceId: containerRegistry.outputs.resourceId
116+
roleDefinitionId: containerRegistryRole
117+
}
118+
}
119+
120+
module logAnalyticsWorkspace 'br/public:avm/res/operational-insights/workspace:0.7.0' = {
121+
name: 'log-analytics-workspace'
122+
params: {
123+
name: 'log-analytics-${resourceToken}'
124+
location: location
125+
tags: tags
126+
}
127+
}
128+
129+
module containerAppsEnvironment 'br/public:avm/res/app/managed-environment:0.8.0' = {
130+
name: 'container-apps-env'
131+
params: {
132+
name: 'container-env-${resourceToken}'
133+
location: location
134+
tags: tags
135+
logAnalyticsWorkspaceResourceId: logAnalyticsWorkspace.outputs.resourceId
136+
zoneRedundant: false
137+
}
138+
}
139+
140+
module containerAppsApp 'br/public:avm/res/app/container-app:0.9.0' = {
141+
name: 'container-apps-app'
142+
params: {
143+
name: 'container-app-${resourceToken}'
144+
environmentResourceId: containerAppsEnvironment.outputs.resourceId
145+
location: location
146+
tags: union(tags, { 'azd-service-name': serviceName })
147+
ingressTargetPort: 3030
148+
ingressExternal: true
149+
ingressTransport: 'auto'
150+
stickySessionsAffinity: 'sticky'
151+
scaleMaxReplicas: 1
152+
scaleMinReplicas: 1
153+
corsPolicy: {
154+
allowCredentials: true
155+
allowedOrigins: [
156+
'*'
157+
]
158+
}
159+
managedIdentities: {
160+
systemAssigned: false
161+
userAssignedResourceIds: [
162+
managedIdentity.outputs.resourceId
163+
]
164+
}
165+
secrets: {
166+
secureList: [
167+
{
168+
name: 'azure-cosmos-db-nosql-endpoint'
169+
value: cosmosDbAccount.outputs.endpoint
170+
}
171+
{
172+
name: 'user-assigned-managed-identity-client-id'
173+
value: managedIdentity.outputs.clientId
174+
}
175+
]
176+
}
177+
containers: [
178+
{
179+
image: 'mcr.microsoft.com/azuredocs/containerapps-helloworld:latest'
180+
name: 'web-front-end'
181+
resources: {
182+
cpu: '0.25'
183+
memory: '.5Gi'
184+
}
185+
env: [
186+
{
187+
name: 'CONFIGURATION__AZURECOSMOSDB__ENDPOINT'
188+
secretRef: 'azure-cosmos-db-nosql-endpoint'
189+
}
190+
{
191+
name: 'CONFIGURATION__AZURECOSMOSDB__DATABASENAME'
192+
value: databaseName
193+
}
194+
{
195+
name: 'CONFIGURATION__AZURECOSMOSDB__CONTAINERNAME'
196+
value: containerName
197+
}
198+
{
199+
name: 'AZURE_CLIENT_ID'
200+
secretRef: 'user-assigned-managed-identity-client-id'
201+
}
202+
]
203+
}
204+
]
205+
}
206+
}
207+
90208
// Azure Cosmos DB for Table outputs
91209
output CONFIGURATION__AZURECOSMOSDB__ENDPOINT string = cosmosDbAccount.outputs.endpoint
92210
output CONFIGURATION__AZURECOSMOSDB__DATABASENAME string = databaseName
93211
output CONFIGURATION__AZURECOSMOSDB__CONTAINERNAME string = containerName
212+
213+
// Azure Container Registry outputs
214+
output AZURE_CONTAINER_REGISTRY_ENDPOINT string = containerRegistry.outputs.loginServer

readme.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<!--
2+
---
3+
page_type: sample
4+
name: "Quickstart: Azure Cosmos DB for NoSQL and Azure SDK for Rust"
5+
description: This is a simple web application to illustrate common basic usage of Azure Cosmos DB for NoSQL and the Azure SDK for Rust.
6+
urlFragment: template
7+
languages:
8+
- rust
9+
- azdeveloper
10+
products:
11+
- azure-cosmos-db
12+
---
13+
-->
14+
15+
# Quickstart: Azure Cosmos DB for NoSQL client library for Go
16+
17+
This is a simple web application to illustrate common basic usage of Azure Cosmos DB for NoSQL's client library for Rust. This sample application accesses an existing account, database, and container using the [`azure_data_cosmos`](https://docs.rs/azure_data_cosmos) and [`azure_identity`](https://docs.rs/azure_identity) crates.
18+
19+
## Prerequisites
20+
21+
- [Docker](https://www.docker.com/)
22+
- [Azure Developer CLI](https://aka.ms/azd-install)
23+
- [Rust 1+](https://go.dev/dl/)
24+
25+
## Quickstart
26+
27+
1. Log in to Azure Developer CLI.
28+
29+
```bash
30+
azd auth login
31+
```
32+
33+
> [!TIP]
34+
> This is only required once per-install.
35+
36+
1. Initialize this template (`cosmos-db-nosql-rust-quickstart`) using `azd init`
37+
38+
```bash
39+
azd init --template cosmos-db-nosql-rust-quickstart
40+
```
41+
42+
1. Ensure that **Docker** is running in your environment.
43+
44+
1. Use `azd up` to provision your Azure infrastructure and deploy the web application to Azure.
45+
46+
```bash
47+
azd up
48+
```
49+
50+
1. Observed the deployed web application
51+
52+
![Screenshot of the deployed web application.](assets/web.png)

src/.dockerignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
debug/
2+
target/
3+
Cargo.lock

src/Dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM rust:1
2+
3+
COPY . ./
4+
5+
RUN cargo build --release
6+
7+
CMD ["./target/release/app-main"]

src/app/cosmos.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use futures::StreamExt;
22
use serde_json;
3-
use azure_data_cosmos::{CosmosClient, CosmosClientOptions, PartitionKey};
3+
use azure_data_cosmos::{CosmosClient, PartitionKey};
44
use azure_identity::DefaultAzureCredential;
55
use crate::item::Item;
66

@@ -17,10 +17,7 @@ where
1717

1818
let credential = DefaultAzureCredential::new().unwrap();
1919

20-
let client_options = CosmosClientOptions::default();
21-
let client_options = Some(client_options);
22-
23-
let service_client = match CosmosClient::new(&endpoint, credential, client_options) {
20+
let client = match CosmosClient::new(&endpoint, credential, None) {
2421
Ok(client) => client,
2522
Err(e) => {
2623
eprintln!("Error creating CosmosClient: {}", e);
@@ -29,10 +26,10 @@ where
2926
};
3027
callback("Client created".to_string());
3128

32-
let database_client = service_client.database_client(&database_name);
29+
let database = client.database(&database_name);
3330
callback(format!("Get database:\t {}", database_name));
3431

35-
let container_client = database_client.container_client(&container_name);
32+
let container = database.container(&container_name);
3633
callback(format!("Get container:\t {}", container_name));
3734

3835
{
@@ -47,7 +44,7 @@ where
4744

4845
let partition_key = PartitionKey::from(item.category.clone());
4946

50-
let upsert_response = container_client.upsert_item(partition_key, item, None).await;
47+
let upsert_response = container.upsert_item(partition_key, item, None).await;
5148

5249
match upsert_response {
5350
Ok(r) => {
@@ -80,7 +77,7 @@ where
8077

8178
let partition_key = PartitionKey::from(item.category.clone());
8279

83-
let upsert_response = container_client.upsert_item(partition_key, item, None).await;
80+
let upsert_response = container.upsert_item(partition_key, item, None).await;
8481

8582
match upsert_response {
8683
Ok(r) => {
@@ -105,7 +102,7 @@ where
105102
let item_id = "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb";
106103
let item_partition_key = "gear-surf-surfboards";
107104

108-
let read_response = container_client.read_item::<Item>(item_partition_key, item_id, None).await;
105+
let read_response = container.read_item::<Item>(item_partition_key, item_id, None).await;
109106

110107
match read_response {
111108
Ok(r) => {
@@ -133,7 +130,7 @@ where
133130

134131
let query = format!("SELECT * FROM c WHERE c.category = '{}'", item_partition_key);
135132

136-
let page_response = container_client.query_items::<Item>(&query, partition_key, None);
133+
let page_response = container.query_items::<Item>(&query, partition_key, None);
137134

138135
callback("Run query:".to_string());
139136
match page_response {

0 commit comments

Comments
 (0)