Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moving to container based app on EC2 instead of directly running on the instance #62

Merged
merged 41 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
7d5b8e7
using systemd instead of sysvinit
Feb 12, 2025
5dc9946
using systemctl commands
Feb 12, 2025
c0e0cc0
starting back with validating ec2
Feb 13, 2025
f98ebc4
adding ec2 fleet
Feb 13, 2025
59e3c00
add app and alb listener
Feb 13, 2025
a3e3ca1
removing ec2
Feb 13, 2025
893bcec
adding ec2 back
Feb 13, 2025
ef494f3
taking ec2 out again
Feb 13, 2025
75f6a79
add ec2 with new configs
Feb 13, 2025
bdbd30d
try ec2 replace again
Feb 13, 2025
7fbd444
ec2 logging to localhost
Feb 13, 2025
1de31ec
update
Feb 13, 2025
5258c59
no ec2
Feb 13, 2025
b664017
use ec2
Feb 13, 2025
84aaef3
removing ec2
Feb 13, 2025
eb020cd
remove ec2
Feb 13, 2025
c72416a
add ec2
Feb 13, 2025
065a4a8
fixing port assignment
Feb 13, 2025
5fd3d66
Remove ec2
Feb 13, 2025
141e212
adding ec2 with new configurations
Feb 13, 2025
33da28e
removing ec2 again
Feb 13, 2025
eea0e74
adding ec2 with new cw config
Feb 13, 2025
f08adbc
removing setting log level
Feb 13, 2025
6393ad0
removing ec2
Feb 13, 2025
0d47cd7
deploying app on ec2
Feb 13, 2025
175ac1f
deploying on ec2, no containers
Feb 13, 2025
8e393a9
removing ec2 and app
Feb 13, 2025
797e5ad
deploying app on ec2
Feb 13, 2025
e510260
remove ec2 again
Feb 13, 2025
00bb7dc
deploying ec2 with new emf config
Feb 13, 2025
593c5d9
remove ec2
Feb 13, 2025
a442549
adding ec2 with containers
Feb 13, 2025
6775690
removing ec2
Feb 14, 2025
36f619e
adding ec2 back
Feb 14, 2025
f06bebe
updated emf configuration
Feb 14, 2025
92c09a2
adding back the rest of the stack
Feb 14, 2025
7d569ce
removing ssm param temporarily
Feb 14, 2025
cb2168d
add failing deployment asset back and configuring xray
Feb 14, 2025
018a64a
updated configuration files
Feb 14, 2025
19f8f50
removing port mapping from cw agent
Feb 14, 2025
250e54f
fixing otlp typo
Feb 14, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
CDK_LOCATION: "cdk"
PROJECT_NAME: ${{ github.event.repository.name }}
DOTNET_SYSTEM_GLOBALIZATION_INVARIANT: 0
BUILD_APP: false
steps:
- name: Checkout code
uses: actions/checkout@v4
Expand Down Expand Up @@ -150,6 +151,7 @@ jobs:
run: curl -SL https://github.com/docker/compose/releases/download/v2.32.4/docker-compose-linux-aarch64 -o ${{ github.workspace }}/assets/docker-compose

- name: Build arm64 web app
if: env.BUILD_APP == true
run: |
rm -rf ${{ github.workspace }}/app-src/output
mkdir -p ${{ github.workspace }}/app-src/output
Expand All @@ -164,6 +166,7 @@ jobs:
rm -rf ${{ github.workspace }}/app-src/output
- name: Build failing arm64 web app
if: env.BUILD_APP == true
run: |
rm -rf ${{ github.workspace }}/app-src/output
mkdir -p ${{ github.workspace }}/app-src/output
Expand Down
11 changes: 0 additions & 11 deletions app-src/Program.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
using Amazon.CloudWatch.EMF.Config;
using BAMCIS.MultiAZApp.Utilities;

var builder = WebApplication.CreateBuilder(args);

EnvironmentConfigurationProvider.Config = new Configuration
{
ServiceName = Constants.SERVICE_NAME,
LogGroupName = Constants.LOG_GROUP_NAME,
ServiceType = "WebApi",
EnvironmentOverride = builder.Environment.IsDevelopment()
? Amazon.CloudWatch.EMF.Environment.Environments.Local
: Amazon.CloudWatch.EMF.Environment.Environments.EC2
};

builder.WebHost.ConfigureKestrel((context, serverOptions) => {
serverOptions.AddServerHeader = true;
serverOptions.ListenAnyIP(5000);
Expand Down
25 changes: 23 additions & 2 deletions app-src/Utilities/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using Amazon.CloudWatch.EMF.Config;
using Amazon.CloudWatch.EMF.Logger;
using Amazon.CloudWatch.EMF.Model;
using Amazon.XRay.Recorder.Core;
Expand Down Expand Up @@ -57,14 +58,34 @@ public static WebApplicationBuilder RegisterServices(this WebApplicationBuilder
// cache refresh worker
builder.Services.AddHostedService<BackgroundWorker>();
builder.Services.AddControllers();
builder.Services.AddEmf();

//builder.Services.AddSerilog();
builder.Services.AddEmf(builder.Environment);
builder.Services.AddOpenApi();

return builder;
}

private static void AddEmf(this IServiceCollection services)
private static void AddEmf(this IServiceCollection services, IWebHostEnvironment env)
{
Console.WriteLine("IS DEVELOPMENT: " + env.IsDevelopment());
Console.WriteLine("ENV: " + env.EnvironmentName);
EnvironmentConfigurationProvider.Config = new Configuration(
serviceName: Constants.SERVICE_NAME,
serviceType: "WebApi",
logGroupName: Constants.LOG_GROUP_NAME,
logStreamName: String.IsNullOrEmpty(System.Environment.GetEnvironmentVariable("AWS_EMF_LOG_STREAM_NAME")) ?
String.Empty :
System.Environment.GetEnvironmentVariable("AWS_EMF_LOG_STREAM_NAME"),
agentEndPoint: String.IsNullOrEmpty(System.Environment.GetEnvironmentVariable("AWS_EMF_AGENT_ENDPOINT")) ?
Amazon.CloudWatch.EMF.Sink.Endpoint.DEFAULT_TCP_ENDPOINT.ToString() :
System.Environment.GetEnvironmentVariable("AWS_EMF_AGENT_ENDPOINT"),
agentBufferSize: Configuration.DEFAULT_AGENT_BUFFER_SIZE,
environmentOverride: env != null && env.IsDevelopment() ?
Amazon.CloudWatch.EMF.Environment.Environments.Local :
Amazon.CloudWatch.EMF.Environment.Environments.Agent
);

services.AddScoped<IMetricsLogger, MetricsLogger>();
services.AddSingleton<Amazon.CloudWatch.EMF.Environment.IEnvironmentProvider, Amazon.CloudWatch.EMF.Environment.EnvironmentProvider>();
services.AddSingleton<Amazon.CloudWatch.EMF.Environment.IResourceFetcher, Amazon.CloudWatch.EMF.Environment.ResourceFetcher>();
Expand Down
2 changes: 1 addition & 1 deletion app-src/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Default": "Debug",
"Microsoft.AspNetCore": "Warning"
}
},
Expand Down
9 changes: 5 additions & 4 deletions app-src/docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ services:
ports:
- 5000:5000
environment:
- "AWS_EMF_AGENT_ENDPOINT=tcp://cwagent:25889"
- "AWS_EMF_AGENT_ENDPOINT=tcp://cwagent:25888"
- "AWS_EMF_LOG_STREAM_NAME=${AWS_EMF_LOG_STREAM_NAME}"
- "DB_SECRET=${DB_SECRET}"
- "AWS_XRAY_DAEMON_ADDRESS=cwagent:2000"
- "OTEL_EXPORTER_OTLP_ENDPOINT=http://cwagent:4318"
# healthcheck:
# test: curl --fail --location http://localhost:5000/health || exit 1
# interval: 5s
Expand All @@ -22,7 +25,5 @@ services:
mem_limit: 256M
cpus: 0.25
restart: always
ports:
- 25889:25889/tcp
environment:
- "CW_CONFIG_CONTENT={\"logs\": {\"metrics_collected\": {\"emf\": { \"service_address\": \"tcp://0.0.0.0:25889\" }}}}"
- "CW_CONFIG_CONTENT={\"logs\": {\"metrics_collected\": {\"emf\": { } } }, \"traces\": { \"traces_collected\": {\"xray\": {}, \"otlp\": {} }}}"
10 changes: 5 additions & 5 deletions app-src/docker/load-container.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/bin/bash

rootDirectory="/opt/codedeploy-agent/deployment-root"
app="$rootDirectory/$DEPLOYMENT_GROUP_ID/$DEPLOYMENT_ID/deployment-archive/container.tar.gz"
cw="$rootDirectory/$DEPLOYMENT_GROUP_ID/$DEPLOYMENT_ID/deployment-archive/cloudwatch-agent.tar.gz"
ROOT_DIR="/opt/codedeploy-agent/deployment-root"
APP="$ROOT_DIR/$DEPLOYMENT_GROUP_ID/$DEPLOYMENT_ID/deployment-archive/container.tar.gz"
CW="$ROOT_DIR/$DEPLOYMENT_GROUP_ID/$DEPLOYMENT_ID/deployment-archive/cloudwatch-agent.tar.gz"

docker load < $app
docker load < $cw
/usr/bin/docker load < $APP
/usr/bin/docker load < $CW
2 changes: 1 addition & 1 deletion app-src/docker/remove-container.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

rootDirectory="/opt/codedeploy-agent/deployment-root"
compose="$rootDirectory/$DEPLOYMENT_GROUP_ID/$DEPLOYMENT_ID/deployment-archive/docker/docker-compose.yml"
docker compose --file $compose rm --force --stop --volumes
/usr/bin/docker compose --file $compose rm --force --stop --volumes
13 changes: 8 additions & 5 deletions app-src/docker/start-container.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#!/bin/bash

rootDirectory="/opt/codedeploy-agent/deployment-root"
compose="$rootDirectory/$DEPLOYMENT_GROUP_ID/$DEPLOYMENT_ID/deployment-archive/docker/docker-compose.yml"
env="$rootDirectory/$DEPLOYMENT_GROUP_ID/$DEPLOYMENT_ID/deployment-archive/docker/.env"
ROOT_DIR="/opt/codedeploy-agent/deployment-root"
COMPOSE_FILE="$ROOT_DIR/$DEPLOYMENT_GROUP_ID/$DEPLOYMENT_ID/deployment-archive/docker/docker-compose.yml"
ENV_FILE="$ROOT_DIR/$DEPLOYMENT_GROUP_ID/$DEPLOYMENT_ID/deployment-archive/docker/.env"
TOKEN=$(curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600")
INSTANCE_ID=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" -s http://169.254.169.254/latest/meta-data/instance-id)
DB_SECRET=$(cat /etc/secret)
echo "DB_SECRET=$DB_SECRET" >> $env
docker compose --file $compose --env-file $env up --detach
echo "DB_SECRET=$DB_SECRET" >> $ENV_FILE
echo "AWS_EMF_LOG_STREAM_NAME=$INSTANCE_ID" >> $ENV_FILE
/usr/bin/docker compose --file $COMPOSE_FILE --env-file $ENV_FILE up --detach
4 changes: 2 additions & 2 deletions app-src/docker/stop-container.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ CONTAINER2="cwagent"
# Function to stop a container if it exists
stop_container() {
local container_name=$1
if docker ps -q -f name="^${container_name}$" | grep -q .; then
if /usr/bin/docker ps -q -f name="^${container_name}$" | grep -q .; then
echo "Stopping container: $container_name"
docker stop "$container_name"
/usr/bin/docker stop "$container_name"
else
echo "Container $container_name is not running or does not exist."
fi
Expand Down
2 changes: 2 additions & 0 deletions app-src/multi-az-workshop-application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
<PackageReference Include="AWSSDK.SecretsManager" Version="3.7.400.59" />
<PackageReference Include="Npgsql" Version="9.0.2" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.0" />
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
</ItemGroup>

</Project>
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 10 additions & 4 deletions cdk/Configs/cw-agent-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,35 @@
{
"file_path": "/var/log/cfn-init-cmd.log",
"log_group_name": "cfn-init-cmd.log",
"log_stream_name": "${aws:InstanceId}",
"log_stream_name": "{instance_id}",
"timestamp_format": "%H: %M: %S%y%b%-d"
},
{
"file_path": "/var/log/cfn-init.log",
"log_group_name": "cfn-init.log",
"log_stream_name": "${aws:InstanceId}",
"log_stream_name": "{instance_id}",
"timestamp_format": "%H: %M: %S%y%b%-d"
},
{
"file_path": "/var/log/cloud-init-output.log",
"log_group_name": "cloud-init-output.log",
"log_stream_name": "${aws:InstanceId}",
"log_stream_name": "{instance_id}",
"timestamp_format": "%H: %M: %S%y%b%-d"
},
{
"file_path": "/var/log/cloud-init.log",
"log_group_name": "cloud-init.log",
"log_stream_name": "${aws:InstanceId}",
"log_stream_name": "{instance_id}",
"timestamp_format": "%H: %M: %S%y%b%-d"
}
]
}
}
},
"traces": {
"traces_collected": {
"xray": {},
"otlp": {}
}
}
}
16 changes: 14 additions & 2 deletions cdk/Constructs/EKSApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public EKSApplication(Construct scope, string id, IEKSApplicationProps props) :
{"namespace", props.Namespace}
}},
{"data", new Dictionary<string, object>() {
{"cwagentconfig.json", "{\"agent\":{\"omit_hostname\":true},\"logs\":{\"metrics_collected\":{\"emf\":{}}}}"}
{"cwagentconfig.json", "{\"agent\":{\"omit_hostname\":true},\"logs\":{\"metrics_collected\":{\"emf\":{}}},\"traces\":{\"traces_collected\":{\"xray\":{},\"otlp\":{}}}}"}
}}
}
});
Expand Down Expand Up @@ -363,7 +363,19 @@ public EKSApplication(Construct scope, string id, IEKSApplicationProps props) :
}},
{"ports", new Dictionary<string, object>[] {
new Dictionary<string, object>() {
{"containerPort", 25888},
{"containerPort", 25888}, // cloudwatch agent
{"protocol", "TCP"}
},
new Dictionary<string, object>() {
{"containerPort", 2000}, // xray
{"protocol", "TCP"}
},
new Dictionary<string, object>() {
{"containerPort", 4317}, // otlp grpc
{"protocol", "TCP"}
},
new Dictionary<string, object>() {
{"containerPort", 4318}, // otlp http
{"protocol", "TCP"}
}
}}
Expand Down
Loading