Skip to content

Commit 730bbae

Browse files
authored
add ECS environment with FireLens support (#28)
bump TypeScript to 3.8 bump lint tooling and fix-lint Add ECSEnvironment tests use mocks in the environment detector tests update docs npm audit fix bump package version to 2.0.0
1 parent 2231fa9 commit 730bbae

17 files changed

+1219
-104
lines changed

examples/README.md

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,58 @@ Run the example:
2727

2828
```
2929
./examples/agent/run.sh
30-
```
30+
```
31+
32+
## FireLens on ECS
33+
34+
You can deploy the example by running the following:
35+
36+
```sh
37+
# create an ECR repository for the example image
38+
aws ecr create-repository --repository-name <image-name> --region <region>
39+
40+
# create an S3 bucket for the Fluent-Bit configuration
41+
aws s3api create-bucket --bucket <bucket-name> --region <region>
42+
43+
# create ECS cluster
44+
# create ECS task definition
45+
# create ECS service
46+
47+
# deploy
48+
./examples/ecs-firelens/publish.sh \
49+
<account-id> \
50+
<region> \
51+
<image-name> \
52+
<s3-bucket> \
53+
<ecs-cluster-name> \
54+
<ecs-task-family> \
55+
<ecs-service-name>
56+
```
57+
58+
### Example Metrics
59+
60+
```json
61+
{
62+
"_aws": {
63+
"Timestamp": 1583902595342,
64+
"CloudWatchMetrics": [
65+
{
66+
"Dimensions": [[ "ServiceName", "ServiceType" ]],
67+
"Metrics": [{ "Name": "ProcessingTime", "Unit": "Milliseconds" }],
68+
"Namespace": "aws-embedded-metrics"
69+
}
70+
]
71+
},
72+
"ServiceName": "example",
73+
"ServiceType": "AWS::ECS::Container",
74+
"Method": "GET",
75+
"Url": "/test",
76+
"containerId": "702e4bcf1345",
77+
"createdAt": "2020-03-11T04:54:24.981207801Z",
78+
"startedAt": "2020-03-11T04:54:25.594413051Z",
79+
"image": "<account-id>.dkr.ecr.<region>.amazonaws.com/emf-examples:latest",
80+
"cluster": "emf-example",
81+
"taskArn": "arn:aws:ecs:<region>:<account-id>:task/2fe946f6-8a2e-41a4-8fec-c4983bad8f74",
82+
"ProcessingTime": 5
83+
}
84+
```

examples/ecs-firelens/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
container-definitions.json

examples/ecs-firelens/Dockerfile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM node:10.16.0-alpine AS base
2+
RUN mkdir -p /app/src
3+
WORKDIR /app/src
4+
5+
COPY package.json ./
6+
# install packages but copy the local version of the package in directly
7+
RUN npm i && rm -rf node_modules/aws-embedded-metrics
8+
COPY node_modules/aws-embedded-metrics ./node_modules/aws-embedded-metrics
9+
10+
# copy the source files over
11+
COPY . .
12+
13+
CMD [ "node", "app" ]

examples/ecs-firelens/app.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const Koa = require('koa');
2+
const app = new Koa();
3+
4+
const { metricScope, Unit } = require('aws-embedded-metrics');
5+
6+
app.use(
7+
metricScope(metrics => async (ctx, next) => {
8+
const start = Date.now();
9+
10+
await next();
11+
12+
ctx.body = `Hello World ... ${ctx.method} ${ctx.url}\n`;
13+
14+
metrics.setProperty('Method', ctx.method);
15+
metrics.setProperty('Url', ctx.url);
16+
metrics.putMetric('ProcessingTime', Date.now() - start, Unit.Milliseconds);
17+
18+
// send application logs to stdout, FireLens will send this to a different LogGroup
19+
console.log('Completed Request');
20+
}),
21+
);
22+
23+
app.listen(3000);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
[
2+
{
3+
"name": "example",
4+
"image": "<account-id>.dkr.ecr.<region>.amazonaws.com/<image-name>:latest",
5+
"portMappings": [{ "containerPort": 3000, "protocol": "tcp" }],
6+
"essential": true,
7+
"logConfiguration": {
8+
"logDriver": "awsfirelens",
9+
"options": {
10+
"Name": "cloudwatch",
11+
"region": "<region>",
12+
"log_group_name": "aws-emf-ecs-firelens-example-logs",
13+
"auto_create_group": "true",
14+
"log_stream_prefix": "from-fluent-bit"
15+
}
16+
},
17+
"links": ["fluent-bit"]
18+
},
19+
{
20+
"name": "fluent-bit",
21+
"image": "906394416424.dkr.ecr.<region>.amazonaws.com/aws-for-fluent-bit:latest",
22+
"essential": true,
23+
"firelensConfiguration": {
24+
"type": "fluentbit",
25+
"options": {
26+
"config-file-type": "s3",
27+
"config-file-value": "arn:aws:s3:::<s3-bucket>/fluent-bit.conf"
28+
}
29+
}
30+
}
31+
]

examples/ecs-firelens/fluent-bit.conf

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# TCP input used for EMF payloads
2+
[INPUT]
3+
Name tcp
4+
Listen 0.0.0.0
5+
Port 25888
6+
Chunk_Size 32
7+
Buffer_Size 64
8+
Format none
9+
Tag emf-${HOSTNAME}
10+
# This tag is used by the output plugin to determine the LogStream
11+
# including the HOSTNAME is a way to increase the number of LogStreams
12+
# proportional to the number of instances. The maximum throughput on a
13+
# single LogStream is 5 MB/s (max 1 MB at max 5 TPS).
14+
15+
# Output for EMF over TCP -> CloudWatch
16+
[OUTPUT]
17+
Name cloudwatch
18+
Match emf-*
19+
region us-east-1
20+
log_key log
21+
log_group_name aws-emf-ecs-firelens-example-metrics
22+
log_stream_prefix from-fluent-bit-
23+
auto_create_group true
24+
log_format json/emf

0 commit comments

Comments
 (0)