Skip to content

Commit 10b68ca

Browse files
committed
first commit
0 parents  commit 10b68ca

22 files changed

Lines changed: 1361 additions & 0 deletions

File tree

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 CuliOps
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# AWS Lambda - Logs API Extension for Fluentd
2+
The provided code how to get a basic Logs API extension to send logs to Fluentd written in Golang up and running.
3+
4+
In this extension, we start by developing a simple extension and then add the ability to read logs from the Logs API. For more details on building an extension, please read the Extension API Developer Guide.
5+
6+
When the Lambda service sets up the execution environment, it runs the extension (`fluentd-lambda-extension`). This extension first registers as an extension and then subscribes to the Logs API to receive the logs via HTTP protocol. It starts an HTTP listener which receives the logs and processes them.
7+
8+
## System Compatible
9+
- Architectures: `AMD64` & `ARM64`
10+
- Runtimes: `All Linux`
11+
- Function code with container and non-container
12+
13+
## Compile package and dependencies
14+
15+
To run this example, you will need to ensure that your build architecture matches that of the Lambda execution environment by compiling with `GOOS=linux` and (`GOARCH=amd64` or `GOARCH=arm64`).
16+
17+
Building and saving package into a `bin` directory:
18+
```bash
19+
$ cd fluentd-lambda-extension/extensions
20+
$ GOOS=linux GOARCH=amd64 go build -o bin/fluentd-lambda-extension
21+
$ chmod +x bin/fluentd-lambda-extension
22+
```
23+
24+
## Layer Setup - by Terraform
25+
```hcl
26+
resource "aws_lambda_function" "example" {
27+
layers = [
28+
module.lambda_layer_extension.lambda_layer_arn,
29+
]
30+
31+
environment {
32+
variables = {
33+
FLUENTD_HOST = module.instance.private_ip[0]
34+
}
35+
}
36+
}
37+
```
38+
39+
## Layer Setup - by Manually
40+
### ZIP file manually setup
41+
The extensions .zip file should contain a root directory called `extensions/`, where the extension executables are located.
42+
43+
Creating zip package for the extension:
44+
```bash
45+
$ chmod +x extensions/bin/fluentd-lambda-extension
46+
$ cd extensions/bin && mkdir extensions
47+
$ mv fluentd-lambda-extension extensions
48+
$ zip -r extension.zip extensions
49+
```
50+
51+
Publish a new layer using the `extension.zip`. The output of the following command should provides you a layer arn.
52+
```bash
53+
aws lambda publish-layer-version \
54+
--layer-name "fluentd-lambda-extension" \
55+
--region <use your region> \
56+
--zip-file "fileb://extension.zip"
57+
```
58+
Note the LayerVersionArn that is produced in the output.
59+
e.g. `"LayerVersionArn": "arn:aws:lambda:<region>:123456789012:layer:fluentd-lambda-extension:1"`
60+
61+
Add the newly created layer version to a Lambda function.
62+
```bash
63+
aws lambda update-function-configuration --region <use your region> --function-name <your function name> --layers <LayerVersionArn from previous step>
64+
```
65+
66+
### Docker manually setup
67+
```
68+
FROM 123456789012.dkr.ecr.ap-southeast-1.amazonaws.com/fluentd-lambda-extension:v1 as extensions-layer
69+
COPY --from=extensions-layer /opt/extensions/fluentd-lambda-extension-{amd64, arm64} /opt/extensions/
70+
```
71+
72+
## Lambda Function Environment variables support
73+
- `FLUENTD_HOST`: fluentd host, which lambda will send log to, default: `localhost`
74+
- `FLUENTD_PORT`: fluent port, which lambda using to connect, default: `24224`
75+
- `FLUENTD_TAG_SUFFIX`: log suffix, default: `es.log`
76+
77+
Example: if `function-test.es.log` is fluentd tag key, then `es.log` is `FLUENTD_TAG_SUFFIX`
78+
79+
Sample log line:
80+
```sh
81+
2022-09-30 03:25:03.000000000 +0000 fluentd-lambda-extension-examples.es.log: {"function_name":"fluentd-lambda-extension-examples","msg":"{\"record\":\"Finished test extensions. Well done!\\n\",\"time\":\"2022-09-30T03:25:02.224Z\",\"type\":\"function\"}"}
82+
2022-09-30 03:25:03.000000000 +0000 fluentd-lambda-extension-examples.es.log: {"function_name":"fluentd-lambda-extension-examples","msg":"{\"record\":{\"requestId\":\"9df595ac-df7f-43b6-b97b-dcdfa6838fda\",\"status\":\"success\"},\"time\":\"2022-09-30T03:25:02.224Z\",\"type\":\"platform.runtimeDone\"}"}
83+
2022-09-30 03:30:51.000000000 +0000 fluentd-lambda-extension-examples.es.log: {"function_name":"fluentd-lambda-extension-examples","msg":"{\"record\":{\"requestId\":\"9df595ac-df7f-43b6-b97b-dcdfa6838fda\"},\"time\":\"2022-09-30T03:25:03.224Z\",\"type\":\"platform.end\"}"}
84+
2022-09-30 03:30:51.000000000 +0000 fluentd-lambda-extension-examples.es.log: {"function_name":"fluentd-lambda-extension-examples","msg":"{\"record\":{\"metrics\":{\"billedDurationMs\":1002,\"durationMs\":1001.22,\"maxMemoryUsedMB\":50,\"memorySizeMB\":2048},\"requestId\":\"9df595ac-df7f-43b6-b97b-dcdfa6838fda\"},\"time\":\"2022-09-30T03:25:03.224Z\",\"type\":\"platform.report\"}"}
85+
```
86+
87+
## Notes
88+
When deploying the Lambda function, be sure to include configure environment variables that the extension will leverage for communicating with the Fluentd endpoint(`FLUENTD_HOST`). This endpoint must be reachable from the Lambda function. This meant, `Lambda need to connect your VPC`.
89+
90+
## References
91+
92+
- https://github.com/aws-samples/aws-lambda-extensions
93+
- https://github.com/fluent/fluent-logger-golang/

examples/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# fluentd-lambda-extension Examples
2+
3+
We are using this place to have a quick test to verify everything changed is still working as expected. And we don't use SSH to connect to instance, we are using SSM Session Manager.
4+
5+
## Preparing
6+
7+
### AWS credentials
8+
Make sure you can access AWS resources by using AWS CLI
9+
10+
### Install AWS SSM plugin for CLI
11+
```
12+
https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-working-with-install-plugin.html
13+
```
14+
15+
## Terraform Command
16+
```hcl
17+
terraform plan
18+
terraform apply
19+
```
20+
21+
## Ansible Command
22+
23+
```
24+
ansible-playbook -i <INSTANCE_ID>, ansible/config.yml -v
25+
```

examples/ansible/config.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
- name: Setup fluentd, ES for testing
2+
hosts: all
3+
become: yes
4+
gather_facts: false
5+
vars:
6+
ansible_connection: aws_ssm
7+
ansible_aws_ssm_region: ap-southeast-1
8+
ansible_aws_ssm_bucket_name: sre-team-tfstate-dev
9+
tasks:
10+
- name: Create a directory if it does not exist
11+
ansible.builtin.file:
12+
path: /root/fluentd
13+
state: directory
14+
mode: '0755'
15+
16+
- name: Copy docker files to instance
17+
copy:
18+
src: "{{ playbook_dir }}/docker/{{ item }}"
19+
dest: "/root/{{ item }}"
20+
loop:
21+
- "docker-compose.yml"
22+
- "fluentd/Dockerfile"
23+
- "fluentd/entrypoint.sh"
24+
- "fluentd/fluent.conf"
25+
26+
- name: Up docker-compose
27+
ansible.builtin.shell: /usr/local/bin/docker-compose up -d
28+
args:
29+
chdir: /root
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
version: '2'
2+
services:
3+
fluentd:
4+
build: ./fluentd
5+
volumes:
6+
- ./fluentd:/fluentd/etc
7+
links:
8+
- "elasticsearch"
9+
ports:
10+
- "24224:24224"
11+
- "24224:24224/udp"
12+
13+
elasticsearch:
14+
image: docker.elastic.co/elasticsearch/elasticsearch:7.17.5
15+
volumes:
16+
- elasticsearch:/usr/share/elasticsearch/data
17+
environment:
18+
- "bootstrap.memory_lock=true"
19+
- "discovery.type=single-node"
20+
- "logger.level=INFO"
21+
- "xpack.security.enabled=true"
22+
- "ELASTIC_PASSWORD=test123456"
23+
ulimits:
24+
memlock:
25+
soft: -1
26+
hard: -1
27+
nofile:
28+
soft: 65536
29+
hard: 65536
30+
restart: always
31+
32+
kibana:
33+
image: kibana:7.17.5
34+
environment:
35+
- "ELASTICSEARCH_USERNAME=elastic"
36+
- "ELASTICSEARCH_PASSWORD=test123456"
37+
links:
38+
- "elasticsearch"
39+
ports:
40+
- "5601:5601"
41+
42+
volumes:
43+
elasticsearch:
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# fluentd/Dockerfile
2+
FROM fluent/fluentd:v1.13.0-debian-1.0
3+
# Use root account to use apt
4+
USER root
5+
6+
# below RUN includes plugin as examples elasticsearch is not required
7+
# you may customize including plugins as you wish
8+
RUN buildDeps="sudo make gcc g++ libc-dev" \
9+
&& apt-get update \
10+
&& apt-get install -y --no-install-recommends $buildDeps \
11+
&& sudo gem install elasticsearch -v 7.17.0 \
12+
&& sudo gem install fluent-plugin-elasticsearch \
13+
&& sudo gem sources --clear-all \
14+
&& SUDO_FORCE_REMOVE=yes \
15+
apt-get purge -y --auto-remove \
16+
-o APT::AutoRemove::RecommendsImportant=false \
17+
$buildDeps \
18+
&& rm -rf /var/lib/apt/lists/* \
19+
&& rm -rf /tmp/* /var/tmp/* /usr/lib/ruby/gems/*/cache/*.gem
20+
21+
#COPY fluent.conf /fluentd/etc/
22+
COPY entrypoint.sh /bin/
23+
RUN chmod +x /bin/entrypoint.sh
24+
25+
USER fluent
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/sh
2+
3+
#source vars if file exists
4+
DEFAULT=/etc/default/fluentd
5+
6+
if [ -r $DEFAULT ]; then
7+
set -o allexport
8+
. $DEFAULT
9+
set +o allexport
10+
fi
11+
12+
# If the user has supplied only arguments append them to `fluentd` command
13+
if [ "${1#-}" != "$1" ]; then
14+
set -- fluentd "$@"
15+
fi
16+
17+
# If user does not supply config file or plugins, use the default
18+
if [ "$1" = "fluentd" ]; then
19+
if ! echo $@ | grep -e ' \-c' -e ' \-\-config' ; then
20+
set -- "$@" --config /fluentd/etc/${FLUENTD_CONF}
21+
fi
22+
23+
if ! echo $@ | grep -e ' \-p' -e ' \-\-plugin' ; then
24+
set -- "$@" --plugin /fluentd/plugins
25+
fi
26+
fi
27+
28+
exec "$@"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# fluentd/conf/fluent.conf
2+
<source>
3+
@type forward
4+
port 24224
5+
bind 0.0.0.0
6+
</source>
7+
<match *.**>
8+
@type copy
9+
<store>
10+
@type elasticsearch
11+
host elasticsearch
12+
cloud_auth 'elastic:test123456'
13+
port 9200
14+
logstash_format true
15+
logstash_prefix fluentd
16+
logstash_dateformat %Y%m%d
17+
include_tag_key true
18+
type_name access_log
19+
tag_key @log_name
20+
flush_interval 1s
21+
</store>
22+
<store>
23+
@type stdout
24+
</store>
25+
</match>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import os
2+
import jsonpickle
3+
4+
5+
def lambda_handler(event, context):
6+
print('## ENVIRONMENT VARIABLES\r' +
7+
jsonpickle.encode(dict(**os.environ)))
8+
print('## EVENT\r' + jsonpickle.encode(event))
9+
print('## CONTEXT\r' + jsonpickle.encode(context))
10+
11+
print("Finished test extensions. Well done!")

examples/function/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
jsonpickle==1.3

0 commit comments

Comments
 (0)