Skip to content

Commit 94e58c5

Browse files
committed
use SSM to replace script
1 parent 3050cd1 commit 94e58c5

File tree

1 file changed

+125
-4
lines changed
  • terraform/dotnet/ec2/windows

1 file changed

+125
-4
lines changed

terraform/dotnet/ec2/windows/main.tf

Lines changed: 125 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,77 @@ resource "aws_ssm_document" "main_service_setup" {
162162
"runCommand": [
163163
"aws s3 cp s3://aws-appsignals-sample-app-prod-${var.aws_region}/amazon-cloudwatch-agent.json ./amazon-cloudwatch-agent.json",
164164
"powershell -Command \"(Get-Content -Path 'amazon-cloudwatch-agent.json') -replace 'REGION', 'us-east-1' | Set-Content -Path 'amazon-cloudwatch-agent.json'\"",
165-
"aws s3 cp s3://aws-appsignals-sample-app-prod-${var.aws_region}/dotnet-ec2-win-main-setup.ps1 ./dotnet-ec2-win-main-setup.ps1",
166-
"powershell -ExecutionPolicy Bypass -File ./dotnet-ec2-win-main-setup.ps1 -GetCloudwatchAgentCommand \"${var.get_cw_agent_msi_command}\" -GetAdotDistroCommand \"${var.get_adot_distro_command}\" -GetSampleAppCommand \"${var.sample_app_zip}\" -TestId \"${var.test_id}\" -RemoteServicePrivateEndpoint \"${aws_instance.remote_service_instance.private_ip}\" -AWSRegion \"${var.aws_region}\""
165+
"$ProgressPreference = 'SilentlyContinue'",
166+
"wget -O dotnet-install.ps1 https://dot.net/v1/dotnet-install.ps1",
167+
".\\dotnet-install.ps1 -Version 8.0.302",
168+
"Invoke-Expression '${var.get_cw_agent_msi_command}'",
169+
"Write-Host 'Installing Cloudwatch Agent'",
170+
"msiexec /i amazon-cloudwatch-agent.msi",
171+
"$timeout = 30",
172+
"$interval = 5",
173+
"$call_cloudwatch = & 'C:\\Program Files\\Amazon\\AmazonCloudWatchAgent\\amazon-cloudwatch-agent-ctl.ps1'",
174+
"$elapsedTime = 0",
175+
"while ($elapsedTime -lt $timeout) {",
176+
" if ($call_cloudwatch) {",
177+
" Start-Sleep -Seconds $interval",
178+
" Write-Host 'Install Finished'",
179+
" break",
180+
" } else {",
181+
" $call_cloudwatch = & 'C:\Program Files\Amazon\AmazonCloudWatchAgent\amazon-cloudwatch-agent-ctl.ps1'",
182+
" Write-Host 'Cloudwatch Agent not found. Checking again in $interval seconds...'",
183+
" Start-Sleep -Seconds $interval",
184+
" $elapsedTime += $interval",
185+
" }",
186+
"}",
187+
"if ($elapsedTime -ge $timeout) {",
188+
" Write-Host 'CloudWatch not found after $timeout seconds.'",
189+
"}",
190+
"& 'C:\\Program Files\\Amazon\\AmazonCloudWatchAgent\\amazon-cloudwatch-agent-ctl.ps1' -a fetch-config -m ec2 -s -c file:./amazon-cloudwatch-agent.json",
191+
"Invoke-Expression '${var.get_adot_distro_command}'",
192+
"Invoke-Expression '${var.sample_app_zip}'",
193+
"Expand-Archive -Path .\\dotnet-sample-app.zip -DestinationPath .\\ -Force",
194+
"$current_dir = Get-Location",
195+
"Write-Host $current_dir",
196+
"Set-Location -Path './asp_frontend_service'",
197+
"$env:CORECLR_ENABLE_PROFILING = '1'",
198+
"$env:CORECLR_PROFILER = '{918728DD-259F-4A6A-AC2B-B85E1B658318}'",
199+
"$env:CORECLR_PROFILER_PATH = '$current_dir\\dotnet-distro\\win-x64\\OpenTelemetry.AutoInstrumentation.Native.dll'",
200+
"$env:DOTNET_ADDITIONAL_DEPS = '$current_dir\\dotnet-distro\\AdditionalDeps'",
201+
"$env:DOTNET_SHARED_STORE = '$current_dir\\dotnet-distro\\store'",
202+
"$env:DOTNET_STARTUP_HOOKS = '$current_dir\\dotnet-distro\\net\\OpenTelemetry.AutoInstrumentation.StartupHook.dll'",
203+
"$env:OTEL_DOTNET_AUTO_HOME = '$current_dir\\dotnet-distro'",
204+
"$env:OTEL_DOTNET_AUTO_PLUGINS = 'AWS.Distro.OpenTelemetry.AutoInstrumentation.Plugin, AWS.Distro.OpenTelemetry.AutoInstrumentation'",
205+
"$env:OTEL_EXPORTER_OTLP_PROTOCOL = 'http/protobuf'",
206+
"$env:OTEL_EXPORTER_OTLP_ENDPOINT = 'http://127.0.0.1:4316'",
207+
"$env:OTEL_AWS_APPLICATION_SIGNALS_EXPORTER_ENDPOINT = 'http://127.0.0.1:4316/v1/metrics'",
208+
"$env:OTEL_METRICS_EXPORTER = 'none'",
209+
"$env:OTEL_RESOURCE_ATTRIBUTES = 'service.name=dotnet-sample-application-${var.test_id}'",
210+
"$env:OTEL_AWS_APPLICATION_SIGNALS_ENABLED = 'true'",
211+
"$env:OTEL_TRACES_SAMPLER = 'always_on'",
212+
"$env:ASPNETCORE_URLS = 'http://0.0.0.0:8080'",
213+
"dotnet build",
214+
"Start-Process -FilePath 'dotnet' -ArgumentList 'bin/Debug/netcoreapp8.0/asp_main_service.dll'",
215+
"Write-Host 'Start Sleep'",
216+
"Start-Sleep -Seconds 30",
217+
"wget -O nodejs.zip https://nodejs.org/dist/v20.16.0/node-v20.16.0-win-x64.zip",
218+
"Expand-Archive -Path .\\nodejs.zip -DestinationPath .\\nodejs -Force",
219+
"$currentdir = Get-Location",
220+
"Write-Host $currentdir",
221+
"$env:Path += ';$currentdir\\nodejs\\node-v20.16.0-win-x64'",
222+
223+
"# Bring in the traffic generator files to EC2 Instance",
224+
"aws s3 cp 's3://aws-appsignals-sample-app-prod-${var.aws_region}/traffic-generator.zip' './traffic-generator.zip'",
225+
"Expand-Archive -Path './traffic-generator.zip' -DestinationPath './' -Force",
226+
227+
"# Install the traffic generator dependencies",
228+
"npm install",
229+
230+
"# Start traffic generator",
231+
"$env:MAIN_ENDPOINT = 'localhost:8080'",
232+
"$env:REMOTE_ENDPOINT = '${aws_instance.remote_service_instance.private_ip}'",
233+
"$env:ID = '${var.test_id}'",
234+
235+
"Start-Process -FilePath 'npm' -ArgumentList 'start'",
167236
]
168237
}
169238
}
@@ -200,8 +269,60 @@ resource "aws_ssm_document" "remote_service_setup" {
200269
"runCommand": [
201270
"aws s3 cp s3://aws-appsignals-sample-app-prod-${var.aws_region}/amazon-cloudwatch-agent.json ./amazon-cloudwatch-agent.json",
202271
"powershell -Command \"(Get-Content -Path 'amazon-cloudwatch-agent.json') -replace 'REGION', 'us-east-1' | Set-Content -Path 'amazon-cloudwatch-agent.json'\"",
203-
"aws s3 cp s3://aws-appsignals-sample-app-prod-${var.aws_region}/dotnet-ec2-win-remote-setup.ps1 ./dotnet-ec2-win-remote-setup.ps1",
204-
"powershell -ExecutionPolicy Bypass -File ./dotnet-ec2-win-remote-setup.ps1 -GetCloudwatchAgentCommand \"${var.get_cw_agent_msi_command}\" -GetAdotDistroCommand \"${var.get_adot_distro_command}\" -GetSampleAppCommand \"${var.sample_app_zip}\" -TestId \"${var.test_id}\""
272+
"$ProgressPreference = 'SilentlyContinue'",
273+
"wget -O dotnet-install.ps1 https://dot.net/v1/dotnet-install.ps1",
274+
".\\dotnet-install.ps1 -Version 8.0.302",
275+
"Invoke-Expression '${var.get_cw_agent_msi_command}'",
276+
"Write-Host 'Installing Cloudwatch Agent'",
277+
"msiexec /i amazon-cloudwatch-agent.msi",
278+
"$timeout = 30",
279+
"$interval = 5",
280+
"$call_cloudwatch = & 'C:\\Program Files\\Amazon\\AmazonCloudWatchAgent\\amazon-cloudwatch-agent-ctl.ps1'",
281+
"$elapsedTime = 0",
282+
"while ($elapsedTime -lt $timeout) {",
283+
" if ($call_cloudwatch) {",
284+
" Start-Sleep -Seconds $interval",
285+
" Write-Host 'Install Finished'",
286+
" break",
287+
" } else {",
288+
" $call_cloudwatch = & 'C:\Program Files\Amazon\AmazonCloudWatchAgent\amazon-cloudwatch-agent-ctl.ps1'",
289+
" Write-Host 'Cloudwatch Agent not found. Checking again in $interval seconds...'",
290+
" Start-Sleep -Seconds $interval",
291+
" $elapsedTime += $interval",
292+
" }",
293+
"}",
294+
"if ($elapsedTime -ge $timeout) {",
295+
" Write-Host 'CloudWatch not found after $timeout seconds.'",
296+
"}",
297+
"& 'C:\\Program Files\\Amazon\\AmazonCloudWatchAgent\\amazon-cloudwatch-agent-ctl.ps1' -a fetch-config -m ec2 -s -c file:./amazon-cloudwatch-agent.json",
298+
"Invoke-Expression '${var.get_adot_distro_command}'",
299+
"Invoke-Expression '${var.sample_app_zip}'",
300+
"Expand-Archive -Path .\\dotnet-sample-app.zip -DestinationPath .\\ -Force",
301+
"New-NetFirewallRule -DisplayName 'Allow TCP 8081' -Direction Inbound -Protocol TCP -LocalPort 8081 -Action Allow",
302+
"$current_dir = Get-Location",
303+
"Write-Host $current_dir",
304+
"Set-Location -Path './asp_remote_service'",
305+
"$env:CORECLR_ENABLE_PROFILING = '1'",
306+
"$env:CORECLR_PROFILER = '{918728DD-259F-4A6A-AC2B-B85E1B658318}'",
307+
"$env:CORECLR_PROFILER_PATH = '$current_dir\\dotnet-distro\\win-x64\\OpenTelemetry.AutoInstrumentation.Native.dll'",
308+
"$env:DOTNET_ADDITIONAL_DEPS = '$current_dir\\dotnet-distro\\AdditionalDeps'",
309+
"$env:DOTNET_SHARED_STORE = '$current_dir\\dotnet-distro\\store'",
310+
"$env:DOTNET_STARTUP_HOOKS = '$current_dir\\dotnet-distro\\net\\OpenTelemetry.AutoInstrumentation.StartupHook.dll'",
311+
"$env:OTEL_DOTNET_AUTO_HOME = '$current_dir\\dotnet-distro'",
312+
"$env:OTEL_DOTNET_AUTO_PLUGINS = 'AWS.Distro.OpenTelemetry.AutoInstrumentation.Plugin, AWS.Distro.OpenTelemetry.AutoInstrumentation'",
313+
"$env:OTEL_EXPORTER_OTLP_PROTOCOL = 'http/protobuf'",
314+
"$env:OTEL_EXPORTER_OTLP_ENDPOINT = 'http://127.0.0.1:4316'",
315+
"$env:OTEL_AWS_APPLICATION_SIGNALS_EXPORTER_ENDPOINT = 'http://127.0.0.1:4316/v1/metrics'",
316+
"$env:OTEL_METRICS_EXPORTER = 'none'",
317+
"$env:OTEL_RESOURCE_ATTRIBUTES = 'service.name=dotnet-sample-remote-application-${var.test_id}'",
318+
"$env:OTEL_AWS_APPLICATION_SIGNALS_ENABLED = 'true'",
319+
"$env:OTEL_TRACES_SAMPLER = 'always_on'",
320+
"$env:ASPNETCORE_URLS = 'http://0.0.0.0:8081'",
321+
"dotnet build",
322+
"Start-Process -FilePath 'dotnet' -ArgumentList 'bin/Debug/netcoreapp8.0/asp_remote_service.dll'",
323+
"Write-Host 'Start Sleep'",
324+
"Start-Sleep -Seconds 30",
325+
"Write-Host 'Exiting'"
205326
]
206327
}
207328
}

0 commit comments

Comments
 (0)