ARM 템플릿의 람다 함수
이 문서에서는 ARM 템플릿에서 사용할 람다 함수에 대해 설명합니다. 람다 함수는 기본적으로 인수로 전달될 수 있는 코드 블록입니다. 여러 매개 변수를 사용할 수 있지만 한 줄의 코드로 제한됩니다. Bicep에서 람다 식은 다음과 같은 형식을 갖습니다.
lambda(<lambda variable>, [<lambda variable>, ...], <expression>)
제한 사항
ARM 템플릿 람다 함수에는 다음과 같은 제한 사항이 있습니다.
- 람다 함수는
filter()
,map()
,reduce()
,sort()
및toObject()
함수에서 함수 인수로만 직접 지정할 수 있습니다. - 리소스 또는 모듈 배열 액세스 내에서 람다 변수(람다 함수에 사용되는 임시 변수)를 사용하는 것은 현재 지원되지 않습니다.
listKeys
함수 내에서 람다 변수를 사용하는 것은 현재 지원되지 않습니다.- reference 함수 내에서 람다 변수를 사용하는 것은 현재 지원되지 않습니다.
필터링
filter(inputArray, lambda function)
사용자 지정 filtering 함수를 사용하여 배열을 필터링합니다.
Bicep에서 filter 함수를 사용합니다.
매개 변수
매개 변수 | 필수 | Type | 설명 |
---|---|---|---|
inputArray | 예 | 배열 | 필터링할 배열입니다. |
람다 함수 | 예 | 식 | 각 입력 배열 요소에 적용된 람다 함수입니다. false이면 항목이 출력 배열에서 필터링됩니다. |
반환 값
배열입니다.
예제
다음 예제에서는 filter
함수를 사용하는 방법을 보여 줍니다.
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"variables": {
"dogs": [
{
"name": "Evie",
"age": 5,
"interests": [
"Ball",
"Frisbee"
]
},
{
"name": "Casper",
"age": 3,
"interests": [
"Other dogs"
]
},
{
"name": "Indy",
"age": 2,
"interests": [
"Butter"
]
},
{
"name": "Kira",
"age": 8,
"interests": [
"Rubs"
]
}
]
},
"resources": [],
"outputs": {
"oldDogs": {
"type": "array",
"value": "[filter(variables('dogs'), lambda('dog', greaterOrEquals(lambdaVariables('dog').age, 5)))]"
}
}
}
앞의 예제의 출력은 5살 이상인 강아지를 보여 줍니다.
이름 | 타입 | 값 |
---|---|---|
oldDogs | 배열 | [{"name":"Evie","age":5,"interests":["Ball","Frisbee"]},{"name":"Kira","age":8,"interests":["Rubs"]}] |
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"variables": {
"copy": [
{
"name": "itemForLoop",
"count": "[length(range(0, 10))]",
"input": "[range(0, 10)[copyIndex('itemForLoop')]]"
}
]
},
"resources": [],
"outputs": {
"filteredLoop": {
"type": "array",
"value": "[filter(variables('itemForLoop'), lambda('i', greater(lambdaVariables('i'), 5)))]"
},
"isEven": {
"type": "array",
"value": "[filter(range(0, 10), lambda('i', equals(0, mod(lambdaVariables('i'), 2))))]"
}
}
}
위 예제의 출력은 다음과 같습니다.
이름 | 타입 | 값 |
---|---|---|
filteredLoop | 배열 | [6, 7, 8, 9] |
isEven | 배열 | [0, 2, 4, 6, 8] |
filterdLoop는 배열에서 5보다 큰 숫자를 표시하고, isEven은 배열의 짝수 숫자를 표시합니다.
map
map(inputArray, lambda function)
배열의 각 요소에 사용자 지정 매핑 함수를 적용합니다.
Bicep에서 map 함수를 사용합니다.
매개 변수
매개 변수 | 필수 | Type | 설명 |
---|---|---|---|
inputArray | 예 | 배열 | 매핑할 배열입니다. |
람다 함수 | 예 | 식 | 출력 배열을 생성하기 위해 각 입력 배열 요소에 적용된 람다 함수입니다. |
반환 값
배열입니다.
예시
다음 예제에서는 map
함수를 사용하는 방법을 보여 줍니다.
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"variables": {
"dogs": [
{
"name": "Evie",
"age": 5,
"interests": [
"Ball",
"Frisbee"
]
},
{
"name": "Casper",
"age": 3,
"interests": [
"Other dogs"
]
},
{
"name": "Indy",
"age": 2,
"interests": [
"Butter"
]
},
{
"name": "Kira",
"age": 8,
"interests": [
"Rubs"
]
}
]
},
"resources": [],
"outputs": {
"dogNames": {
"type": "array",
"value": "[map(variables('dogs'), lambda('dog', lambdaVariables('dog').name))]"
},
"sayHi": {
"type": "array",
"value": "[map(variables('dogs'), lambda('dog', format('Hello {0}!', lambdaVariables('dog').name)))]"
},
"mapObject": {
"type": "array",
"value": "[map(range(0, length(variables('dogs'))), lambda('i', createObject('i', lambdaVariables('i'), 'dog', variables('dogs')[lambdaVariables('i')].name, 'greeting', format('Ahoy, {0}!', variables('dogs')[lambdaVariables('i')].name))))]"
}
}
}
위 예제의 출력은 다음과 같습니다.
이름 | 타입 | 값 |
---|---|---|
dogNames | 배열 | ["Evie","Casper","Indy","Kira"] |
sayHi | 배열 | ["Hello Evie!","Hello Casper!","Hello Indy!","Hello Kira!"] |
mapObject | 배열 | [{"i":0,"dog":"Evie","greeting":"Ahoy, Evie!"},{"i":1,"dog":"Casper","greeting":"Ahoy, Casper!"},{"i":2,"dog":"Indy","greeting":"Ahoy, Indy!"},{"i":3,"dog":"Kira","greeting":"Ahoy, Kira!"}] |
dogNames는 개체 배열의 강아지 이름을 표시합니다. sayHi는 "Hello"와 각 강아지의 이름을 연결합니다. mapObject는 개체의 다른 배열을 만듭니다.
reduce
reduce(inputArray, initialValue, lambda function)
사용자 지정 reduce 함수를 사용하여 배열을 줄입니다.
Bicep에서 reduce 함수를 사용합니다.
매개 변수
매개 변수 | 필수 | Type | 설명 |
---|---|---|---|
inputArray | 예 | 배열 | 줄일 배열입니다. |
initialValue | 예 | any | 초기 값입니다. |
람다 함수 | 예 | 식 | 현재 값과 다음 값을 집계하는 데 사용되는 람다 함수입니다. |
반환 값
임의 값
예시
다음 예제에서는 reduce
함수를 사용하는 방법을 보여 줍니다.
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"variables": {
"dogs": [
{
"name": "Evie",
"age": 5,
"interests": [
"Ball",
"Frisbee"
]
},
{
"name": "Casper",
"age": 3,
"interests": [
"Other dogs"
]
},
{
"name": "Indy",
"age": 2,
"interests": [
"Butter"
]
},
{
"name": "Kira",
"age": 8,
"interests": [
"Rubs"
]
}
],
"ages": "[map(variables('dogs'), lambda('dog', lambdaVariables('dog').age))]"
},
"resources": [],
"outputs": {
"totalAge": {
"type": "int",
"value": "[reduce(variables('ages'), 0, lambda('cur', 'next', add(lambdaVariables('cur'), lambdaVariables('next'))))]"
},
"totalAgeAdd1": {
"type": "int",
"value": "[reduce(variables('ages'), 1, lambda('cur', 'next', add(lambdaVariables('cur'), lambdaVariables('next'))))]"
}
}
}
위 예제의 출력은 다음과 같습니다.
이름 | 타입 | 값 |
---|---|---|
totalAge | int | 18 |
totalAgeAdd1 | int | 19 |
totalAge는 강아지 나이를 합산합니다. totalAgeAdd1의 초기 값은 1이며 모든 강아지 나이에 초기 값에 추가합니다.
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [],
"outputs": {
"reduceObjectUnion": {
"type": "object",
"value": "[reduce(createArray(createObject('foo', 123), createObject('bar', 456), createObject('baz', 789)), createObject(), lambda('cur', 'next', union(lambdaVariables('cur'), lambdaVariables('next'))))]"
}
}
}
위 예제의 출력은 다음과 같습니다.
이름 | 타입 | 값 |
---|---|---|
reduceObjectUnion | 개체 | {"foo":123,"bar":456,"baz":789} |
union 함수는 매개 변수의 모든 요소가 포함된 단일 개체를 반환합니다. 이 함수 호출은 개체의 키-값 쌍을 새 개체로 통합합니다.
sort
sort(inputArray, lambda function)
사용자 지정 sort 함수를 사용하여 배열을 정렬합니다.
Bicep에서 sort 함수를 사용합니다.
매개 변수
매개 변수 | 필수 | Type | 설명 |
---|---|---|---|
inputArray | 예 | 배열 | 정렬할 배열입니다. |
람다 함수 | 예 | 식 | 순서 지정을 위해 두 배열 요소를 비교하는 데 사용되는 람다 함수입니다. true이면 두 번째 요소가 출력 배열에서 첫 번째 요소 다음에 옵니다. |
반환 값
배열입니다.
예시
다음 예제에서는 sort
함수를 사용하는 방법을 보여 줍니다.
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"variables": {
"dogs": [
{
"name": "Evie",
"age": 5,
"interests": [
"Ball",
"Frisbee"
]
},
{
"name": "Casper",
"age": 3,
"interests": [
"Other dogs"
]
},
{
"name": "Indy",
"age": 2,
"interests": [
"Butter"
]
},
{
"name": "Kira",
"age": 8,
"interests": [
"Rubs"
]
}
]
},
"resources": [],
"outputs": {
"dogsByAge": {
"type": "array",
"value": "[sort(variables('dogs'), lambda('a', 'b', less(lambdaVariables('a').age, lambdaVariables('b').age)))]"
}
}
}
이전 예제의 출력은 강아지 개체를 나이가 가장 적은 것부터 많은 것까지 정렬합니다.
이름 | 타입 | 값 |
---|---|---|
dogsByAge | 배열 | [{"name":"Indy","age":2,"interests":["Butter"]},{"name":"Casper","age":3,"interests":["Other dogs"]},{"name":"Evie","age":5,"interests":["Ball","Frisbee"]},{"name":"Kira","age":8,"interests":["Rubs"]}] |
toObject
toObject(inputArray, lambda function, [lambda function])
사용자 지정 키 함수 및 선택적 사용자 지정 값 함수를 사용하여 배열을 개체로 변환합니다. 개체를 배열로 변환하는 방법에 대한 항목을 참조하세요.
Bicep에서 toObject 함수를 사용합니다.
매개 변수
매개 변수 | 필수 | Type | 설명 |
---|---|---|---|
inputArray | 예 | 배열 | 개체를 만드는 데 사용되는 배열입니다. |
람다 함수 | 예 | 식 | 키 조건자를 제공하는 데 사용되는 람다 함수입니다. |
람다 함수 | 아니요 | 식 | 값 조건자를 제공하는 데 사용되는 람다 함수입니다. |
반환 값
개체입니다.
예시
다음 예제에서는 두 개의 필수 매개 변수와 함께 toObject
함수를 사용하는 방법을 보여줍니다.
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"variables": {
"dogs": [
{
"name": "Evie",
"age": 5,
"interests": [
"Ball",
"Frisbee"
]
},
{
"name": "Casper",
"age": 3,
"interests": [
"Other dogs"
]
},
{
"name": "Indy",
"age": 2,
"interests": [
"Butter"
]
},
{
"name": "Kira",
"age": 8,
"interests": [
"Rubs"
]
}
]
},
"resources": [],
"outputs": {
"dogsObject": {
"type": "object",
"value": "[toObject(variables('dogs'), lambda('entry', lambdaVariables('entry').name))]"
}
}
}
앞의 예제에서는 배열을 기반으로 개체를 생성합니다.
이름 | 타입 | 값 |
---|---|---|
dogsObject | Object | {"Evie":{"name":"Evie","age":5,"interests":["Ball","Frisbee"]},"Casper":{"name":"Casper","age":3,"interests":["Other dogs"]},"Indy":{"name":"Indy","age":2,"interests":["Butter"]},"Kira":{"name":"Kira","age":8,"interests":["Rubs"]}} |
세 번째 매개 변수가 있는 다음 toObject
함수는 동일한 출력을 제공합니다.
"outputs": {
"dogsObject": {
"type": "object",
"value": "[toObject(variables('dogs'), lambda('entry', lambdaVariables('entry').name), lambda('entry', lambdaVariables('entry')))]"
}
}
다음 예제에서는 세 개의 매개 변수와 함께 toObject
함수를 사용하는 방법을 보여줍니다.
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"variables": {
"dogs": [
{
"name": "Evie",
"properties": {
"age": 5,
"interests": [
"Ball",
"Frisbee"
]
}
},
{
"name": "Casper",
"properties": {
"age": 3,
"interests": [
"Other dogs"
]
}
},
{
"name": "Indy",
"properties": {
"age": 2,
"interests": [
"Butter"
]
}
},
{
"name": "Kira",
"properties": {
"age": 8,
"interests": [
"Rubs"
]
}
}
]
},
"resources": [],
"outputs": {
"dogsObject": {
"type": "object",
"value": "[toObject(variables('dogs'), lambda('entry', lambdaVariables('entry').name), lambda('entry', lambdaVariables('entry').properties))]"
}
}
}
앞의 예제에서는 배열을 기반으로 개체를 생성합니다.
이름 | 타입 | 값 |
---|---|---|
dogsObject | Object | {"Evie":{"age":5,"interests":["Ball","Frisbee"]},"Casper":{"age":3,"interests":["Other dogs"]},"Indy":{"age":2,"interests":["Butter"]},"Kira":{"age":8,"interests":["Rubs"]}} |
다음 단계
- 추가 배열 관련 템플릿 함수는 템플릿 함수 - 배열을 참조하세요.