Lab 09a - PaaS Web App (Terraform)¶
목표
Azure App Service (Web App for Containers) 를 Terraform으로 배포하고, 강사 공유 ACR의 컨테이너 이미지를 Managed Identity 인증으로 Pull하여 실행합니다. PPT M9 (Administer PaaS Compute Options) 모듈의 직접 실습 대응입니다.
Architecture Diagram¶
Resources Created¶
terraform apply 로 생성되는 리소스: 3개
| 리소스 | 기본 이름/값 |
|---|---|
| App Service Plan | {prefix}-plan (Linux, B1 SKU) |
| Linux Web App | {prefix}-{random6} (Container mode, System Assigned MI 포함) |
| random_string | Web App 전역 고유 이름 suffix (6자리) |
AcrPull 역할 할당은 Terraform 코드에서 제외되어 있습니다. 강사가
instructor-acr-role-assign.sh로 별도 부여합니다.
Web App for Containers
일반 App Service는 코드(ZIP deploy)를 실행하지만, Web App for Containers는 Docker 이미지를 직접 실행합니다. ACR 이미지 주소와 WEBSITES_PORT 환경변수만 설정하면 별도 빌드 없이 컨테이너가 기동됩니다.
비용 주의
App Service Plan은 VM과 동일하게 실행 중이면 시간당 과금됩니다. 실습이 끝나면 반드시 terraform destroy로 정리하세요.
Prerequisite¶
| 항목 | 설명 |
|---|---|
| 권한 | 할당된 Resource Group에 Contributor 이상 |
| 도구 | Azure CLI 설치, Terraform 1.5+ 설치 |
| 인증 | az login 후 올바른 subscription 선택 |
| 선행 Lab | Lab 00 - Skeleton으로 RG 연결 검증 권장 |
| 공유 ACR 이미지 | 강사가 사전 push — az acr repository list --name <ACR> |
Concept Mapping (Official Lab 09)¶
| 공식 Lab 09 주제 | 이 모듈에서의 위치 |
|---|---|
| Web App 배포 (App Service Plan) | main.tf - azurerm_service_plan |
| 컨테이너 이미지 배포 | main.tf - azurerm_linux_web_app (docker config) |
| Managed Identity | main.tf - Web App identity block |
| ACR 연동 | main.tf - azurerm_role_assignment (AcrPull) |
| Container Instances (ACI) | 본 모듈 범위 밖 — PPT에서 비교 설명 |
| Container Apps | 본 모듈 범위 밖 — PPT에서 비교 설명 |
AKS 배포와의 비교¶
| 항목 | Web App for Containers (이 Lab) | AKS (Lab 90-92) |
|---|---|---|
| 컨테이너 단위 | 단일 컨테이너 (1 Web App = 1 image) | Pod (multi-container 가능) |
| 오케스트레이션 | 없음 (PaaS가 관리) | Kubernetes (Deployment, Service, Ingress) |
| 스케일링 | App Service Plan 스케일 (수동/자동) | HPA, Cluster Autoscaler |
| 네트워크 | App Service 기본 도메인 + Custom Domain | Service + Ingress Controller |
| 적합한 워크로드 | 단일 서비스, API backend, 간단한 웹 앱 | 마이크로서비스, 복잡한 토폴로지 |
Step-by-Step¶
Terraform 코드 위치: labs/terraform/lab09a-paas-webapp/
1) Prepare variables¶
cd labs/terraform/lab09a-paas-webapp
cp terraform.tfvars.example terraform.tfvars
에디터로 terraform.tfvars를 열고 다음을 설정합니다.
resource_group_name(필수) — 강사가 알려준 본인 RG 이름acr_name(필수) — 강사가 알려준 공유 ACR 이름
2) Login to Azure¶
az login
az account show -o table
3) Initialize Terraform¶
terraform init
예상 출력
Initializing the backend...
Initializing provider plugins...
- Finding hashicorp/azurerm versions matching "~> 4.0"...
- Installing hashicorp/azurerm v4.x.x...
Terraform has been successfully initialized!
4) Plan¶
terraform plan
terraform plan에서 볼 것
- 추가될 리소스 수 — App Service Plan, Linux Web App, Role Assignment 총 3개 (+ data source 2개는 카운트 미포함)
- Web App —
docker_registry_url,docker_image_name확인 - Role Assignment — Web App Managed Identity → ACR에
AcrPull webapp_url—(known after apply)— apply 후 확정됨
5) Apply¶
terraform apply
프롬프트에 yes를 입력합니다. 완료까지 2~3분 소요됩니다.
예상 출력 (요약)
Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
Outputs:
app_service_plan_name = "az104-lab09a-plan"
resource_group_name = "rg-az104-student01"
webapp_name = "az104-lab09a-app"
webapp_principal_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
webapp_url = "https://az104-lab09a-app.azurewebsites.net"
6) Check outputs¶
terraform output
terraform output webapp_url
Verify - Azure Portal¶
- App Service → Overview에서 URL, Status가
Running인지 확인합니다. - 배포 센터(Deployment Center) → 컨테이너 설정, ACR 레지스트리, 이미지 이름이 정확한지 확인합니다.
- Identity → System Assigned가 On이고 Principal ID가 있는지 확인합니다.
- Log stream → 컨테이너 stdout/stderr 실시간 로그를 확인합니다.
Verify - CLI¶
# Web App URL 접근 테스트
curl -s "$(terraform output -raw webapp_url)/health" | head -20
for /f "tokens=*" %a in ('terraform output -raw webapp_url') do curl -s %a/health
초기 시작 지연
Web App for Containers는 최초 이미지 pull + cold start에 30초~2분 걸릴 수 있습니다. 502 응답이 나오면 잠시 대기 후 재시도하세요.
Cleanup (Teardown)¶
terraform destroy
프롬프트에 yes를 입력합니다.
비용 방지
App Service Plan은 시간당 과금됩니다. 실습이 끝나면 반드시 terraform destroy를 실행하세요.
Terraform Files¶
| 파일 | 역할 |
|---|---|
versions.tf |
Terraform / provider 버전 제약 |
providers.tf |
azurerm provider |
variables.tf |
prefix, RG, ACR, 이미지, SKU 등 |
main.tf |
App Service Plan, Web App, Role Assignment |
outputs.tf |
webapp_url, webapp_name, principal_id |
terraform.tfvars.example |
변수 예시 파일 (비밀 값 없음) |
Troubleshooting¶
| 증상 | 원인 | 해결 |
|---|---|---|
| 502 Bad Gateway | 컨테이너 시작 실패 또는 cold start 지연 | Log stream 확인, 2분 대기 후 재시도 |
docker pull 실패 로그 |
ACR Pull 권한 미부여 | Role Assignment 전파 지연(~5분), terraform apply 재실행 |
| 포트 불일치 | 컨테이너가 8090 이 아닌 다른 포트 listen | WEBSITES_PORT 환경변수와 컨테이너 포트 일치 확인 |
| 이미지 not found | container_image 변수 오타 |
az acr repository show-tags --name <ACR> --repository awesome-apigateway |
| Managed Identity 비활성 | Terraform 적용 순서 이슈 (드묾) | terraform apply 재실행 |
Resource Group "xxx" was not found |
RG 이름 오류 또는 다른 subscription | terraform.tfvars와 az account show 확인 |
Cost & Caution¶
실수 방지
terraform.tfvars의 resource_group_name이 반드시 본인 RG인지 확인한 뒤 apply하세요. Web App은 Public URL로 노출되므로, 인증 없는 API 엔드포인트를 방치하지 않도록 실습 외 시간에는 반드시 삭제합니다.
참고: Azure Web App에서의 OpenTelemetry 연계¶
이 Lab에서는 OpenTelemetry 계측을 다루지 않지만, Azure Web App 기반에서 OTel 데이터를 Application Insights로 보내는 방법은 다음과 같습니다.
방법 1 — Azure Monitor OpenTelemetry Distro (권장)¶
각 언어별로 제공되는 Azure Monitor OpenTelemetry Distro/SDK를 앱에 추가합니다.
# .NET 예시
dotnet add package Azure.Monitor.OpenTelemetry.AspNetCore
// Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenTelemetry().UseAzureMonitor();
| 언어 | 패키지 |
|---|---|
| .NET | Azure.Monitor.OpenTelemetry.AspNetCore |
| Java | Azure Monitor OpenTelemetry Distro (auto-attach JAR) |
| Node.js | @azure/monitor-opentelemetry |
| Python | azure-monitor-opentelemetry |
방법 2 — App Service 자동 계측 (Codeless)¶
Azure Portal에서 Application Insights를 연결하면, .NET / Java / Node.js 앱은 코드 변경 없이 자동 계측됩니다.
- App Service → Application Insights → "Turn on" 클릭
- Connection String이 환경변수로 자동 주입
- Traces, Metrics, Logs가 Application Insights로 전송
Java 자동 계측 — "Codeless"의 실제 의미¶
Java의 자동 계측은 소스 코드 수정이 없다는 뜻이지, 아무것도 실행하지 않는다는 뜻이 아닙니다. Java는 JVM에 -javaagent JAR이 attach되어야 바이트코드 수준에서 동적 계측이 이루어집니다.
# Java 자동 계측의 본질 — JAR이 JVM에 로드되어야 동작
java -javaagent:/path/to/applicationinsights-agent-3.7.8.jar -jar myapp.jar
차이는 누가 JAR을 주입하느냐입니다:
| 배포 환경 | JAR 주입 주체 | 개발자가 할 일 |
|---|---|---|
| App Service (비컨테이너, JAR/WAR 배포) | Azure 플랫폼이 자동으로 JAR 다운로드 + JVM 인수 설정 | Portal에서 Application Insights 켜기만 하면 됨 (진짜 codeless) |
| App Service for Containers (이 Lab) | 플랫폼이 환경변수 주입 시도하나, 컨테이너 내부 JVM 인수는 이미지에 의존 | 이미지에 JAR 포함 + JAVA_TOOL_OPTIONS 설정 필요 |
| AKS Autoinstrumentation (Preview) | AKS 플랫폼이 init container로 JAR 주입 + JAVA_TOOL_OPTIONS 환경변수 자동 설정 |
코드/이미지 수정 없음 |
| AKS 직접 구성 (이 과정 Lab 92) | 개발자 | Dockerfile에 JAR 포함 또는 OTel Collector 별도 배포 |
컨테이너 환경에서의 Java 자동 계측
Web App for Containers 또는 AKS에서 Java 앱을 자동 계측하려면, 이미지 빌드 시 JAR을 포함하거나 환경변수로 JVM에 연결해야 합니다.
# Dockerfile 예시
ADD applicationinsights-agent-3.7.8.jar /agent/
ENV JAVA_TOOL_OPTIONS="-javaagent:/agent/applicationinsights-agent-3.7.8.jar"
ENV APPLICATIONINSIGHTS_CONNECTION_STRING="InstrumentationKey=xxx;..."
또는 Terraform app_settings로 환경변수를 주입할 수 있습니다:
app_settings = {
JAVA_TOOL_OPTIONS = "-javaagent:/agent/applicationinsights-agent-3.7.8.jar"
APPLICATIONINSIGHTS_CONNECTION_STRING = "<connection-string>"
}
언어별 "자동 계측" 비교 요약¶
| 언어 | 계측 방식 | 소스 변경 | 런타임 의존성 |
|---|---|---|---|
| .NET | SDK 패키지 + 한 줄 초기화 | UseAzureMonitor() 1줄 |
NuGet 패키지 |
| Java | JVM Agent (바이트코드 계측) | 없음 | JAR 파일이 JVM에 attach 되어야 함 |
| Node.js | SDK 패키지 + 한 줄 초기화 | useAzureMonitor() 1줄 |
npm 패키지 |
| Python | SDK 패키지 + 한 줄 초기화 | configure_azure_monitor() 1줄 |
pip 패키지 |
AKS vs Web App 비교
- Web App (비컨테이너 Java): Portal에서 Application Insights 켜면 Azure가 JAR + JVM 설정 전부 자동 처리 → 진짜 codeless
- Web App for Containers: 이미지에 JAR이 포함되어 있어야 함 → semi-codeless
- AKS Autoinstrumentation (Preview): 플랫폼이 init container로 자동 주입 → codeless (Preview)
- AKS + OTel Collector (Lab 92): 앱에서 OTLP로 export → Collector가 변환 전송 → 가장 유연하지만 설정 복잡