Skip to content

Commit 6c10ae0

Browse files
authored
Added EC2 and ECS-FireLens examples (#32)
* Added EC2 and ECS-FireLens examples
1 parent 674befd commit 6c10ae0

File tree

17 files changed

+414
-5
lines changed

17 files changed

+414
-5
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ build
1010
*.iml
1111
out
1212
.settings
13-
13+
.temp

examples/README.md

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,48 @@ export AWS_REGION=us-west-2
99
export LAMBDA_ARN="arn:aws:lambda:$AWS_REGION:<AccountId>:function:<FunctionName>"
1010
./examples/lambda/deploy/deploy-lambda.sh $LAMBDA_ARN $AWS_REGION
1111
```
12-
13-
## TODO: Add more environments
12+
13+
## Agent
14+
15+
In order to run this example you will need the CloudWatch Agent running locally.
16+
The easiest way to do this is by running it in a Docker container using the following script.
17+
Alternatively, you can find installation instructions [here](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/install-CloudWatch-Agent-on-EC2-Instance.html).
18+
19+
```sh
20+
export AWS_ACCESS_KEY_ID=
21+
export AWS_SECRET_ACCESS_KEY=
22+
export AWS_REGION=us-west-2
23+
./bin/start-agent.sh
24+
```
25+
26+
Run the example:
27+
28+
```
29+
./examples/agent/bin/run.sh
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+
```

examples/agent/bin/run.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
#!/usr/bin/env bash
3+
4+
rootdir=$(git rev-parse --show-toplevel)
5+
6+
pushd $rootdir/
7+
export AWS_EMF_LOG_GROUP_NAME=AgentDemo
8+
export AWS_EMF_LOG_STREAM_NAME=local
9+
export AWS_EMF_SERVICE_NAME=Demo
10+
export AWS_EMF_SERVICE_TYPE=local
11+
export AWS_EMF_AGENT_ENDPOINT=tcp://0.0.0.0:25888
12+
./gradlew :examples:agent:run
13+
popd

examples/agent/build.gradle

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* This file was generated by the Gradle 'init' task.
3+
*
4+
* This generated file contains a sample Java project to get you started.
5+
* For more details take a look at the Java Quickstart chapter in the Gradle
6+
* User Manual available at https://docs.gradle.org/6.5.1/userguide/tutorial_java_projects.html
7+
*/
8+
9+
plugins {
10+
id 'java'
11+
12+
id 'application'
13+
}
14+
15+
repositories {
16+
jcenter()
17+
}
18+
19+
dependencies {
20+
implementation rootProject
21+
implementation "org.apache.logging.log4j:log4j-api:2.13.3"
22+
implementation "org.apache.logging.log4j:log4j-core:2.13.3"
23+
implementation "org.apache.logging.log4j:log4j-slf4j-impl:2.13.3"
24+
}
25+
26+
application {
27+
mainClassName = 'agent.App'
28+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package agent;
2+
3+
import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger;
4+
import software.amazon.cloudwatchlogs.emf.model.DimensionSet;
5+
import software.amazon.cloudwatchlogs.emf.model.Unit;
6+
7+
public class App {
8+
9+
public static void main(String[] args) {
10+
MetricsLogger logger = new MetricsLogger();
11+
logger.putDimensions(DimensionSet.of("Operation", "Agent"));
12+
logger.putMetric("ExampleMetric", 100, Unit.MILLISECONDS);
13+
logger.putProperty("RequestId", "422b1569-16f6-4a03-b8f0-fe3fd9b100f8");
14+
logger.flush();
15+
}
16+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#
2+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License").
5+
# You may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
# Root logger option
18+
log4j.rootLogger=INFO, stdout
19+
20+
# Redirect log messages to console
21+
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
22+
log4j.appender.stdout.Target=System.out
23+
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
24+
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM openjdk:8-jdk-slim
2+
RUN mkdir -p /app
3+
4+
# copy the source files over
5+
COPY build/libs/*.jar /app/app.jar
6+
7+
ENV JAVA_OPTS=""
8+
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app/app.jar" ]
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/env bash
2+
# Usage:
3+
# ./examples/ecs-firelens/publish.sh \
4+
# <account-id> \
5+
# <region> \
6+
# <image-name> \
7+
# <s3-bucket> \
8+
# <ecs-cluster-name> \
9+
# <ecs-task-family> \
10+
# <ecs-service-name>
11+
12+
rootdir=$(git rev-parse --show-toplevel)
13+
14+
ACCOUNT_ID=$1
15+
REGION=$2
16+
IMAGE_NAME=$3 # emf-ecs-firelens
17+
FLUENT_BIT_S3_CONFIG_BUCKET=$4 # aws-emf-ecs-firelens-configurations
18+
CLUSTER_NAME=$5 # emf-example
19+
ECS_TASK_FAMILY=$6 # aws-emf-ecs-app-example
20+
ECS_SERVICE_NAME=$7 # aws-emf-ecs-firelens-ec2
21+
22+
LIB_PATH=$rootdir
23+
EXAMPLE_DIR=$rootdir/examples/ecs-firelens
24+
ECR_REMOTE=$ACCOUNT_ID.dkr.ecr.$REGION.amazonaws.com/$IMAGE_NAME
25+
26+
function check_exit() {
27+
last_exit_code=$?
28+
if [ $last_exit_code -ne 0 ];
29+
then
30+
echo "Last command failed with exit code: $last_exit_code."
31+
echo "Exiting."
32+
exit $last_exit_code;
33+
fi
34+
}
35+
36+
echo 'BUILDING THE LOCAL PROJECT'
37+
pushd $rootdir
38+
./gradlew :examples:ecs-firelens:build
39+
check_exit
40+
popd
41+
42+
pushd $EXAMPLE_DIR
43+
pwd
44+
45+
46+
echo 'UPDATING CONTAINER DEFINITIONS'
47+
sed "s/<account-id>/$ACCOUNT_ID/g" $EXAMPLE_DIR/container-definitions.template.json \
48+
| sed "s/<region>/$REGION/g" \
49+
| sed "s/<s3-bucket>/$FLUENT_BIT_S3_CONFIG_BUCKET/g" \
50+
| sed "s/<image-name>/$IMAGE_NAME/g" \
51+
> $EXAMPLE_DIR/container-definitions.json
52+
check_exit
53+
54+
echo 'PUSHING FLUENT BIT CONFIG TO S3'
55+
aws s3 cp fluent-bit.conf s3://$FLUENT_BIT_S3_CONFIG_BUCKET --region $REGION
56+
check_exit
57+
58+
echo 'BUILDING THE EXAMPLE DOCKER IMAGE'
59+
`aws ecr get-login --no-include-email --region $REGION`
60+
docker build . -t $IMAGE_NAME:latest
61+
check_exit
62+
63+
echo 'PUSHING THE EXAMPLE DOCKER IMAGE TO ECR'
64+
imageid=$(docker images -q $IMAGE_NAME:latest)
65+
docker tag $imageid $ECR_REMOTE
66+
docker push $ECR_REMOTE
67+
check_exit
68+
69+
echo 'UPDATING THE ECS SERVICE'
70+
aws ecs update-service \
71+
--region $REGION \
72+
--cluster $CLUSTER_NAME \
73+
--service $ECS_SERVICE_NAME \
74+
--force-new-deployment \
75+
--task-definition $(aws ecs register-task-definition \
76+
--network-mode bridge \
77+
--requires-compatibilities EC2 \
78+
--task-role arn:aws:iam::$ACCOUNT_ID:role/ecsTaskExecutionRole \
79+
--execution-role-arn "arn:aws:iam::$ACCOUNT_ID:role/ecsTaskExecutionRole" \
80+
--region $REGION \
81+
--memory 256 \
82+
--cpu '1 vcpu' \
83+
--family $ECS_TASK_FAMILY \
84+
--container-definitions "$(cat container-definitions.json)" \
85+
| jq --raw-output '.taskDefinition.taskDefinitionArn' | awk -F '/' '{ print $2 }')
86+
87+
popd

examples/ecs-firelens/build.gradle

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
plugins {
18+
id 'java'
19+
id 'application'
20+
21+
}
22+
23+
repositories {
24+
jcenter()
25+
}
26+
27+
28+
dependencies {
29+
implementation rootProject
30+
implementation "org.apache.logging.log4j:log4j-api:2.13.3"
31+
implementation "org.apache.logging.log4j:log4j-core:2.13.3"
32+
implementation "org.apache.logging.log4j:log4j-slf4j-impl:2.13.3"
33+
}
34+
35+
jar {
36+
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
37+
38+
manifest {
39+
attributes 'Main-Class': 'App',
40+
'Implementation-Version': archiveVersion.get()
41+
}
42+
43+
from files(sourceSets.main.output.classesDirs)
44+
from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
45+
}
46+
47+

0 commit comments

Comments
 (0)