Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Send logs to CloudWatch

Michal Rutkaaabf4ff3668f
diff --git a/mvp/aws-cloudformation.template.yml b/mvp/aws-cloudformation.template.yml
index e2850e021..fc1a7684c 100644
--- a/mvp/aws-cloudformation.template.yml
+++ b/mvp/aws-cloudformation.template.yml
@@ -94,8 +94,172 @@ Resources:
# - !GetAtt EC2Instance.PublicIp
# TTL: '60'
# Type: AAAA
+ LogRole:
+ Type: AWS::IAM::Role
+ Properties:
+ AssumeRolePolicyDocument:
+ Version: '2012-10-17'
+ Statement:
+ - Effect: Allow
+ Principal:
+ Service:
+ - ec2.amazonaws.com
+ Action:
+ - sts:AssumeRole
+ Path: /
+ Policies:
+ - PolicyName: LogRolePolicy
+ PolicyDocument:
+ Version: '2012-10-17'
+ Statement:
+ - Effect: Allow
+ Action:
+ - logs:CreateLogGroup
+ - logs:CreateLogStream
+ - logs:PutLogEvents
+ - logs:DescribeLogStreams
+ Resource:
+ - !Join
+ - ''
+ - - 'arn:aws:'
+ - logs:*:*:*
+ LogRoleInstanceProfile:
+ Type: AWS::IAM::InstanceProfile
+ Properties:
+ Path: /
+ Roles:
+ - !Ref 'LogRole'
+ CloudFormationLogs:
+ Type: AWS::Logs::LogGroup
+ Properties:
+ RetentionInDays: 7
+
EC2Instance:
Type: AWS::EC2::Instance
+ Metadata:
+ AWS::CloudFormation::Init:
+ configSets:
+ install_all:
+ - install_cfn
+ - install_logs
+ install_cfn:
+ files:
+ /etc/cfn/cfn-hup.conf:
+ content: !Join
+ - ''
+ - - "[main]\n"
+ - stack=
+ - !Ref 'AWS::StackId'
+ - "\n"
+ - region=
+ - !Ref 'AWS::Region'
+ - "\n"
+ mode: '000400'
+ owner: root
+ group: root
+ /etc/cfn/hooks.d/cfn-auto-reloader.conf:
+ content: !Join
+ - ''
+ - - "[cfn-auto-reloader-hook]\n"
+ - "triggers=post.update\n"
+ - "path=Resources.WebServerInstance.Metadata.AWS::CloudFormation::Init\n"
+ - 'action=/opt/aws/bin/cfn-init -v '
+ - ' --stack '
+ - !Ref 'AWS::StackName'
+ - ' --resource EC2Instance '
+ - ' --configsets install_all '
+ - ' --region '
+ - !Ref 'AWS::Region'
+ - "\n"
+ - "runas=root\n"
+ mode: '000400'
+ owner: root
+ group: root
+ services:
+ sysvinit:
+ cfn-hup:
+ enabled: 'true'
+ ensureRunning: 'true'
+ files:
+ - /etc/cfn/cfn-hup.conf
+ - /etc/cfn/hooks.d/cfn-auto-reloader.conf
+ install_logs:
+ # packages:
+ # pip:
+ # awslogs: []
+ files:
+ /etc/awslogs/awslogs.conf:
+ content: !Join
+ - ''
+ - - "[general]\n"
+ - "state_file= /var/awslogs/state/agent-state\n"
+ - "[/var/log/cloud-init.log]\n"
+ - "file = /var/log/cloud-init.log\n"
+ - 'log_group_name = '
+ - !Ref 'CloudFormationLogs'
+ - "\n"
+ - "log_stream_name = {instance_id}/cloud-init.log\n"
+ - "datetime_format = \n"
+ - "[/var/log/cloud-init-output.log]\n"
+ - "file = /var/log/cloud-init-output.log\n"
+ - 'log_group_name = '
+ - !Ref 'CloudFormationLogs'
+ - "\n"
+ - "log_stream_name = {instance_id}/cloud-init-output.log\n"
+ - "datetime_format = \n"
+ - "[/var/log/cfn-init.log]\n"
+ - "file = /var/log/cfn-init.log\n"
+ - 'log_group_name = '
+ - !Ref 'CloudFormationLogs'
+ - "\n"
+ - "log_stream_name = {instance_id}/cfn-init.log\n"
+ - "datetime_format = \n"
+ - "[/var/log/cfn-hup.log]\n"
+ - "file = /var/log/cfn-hup.log\n"
+ - 'log_group_name = '
+ - !Ref 'CloudFormationLogs'
+ - "\n"
+ - "log_stream_name = {instance_id}/cfn-hup.log\n"
+ - "datetime_format = \n"
+ - "[/var/log/cfn-wire.log]\n"
+ - "file = /var/log/cfn-wire.log\n"
+ - 'log_group_name = '
+ - !Ref 'CloudFormationLogs'
+ - "\n"
+ - "log_stream_name = {instance_id}/cfn-wire.log\n"
+ - "datetime_format = \n"
+ - "[/var/log/docker]\n"
+ - "file = /var/log/docker.log\n"
+ - 'log_group_name = '
+ - !Ref 'CloudFormationLogs'
+ - "\n"
+ - "log_stream_name = {instance_id}/docker.log\n"
+ - "datetime_format = %d/%b/%Y:%H:%M:%S\n"
+ mode: '000444'
+ owner: root
+ group: root
+ /etc/awslogs/awscli.conf:
+ content: !Join
+ - ''
+ - - "[plugins]\n"
+ - "cwlogs = cwlogs\n"
+ - "[default]\n"
+ - 'region = '
+ - !Ref 'AWS::Region'
+ - "\n"
+ mode: '000444'
+ owner: root
+ group: root
+ commands:
+ '01_create_state_directory':
+ command: mkdir -p /var/awslogs/state
+ services:
+ sysvinit:
+ awslogs:
+ enabled: 'true'
+ ensureRunning: 'true'
+ files:
+ - /etc/awslogs/awslogs.conf
Properties:
InstanceType:
Ref: InstanceType
@@ -109,6 +273,7 @@ Resources:
Tags:
- Key: "Name"
Value: !Join [ ".", [ !Ref HostName, !Ref DomainName ] ]
+ IamInstanceProfile: !Ref 'LogRoleInstanceProfile'
UserData:
Fn::Base64: !Sub |
#!/bin/bash
@@ -123,6 +288,24 @@ Resources:
# Install openremote-cli tool
sudo apt install python3-pip -y
sudo pip3 install -U openremote-cli
+ # CFN tools for CloudWatch
+ pip install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-py3-latest.tar.gz
+ cp /usr/local/init/ubuntu/cfn-hup /etc/init.d/cfn-hup
+ chmod +x /etc/init.d/cfn-hup
+ update-rc.d cfn-hup defaults
+ service cfn-hup start
+ echo cfn-init -v --stack ${AWS::StackName} --resource EC2Instance --configsets install_all --region ${AWS::Region}
+ cfn-init -v --stack ${AWS::StackName} --resource EC2Instance --configsets install_all --region ${AWS::Region}
+ echo cfn-signal -e $? --stack ${AWS::StackName} --resource EC2Instance --region ${AWS::Region}
+ cfn-signal -e $? --stack ${AWS::StackName} --resource EC2Instance --region ${AWS::Region}
+ # end CFN
+ # awslogs
+ echo AWS Logs
+ curl https://s3.amazonaws.com/aws-cloudwatch/downloads/latest/awslogs-agent-setup.py -O
+ # Bump Python version (TODO AWS)
+ sed -i 's/3,6/3,9/g' awslogs-agent-setup.py
+ python3 awslogs-agent-setup.py -n -r ${AWS::Region} -c /etc/awslogs/awslogs.conf
+ # awslogs
cd /home/ubuntu
# Create volume for deployment (needed for SWARM due to concurency in compose-file)
# docker volume create openremote_deployment-data
@@ -155,6 +338,7 @@ Resources:
export EMAIL_PASSWORD=$(or configure_aws --secret ${AccessKeys.SecretAccessKey} --quiet)
export EMAIL_HOST=email-smtp.eu-west-1.amazonaws.com
docker-compose -f mvp-docker-compose.yml -p openremote up -d
+ docker-compose -f mvp-docker-compose.yml -p openremote logs --no-color -f &> /var/log/docker.log &
echo USER=${AccessKeys} > access.txt
echo SECRET=${AccessKeys.SecretAccessKey} >> access.txt
echo export EMAIL_PASSWORD=$(or configure_aws --secret ${AccessKeys.SecretAccessKey} --quiet) >> access.txt