Skip to content

Commit 298b371

Browse files
committed
Java fixes
1 parent a42f5cc commit 298b371

File tree

2 files changed

+124
-64
lines changed

2 files changed

+124
-64
lines changed

articles/azure-functions/create-first-function-azd-cli.md

Lines changed: 124 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ By default, the Flex Consumption plan follows a _pay-for-what-you-use_ billing m
4545
::: zone pivot="programming-language-python"
4646
+ [Python 3.11](https://www.python.org/).
4747
::: zone-end
48-
+ A [secure HTTP test tool](functions-develop-local.md#http-test-tools) for sending HTTP GET and HTTP POST requests to your function endpoints. This article uses `curl`.
48+
+ A [secure HTTP test tool](functions-develop-local.md#http-test-tools) for sending requests with JSON payloads to your function endpoints. This article uses `curl`.
4949

5050
## Initialize the project
5151

@@ -55,15 +55,18 @@ You can use the `azd init` command to create a local Azure Functions code projec
5555
1. In your local terminal or command prompt, run this `azd init` command in an empty folder:
5656

5757
```console
58-
azd init --template functions-quickstart-dotnet-azd
59-
cd http
58+
azd init --template functions-quickstart-dotnet-azd -e flexquickstart-dotnet
6059
```
61-
62-
This pulls the project files from the [template repository](https://github.com/Azure-Samples/functions-quickstart-dotnet-azd) and initializes the project in the current folder.
6360

64-
[!INCLUDE [functions-quickstart-azd-env](../../includes/functions-quickstart-azd-env.md)]
61+
This command pulls the project files from the [template repository](https://github.com/Azure-Samples/functions-quickstart-dotnet-azd) and initializes the project in the current folder. The `-e` flag sets a name for the current environment. In `azd`, the environment is used to maintain a unique deployment context for your app, and you can define more than one. It's also used in the name of the resource group you create in Azure.
62+
63+
1. Run this command to navigate to the `http` app folder:
64+
65+
```console
66+
cd http
67+
```
6568

66-
3. Create a file named _local.settings.json_ in the app folder (`http`) that contains this JSON data:
69+
1. Create a file named _local.settings.json_ in the `http` folder that contains this JSON data:
6770

6871
```json
6972
{
@@ -81,15 +84,18 @@ You can use the `azd init` command to create a local Azure Functions code projec
8184
1. In your local terminal or command prompt, run this `azd init` command in an empty folder:
8285
8386
```console
84-
azd init --template azure-functions-java-flex-consumption-azd
85-
cd http
87+
azd init --template azure-functions-java-flex-consumption-azd -e flexquickstart-java
8688
```
8789

88-
This pulls the project files from the [template repository](https://github.com/Azure-Samples/azure-functions-java-flex-consumption-azd) and initializes the project in the current folder.
90+
This command pulls the project files from the [template repository](https://github.com/Azure-Samples/azure-functions-java-flex-consumption-azd) and initializes the project in the current folder. The `-e` flag sets a name for the current environment. In `azd`, the environment is used to maintain a unique deployment context for your app, and you can define more than one. It's also used in the name of the resource group you create in Azure.
8991

90-
[!INCLUDE [functions-quickstart-azd-env](../../includes/functions-quickstart-azd-env.md)]
92+
1. Run this command to navigate to the `http` app folder:
9193

92-
3. Create a file named _local.settings.json_ in the app folder (`http`) that contains this JSON data:
94+
```console
95+
cd http
96+
```
97+
98+
1. Create a file named _local.settings.json_ in the `http` folder that contains this JSON data:
9399

94100
```json
95101
{
@@ -107,13 +113,12 @@ You can use the `azd init` command to create a local Azure Functions code projec
107113
1. In your local terminal or command prompt, run this `azd init` command in an empty folder:
108114
109115
```console
110-
azd init --template functions-quickstart-javascript-azd
116+
azd init --template functions-quickstart-javascript-azd -e flexquickstart-js
111117
```
112-
This pulls the project files from the [template repository](https://github.com/Azure-Samples/functions-quickstart-javascript-azd) and initializes the project in the root folder.
113118

114-
[!INCLUDE [functions-quickstart-azd-env](../../includes/functions-quickstart-azd-env.md)]
119+
This command pulls the project files from the [template repository](https://github.com/Azure-Samples/functions-quickstart-javascript-azd) and initializes the project in the root folder. The `-e` flag sets a name for the current environment. In `azd`, the environment is used to maintain a unique deployment context for your app, and you can define more than one. It's also used in the name of the resource group you create in Azure.
115120

116-
3. Create a file named _local.settings.json_ in the root folder that contains this JSON data:
121+
1. Create a file named _local.settings.json_ in the root folder that contains this JSON data:
117122

118123
```json
119124
{
@@ -131,14 +136,18 @@ You can use the `azd init` command to create a local Azure Functions code projec
131136
1. In your local terminal or command prompt, run this `azd init` command in an empty folder:
132137
133138
```console
134-
azd init --template functions-quickstart-powershell-azd
135-
cd src
139+
azd init --template functions-quickstart-powershell-azd -e flexquickstart-ps
136140
```
137-
This pulls the project files from the [template repository](https://github.com/Azure-Samples/functions-quickstart-powershell-azd) and initializes the project in the root folder.
138141

139-
[!INCLUDE [functions-quickstart-azd-env](../../includes/functions-quickstart-azd-env.md)]
142+
This command pulls the project files from the [template repository](https://github.com/Azure-Samples/functions-quickstart-powershell-azd) and initializes the project in the root folder. The `-e` flag sets a name for the current environment. In `azd`, the environment is used to maintain a unique deployment context for your app, and you can define more than one. It's also used in the name of the resource group you create in Azure.
140143

141-
3. Create a file named _local.settings.json_ in the app folder (`http`) that contains this JSON data:
144+
1. Run this command to navigate to the `src` app folder:
145+
146+
```console
147+
cd src
148+
```
149+
150+
1. Create a file named _local.settings.json_ in the `src` folder that contains this JSON data:
142151

143152
```json
144153
{
@@ -157,13 +166,12 @@ You can use the `azd init` command to create a local Azure Functions code projec
157166
1. In your local terminal or command prompt, run this `azd init` command in an empty folder:
158167
159168
```console
160-
azd init --template functions-quickstart-typescript-azd
169+
azd init --template functions-quickstart-typescript-azd -e flexquickstart-ts
161170
```
162-
This pulls the project files from the [template repository](https://github.com/Azure-Samples/functions-quickstart-typescript-azd) and initializes the project in the root folder.
163171

164-
[!INCLUDE [functions-quickstart-azd-env](../../includes/functions-quickstart-azd-env.md)]
172+
This command pulls the project files from the [template repository](https://github.com/Azure-Samples/functions-quickstart-typescript-azd) and initializes the project in the root folder. The `-e` flag sets a name for the current environment. In `azd`, the environment is used to maintain a unique deployment context for your app, and you can define more than one. It's also used in the name of the resource group you create in Azure.
165173

166-
3. Create a file named _local.settings.json_ in the root folder that contains this JSON data:
174+
1. Create a file named _local.settings.json_ in the root folder that contains this JSON data:
167175

168176
```json
169177
{
@@ -181,14 +189,12 @@ You can use the `azd init` command to create a local Azure Functions code projec
181189
1. In your local terminal or command prompt, run this `azd init` command in an empty folder:
182190
183191
```console
184-
azd init --template functions-quickstart-python-http-azd
192+
azd init --template functions-quickstart-python-http-azd -e flexquickstart-py
185193
```
186194
187-
This pulls the project files from the [template repository](https://github.com/Azure-Samples/functions-quickstart-python-http-azd) and initializes the project in the root folder.
188-
189-
[!INCLUDE [functions-quickstart-azd-env](../../includes/functions-quickstart-azd-env.md)]
195+
This command pulls the project files from the [template repository](https://github.com/Azure-Samples/functions-quickstart-python-http-azd) and initializes the project in the root folder. The `-e` flag sets a name for the current environment. In `azd`, the environment is used to maintain a unique deployment context for your app, and you can define more than one. It's also used in the name of the resource group you create in Azure.
190196

191-
3. Create a file named _local.settings.json_ in the root folder that contains this JSON data:
197+
1. Create a file named _local.settings.json_ in the root folder that contains this JSON data:
192198

193199
```json
194200
{
@@ -201,15 +207,6 @@ You can use the `azd init` command to create a local Azure Functions code projec
201207
```
202208

203209
This file is required when running locally.
204-
[!INCLUDE [functions-quickstart-azd-env](../../includes/functions-quickstart-azd-env.md)]
205-
206-
3. Run this command in the root folder:
207-
208-
```console
209-
func init --worker-runtime python
210-
```
211-
212-
This command restores the _local.settings.json_ file in the root folder, which is required when running locally.
213210

214211
## Create and activate a virtual environment
215212

@@ -267,13 +264,13 @@ py -m venv .venv
267264
```
268265
::: zone-end
269266

270-
When the Azure Functions host starts in your local project folder, it displays the URL endpoints of the HTTP triggered functions in your project.
267+
When the Functions host starts in your local project folder, it writes the URL endpoints of your HTTP triggered functions to the terminal output.
271268

272269
1. In your browser, navigate to the `httpget` endpoint, which should look like this URL:
273270

274271
<http://localhost:7071/api/httpget>
275272

276-
1. From a new terminal or command prompt window, run this `curl` to send an POST request with a JSON payload to the `httppost` endpoint:
273+
1. From a new terminal or command prompt window, run this `curl` command to send a POST request with a JSON payload to the `httppost` endpoint:
277274

278275
```console
279276
curl -i http://localhost:7071/api/httppost -H "Content-Type: text/json" -d @testdata.json
@@ -292,7 +289,7 @@ You can review the code that defines the two HTTP trigger function endpoints:
292289
293290
### [`httpget`](#tab/get)
294291
::: zone pivot="programming-language-csharp"
295-
:::code language="csharp" source="~/functions-quickstart-dotnet-azd/http/httpGetFunction.cs" range="17-28" :::
292+
:::code language="csharp" source="~/functions-quickstart-dotnet-azd/http/httpGetFunction.cs" range="17-29" :::
296293
::: zone-end
297294
::: zone pivot="programming-language-java"
298295
:::code language="java" source="~/functions-quickstart-java-azd/http/src/main/java/com/contoso/Function.java" range="24-38" :::
@@ -316,7 +313,7 @@ This `run.ps1` file implements the function code:
316313
### [`httppost`](#tab/post)
317314
318315
::: zone pivot="programming-language-csharp"
319-
:::code language="csharp" source="~/functions-quickstart-dotnet-azd/http/httpPostBodyFunction.cs" range="19-31":::
316+
:::code language="csharp" source="~/functions-quickstart-dotnet-azd/http/httpPostBodyFunction.cs" range="19-35":::
320317
::: zone-end
321318
::: zone pivot="programming-language-java"
322319
:::code language="java" source="~/functions-quickstart-java-azd/http/src/main/java/com/contoso/Function.java" range="44-71" :::
@@ -340,20 +337,90 @@ This `run.ps1` file implements the function code:
340337
---
341338

342339
After you verify your functions locally, it's time to publish them to Azure.
340+
::: zone pivot="programming-language-java"
341+
## Create Azure resources
342+
343+
This project is configured to use the `azd provision` command to create a function app in a Flex Consumption plan, along with other required Azure resources.
344+
345+
>[!NOTE]
346+
>This project includes a set of Bicep files that `azd` uses to create a secure deployment to a Flex consumption plan that follows best practices.
347+
>
348+
>The `azd up` and `azd deploy` commands aren't currently supported for Java apps.
349+
350+
1. In the root folder of the project, run this command to create the required Azure resources:
351+
352+
```console
353+
azd provision
354+
```
355+
356+
The root folder contains the `azure.yaml` definition file required by `azd`.
357+
358+
If you aren't already signed-in, you're asked to authenticate with your Azure account.
359+
360+
1. When prompted, provide these required deployment parameters:
361+
362+
| Parameter | Description |
363+
| ---- | ---- |
364+
| _Azure subscription_ | Subscription in which your resources are created.|
365+
| _Azure location_ | Azure region in which to create the resource group that contains the new Azure resources. Only regions that currently support the Flex Consumption plan are shown.|
366+
367+
The `azd provision` command uses your response to these prompts with the Bicep configuration files to create and configure these required Azure resources:
368+
369+
+ Flex Consumption plan and function app
370+
+ Azure Storage (required) and Application Insights (recommended)
371+
+ Access policies and roles for your account
372+
+ Service-to-service connections using managed identities (instead of stored connection strings)
373+
+ Virtual network to securely run both the function app and the other Azure resources
374+
375+
After the command completes successfully, you can deploy your project code to this new function app in Azure.
376+
377+
## Deploy to Azure
378+
379+
You can use Core Tools to package your code and deploy it to Azure from the `target` output folder.
380+
381+
1. Navigate to the app folder equivalent in the `target` output folder:
382+
383+
```console
384+
cd http/target/azure-functions/contoso-functions
385+
```
386+
387+
This folder should have a host.json file, which indicates that it's the root of your compiled Java function app.
343388
389+
1. Run these commands to deploy your compiled Java code project to the new function app resource in Azure using Core Tools:
390+
391+
### [bash](#tab/bash)
392+
393+
```bash
394+
APP_NAME=$(azd env get-value AZURE_FUNCTION_NAME)
395+
func azure functionapp publish $APP_NAME
396+
```
397+
398+
### [Cmd](#tab/cmd)
399+
```cmd
400+
for /f "tokens=*" %i in ('azd env get-value AZURE_FUNCTION_NAME') do set APP_NAME=%i
401+
func azure functionapp publish %APP_NAME%
402+
```
403+
404+
---
405+
406+
The `azd env get-value` command gets your function app name from the local environment, which is required for deployment using `func azure functionapp publish`. After publishing completes successfully, you see links to the HTTP trigger endpoints in Azure.
407+
::: zone-end
408+
::: zone pivot="programming-language-csharp,programming-language-javascript,programming-language-typescript,programming-language-powershell,programming-language-python"
344409
## Deploy to Azure
345410

346411
This project is configured to use the `azd up` command to deploy this project to a new function app in a Flex Consumption plan in Azure.
347412

348413
>[!TIP]
349414
>This project includes a set of Bicep files that `azd` uses to create a secure deployment to a Flex consumption plan that follows best practices.
350415

351-
1. Run this command to create the Azure resources and deploy your app to Azure.
416+
1. Run this command to have `azd` create the required Azure resources in Azure and deploy your code project to the new function app:
352417

353418
```console
354419
azd up
355420
```
356421

422+
The root folder contains the `azure.yaml` definition file required by `azd`.
423+
357424
If you aren't already signed-in, you're asked to authenticate with your Azure account.
358425

359426
1. When prompted, provide these required deployment parameters:
@@ -365,25 +432,25 @@ This project is configured to use the `azd up` command to deploy this project to
365432
366433
The `azd up` command uses your response to these prompts with the Bicep configuration files to complete these deployment tasks:
367434

368-
+ Create and configure these required Azure resources (equivalent to [`azd provision`](/azure/developer/azure-developer-cli/reference#azd-provision)):
435+
+ Create and configure these required Azure resources (equivalent to `azd provision`):
369436

370437
+ Flex Consumption plan and function app
371438
+ Azure Storage (required) and Application Insights (recommended)
372439
+ Access policies and roles for your account
373440
+ Service-to-service connections using managed identities (instead of stored connection strings)
374441
+ Virtual network to securely run both the function app and the other Azure resources
375442

376-
+ Package and deploy your code to the deployment container (equivalent to [`azd deploy`](/azure/developer/azure-developer-cli/reference#azd-deploy)). The app is then started and runs in the deployed package.
377-
378-
1. After the command completes successfully, you see links to the resources created.
443+
+ Package and deploy your code to the deployment container (equivalent to `azd deploy`). The app is then started and runs in the deployed package.
379444

445+
After the command completes successfully, you see links to the resources you created.
446+
::: zone-end
380447
## Invoke the function on Azure
381448

382449
You can now invoke your function endpoints in Azure by making HTTP requests to their URLs using your HTTP test tool or from the browser (for GET requests). When your functions run in Azure, access key authorization is enforced, and you must provide a function access key with your request.
383450

384451
You can use the Core Tools to obtain the URL endpoints of your functions running in Azure.
385452

386-
1. In your local terminal or command prompt, run this command to get the URL endpoint values, including access keys:
453+
1. In your local terminal or command prompt, run this command to get the URL endpoint values:
387454
388455
### [bash](#tab/bash)
389456

@@ -403,7 +470,7 @@ You can use the Core Tools to obtain the URL endpoints of your functions running
403470
The `azd env get-value` command gets your function app name from the local environment. Using the `--show-keys` option means that the returned **Invoke URL:** value for each endpoint includes a function-level access key.
404471

405472
1. As before, use your HTTP test tool to validate these URLs in your function app running in Azure.
406-
473+
::: zone pivot="programming-language-csharp,programming-language-javascript,programming-language-typescript,programming-language-powershell,programming-language-python"
407474
## Redeploy your code
408475

409476
You can run the `azd up` command as many times as you need to both provision your Azure resources and deploy code updates to your function app.
@@ -412,20 +479,24 @@ You can run the `azd up` command as many times as you need to both provision you
412479
>Deployed code files are always overwritten by the latest deployment package.
413480

414481
Your initial responses to `azd` prompts and any environment variables generated by `azd` are stored locally in your named environment. Use the `azd env get-values` command to review all of the variables in your environment that were used when creating Azure resources.
415-
482+
::: zone-end
416483
## Clean up resources
417484

418485
When you're done working with your function app and related resources, you can use this command to delete the function app and its related resources from Azure and avoid incurring any further costs:
419486

420487
```console
421-
azd down
488+
azd down --no-prompt
422489
```
423490

424-
This command doesn't affect your local code project.
491+
>[!NOTE]
492+
>The `--no-prompt` option has `azd` delete your resource group without a confirmation.
493+
>
494+
>This command doesn't affect your local code project.
425495
426496
## Related content
427497

428498
+ [Flex Consumption plan](flex-consumption-plan.md)
429499
+ [Azure Developer CLI (azd)](/azure/developer/azure-developer-cli/)
500+
+ [azd reference](/azure/developer/azure-developer-cli/reference)
430501
+ [Azure Functions Core Tools reference](functions-core-tools-reference.md)
431502
+ [Code and test Azure Functions locally](functions-develop-local.md)

includes/functions-quickstart-azd-env.md

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)