Skip to content

Commit 9b6e1a2

Browse files
committed
Test build 09
1 parent 67ab137 commit 9b6e1a2

File tree

2 files changed

+130
-2
lines changed

2 files changed

+130
-2
lines changed

articles/deployment-environments/how-to-configure-extensibility-model-custom-image.md

Lines changed: 130 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,14 @@ Creating a custom container image allows you to customize your deployments to fi
7373
7474
After you complete the image customization, you can build the image and push it to your container registry by using a script provided by Microsoft to automate the process.
7575
76-
[!INCLUDE [custom-image-create](includes/custom-image-create.md)]
76+
[!INCLUDE [custom-image-create-arm](includes/custom-image-create-arm.md)]
7777
7878
### Build a container image with a script
7979
8080
Rather than building your custom image and pushing it to a container registry yourself, you can use a script to build and push it to a specified container registry.
8181
8282
[!INCLUDE [custom-image-script](includes/custom-image-script.md)]
8383
84-
8584
### [Create a custom image manually](#tab/custom-manual/)
8685
### Create a custom container image manually
8786
@@ -108,6 +107,135 @@ docker build . -t {YOUR_REGISTRY}.azurecr.io/customImage:1.0.0
108107

109108
::: zone-end
110109

110+
::: zone pivot="terraform"
111+
## Use container images with ADE
112+
113+
You can take one of the following approaches to use container images with ADE:
114+
- **Create a container image leveraging a GitHub workflow:** To start with, you can use the published GitHub workflow from the Leveraging ADE's Extensibility Model With Terraform repository.
115+
- **Create a custom container image:** You can create a workflow that creates a Terraform specific image customized with all the software, settings, and configuration that you need.
116+
117+
118+
## Create a container image leveraging a GitHub workflow
119+
Creating a custom container image allows you to customize your deployments to fit your requirements. You can create custom images based on the ADE standard images.
120+
121+
After you complete the image customization, you must build the image and push it to your container registry.
122+
123+
You can build and push the image manually, or use a script provided by Microsoft to automate the process.
124+
125+
The published GitHub Action helps to build and push an image to an Azure Container Registry (ACR). You can reference a provided ACR image link within an environment definition in ADE to deploy or delete an environment with the provided image.
126+
127+
To create an image configured for ADE, follow these steps:
128+
1. Create a custom image based on a GitHub workflow.
129+
1. Install desired packages.
130+
1. Configure operation shell scripts.
131+
1. Create operation shell scripts that use the Terraform CLI.
132+
133+
**1. Create a custom image based on a GitHub workflow**
134+
135+
Use the [published repository](https://github.com/Azure/ade-extensibility-model-terraform/blob/main/README.md#azure-deployment-environments---leveraging-ades-extensibility-model-with-terraform) to take advantage of the GitHub workflow. The repository contains ADE-compatible sample image components, including a Dockerfile and shell scripts for deploying and deleting environments using Terraform IaC templates. This sample code helps you create your own container image.
136+
137+
138+
**2. Install required packages**
139+
In this step, you install any packages you require in your image, including Terraform. You can install the Terraform CLI to an executable location so that it can be used in your deployment and deletion scripts.
140+
141+
Here's an example of that process, installing version 1.7.5 of the Terraform CLI:
142+
143+
```azure cli
144+
RUN wget -O terraform.zip https://releases.hashicorp.com/terraform/1.7.5/terraform_1.7.5_linux_amd64.zip
145+
RUN unzip terraform.zip && rm terraform.zip
146+
RUN mv terraform /usr/bin/terraform
147+
```
148+
149+
> [!Tip]
150+
> You can get the download URL for your preferred version of the Terraform CLI from [Hashicorp releases](https://aka.ms/deployment-environments/terraform-cli-zip).
151+
152+
153+
**3. Configure operation shell scripts**
154+
155+
Within the standard images, operations are determined and executed based on the operation name. Currently, the two operation names supported are *deploy* and *delete*.
156+
157+
To set up your custom image to utilize this structure, specify a folder at the level of your Dockerfile named *scripts*, and specify two files, *deploy.sh*, and *delete.sh*. The deploy shell script runs when your environment is created or redeployed, and the delete shell script runs when your environment is deleted. You can see examples of shell scripts in the repository under the [Runner-Images folder for the ARM-Bicep](https://github.com/Azure/deployment-environments/tree/main/Runner-Images/ARM-Bicep) image.
158+
159+
To ensure these shell scripts are executable, add the following lines to your Dockerfile:
160+
161+
```docker
162+
COPY scripts/* /scripts/
163+
RUN find /scripts/ -type f -iname "*.sh" -exec dos2unix '{}' '+'
164+
RUN find /scripts/ -type f -iname "*.sh" -exec chmod +x {} \;
165+
```
166+
167+
**4. Create operation shell scripts that use the Terraform CLI**
168+
169+
There are three steps to deploy infrastructure via Terraform:
170+
1. `terraform init` - initializes the Terraform CLI to perform actions within the working directory
171+
1. `terraform plan` - develops a plan based on the incoming Terraform infrastructure files and variables, and any existing state files, and develops steps needed to create or update infrastructure specified in the *.tf* files
172+
1. `terraform apply` - applies the plan to create new or update existing infrastructure in Azure
173+
174+
During the core image's entrypoint, any existing state files are pulled into the container and the directory saved under the environment variable ```$ADE_STORAGE```. Additionally, any parameters set for the current environment stored under the variable ```$ADE_OPERATION_PARAMETERS```. In order to access the existing state file, and set your variables within a *.tfvars.json* file, run the following commands:
175+
```bash
176+
EnvironmentState="$ADE_STORAGE/environment.tfstate"
177+
EnvironmentPlan="/environment.tfplan"
178+
EnvironmentVars="/environment.tfvars.json"
179+
180+
echo "$ADE_OPERATION_PARAMETERS" > $EnvironmentVars
181+
```
182+
183+
Additionally, to utilize ADE privileges to deploy infrastructure inside your subscription, your script needs to use the Managed Service Identity (MSI) when provisioning infrastructure by using the Terraform AzureRM provider. If your deployment needs special permissions to complete your deployment, such as particular roles, assign those permissions to the project environment type's identity that is being used for your environment deployment. ADE sets the relevant environment variables, such as the client, tenant, and subscription IDs within the core image's entrypoint, so run the following commands to ensure the provider uses the ADE MSI:
184+
```bash
185+
export ARM_USE_MSI=true
186+
export ARM_CLIENT_ID=$ADE_CLIENT_ID
187+
export ARM_TENANT_ID=$ADE_TENANT_ID
188+
export ARM_SUBSCRIPTION_ID=$ADE_SUBSCRIPTION_ID
189+
```
190+
191+
If you have other variables to reference within your template that aren't specified in your environment's parameters, set environment variables using the prefix *TF_VAR*. A list of provided ADE environment variables is provided [Azure Deployment Environment CLI variables reference](./reference-deployment-environment-variables.md). An example of those commands could be;
192+
```bash
193+
export TF_VAR_resource_group_name=$ADE_RESOURCE_GROUP_NAME
194+
export TF_VAR_ade_env_name=$ADE_ENVIRONMENT_NAME
195+
export TF_VAR_env_name=$ADE_ENVIRONMENT_NAME
196+
export TF_VAR_ade_subscription=$ADE_SUBSCRIPTION_ID
197+
export TF_VAR_ade_location=$ADE_ENVIRONMENT_LOCATION
198+
export TF_VAR_ade_environment_type=$ADE_ENVIRONMENT_TYPE
199+
```
200+
201+
Now, you can run the steps listed previously to initialize the Terraform CLI, generate a plan for provisioning infrastructure, and apply a plan during your deployment script:
202+
```bash
203+
terraform init
204+
terraform plan -no-color -compact-warnings -refresh=true -lock=true -state=$EnvironmentState -out=$EnvironmentPlan -var-file="$EnvironmentVars"
205+
terraform apply -no-color -compact-warnings -auto-approve -lock=true -state=$EnvironmentState $EnvironmentPlan
206+
```
207+
208+
During your deletion script, you can add the `destroy` flag to your plan generation to delete the existing resources, as shown in the following example:
209+
```bash
210+
terraform init
211+
terraform plan -no-color -compact-warnings -destroy -refresh=true -lock=true -state=$EnvironmentState -out=$EnvironmentPlan -var-file="$EnvironmentVars"
212+
terraform apply -no-color -compact-warnings -auto-approve -lock=true -state=$EnvironmentState $EnvironmentPlan
213+
```
214+
215+
Finally, to make the outputs of your deployment uploaded and accessible when accessing your environment via the Azure CLI, transform the output object from Terraform to the ADE-specified format through the JQ package. Set the value to the $ADE_OUTPUTS environment variable, as shown in the following example:
216+
```bash
217+
tfOutputs=$(terraform output -state=$EnvironmentState -json)
218+
# Convert Terraform output format to ADE format.
219+
tfOutputs=$(jq 'walk(if type == "object" then
220+
if .type == "bool" then .type = "boolean"
221+
elif .type == "list" then .type = "array"
222+
elif .type == "map" then .type = "object"
223+
elif .type == "set" then .type = "array"
224+
elif (.type | type) == "array" then
225+
if .type[0] == "tuple" then .type = "array"
226+
elif .type[0] == "object" then .type = "object"
227+
elif .type[0] == "set" then .type = "array"
228+
else .
229+
end
230+
else .
231+
end
232+
else .
233+
end)' <<< "$tfOutputs")
234+
235+
echo "{\"outputs\": $tfOutputs}" > $ADE_OUTPUTS
236+
```
237+
::: zone-end
238+
111239

112240
## Make the custom image available to ADE
113241

File renamed without changes.

0 commit comments

Comments
 (0)