100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Cleanup of AWS CloudFormation template(s) now have a single standard template (more can be added as required)
Rich Turnere07d7f08d85f
diff --git a/.ci_cd/aws/aws-cloudformation.template.arm64.yml b/.ci_cd/aws/aws-cloudformation.template.arm64.ymldeleted file mode 100644index b40768f43..000000000--- a/.ci_cd/aws/aws-cloudformation.template.arm64.yml+++ /dev/null@@ -1,155 +0,0 @@-----AWSTemplateFormatVersion: '2010-09-09'-Description: 'Template to install OpenRemote 3 stack on AWS environment on ubuntu'-Parameters:- OpenRemotePassword:- Description: Password for OpenRemote admin login- Type: String- Default: secret- DomainName:- Description: DNS domain for created stack (clear it is you want the deployment with an insecure SSL certificate)- Type: String- Default: developers.openremote.io- HostName:- Description: Hostname of the Openremote manager- Type: String- Default: demo-arm- HostedZone:- Description: Use AWS hosted zone- Type: String- Default: true- AllowedValues:- - true- - false- KeyName:- Description: Name of an existing EC2 KeyPair to enable SSH access to the instance- Type: AWS::EC2::KeyPair::KeyName- Default: openremote- ConstraintDescription: must be the name of an existing EC2 KeyPair.- InstanceType:- Description: EC2 instance type (don't change it unless you know what you are doing)- Type: String- # AWS Compute Optimizer reports that this instance type is under-provisioned and advise to use- # more than double the price instance. However, it works in our test setup.- Default: t4g.micro- ConstraintDescription: must be a valid EC2 instance type.- InstanceAMI:- Description: Managed AMI ID for EC2 Instance (don't change it unless you know what you are doing)- Type : String- Default: ami-03ec287fa560a6ccc-Conditions:- DnsRecordCreate: !Equals [!Ref HostedZone, true]- DomainName: !Not [ !Equals [!Ref DomainName, ""] ]-Outputs:- InstanceIP:- Description: The Instance public IP- Value: !GetAtt EC2Instance.PublicIp- PublicUrl:- Condition: DomainName- Description: OpenRemote Instance URL- Value: !Join [ ".", [ !Ref HostName, !Ref DomainName ] ]-Resources:- DNSRecord:- Condition: DnsRecordCreate- Type: AWS::Route53::RecordSet- Properties:- HostedZoneName: !Join [ "", [ !Ref DomainName, "." ] ]- Name: !Join [ ".", [!Ref HostName, !Ref DomainName ] ]- ResourceRecords:- - !GetAtt EC2Instance.PublicIp- TTL: '60'- Type: A- EC2Instance:- Type: AWS::EC2::Instance- Properties:- InstanceType:- Ref: InstanceType- KeyName:- Ref: KeyName- ImageId:- Ref: InstanceAMI- SecurityGroups:- - Ref: InstanceSecurityGroup- Tags:- - Key: "Name"- Value: "Openremote v3"- UserData:- Fn::Base64: !Sub |- #!/bin/bash- # Create swap file- sudo fallocate -l 1G /swapfile- sudo chmod 600 /swapfile- sudo mkswap /swapfile- sudo swapon /swapfile- echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab- sysctl vm.swappiness=10- sysctl vm.vfs_cache_pressure=50- echo vm.swappiness=10 | sudo tee -a /etc/sysctl.conf- echo vm.vfs_cache_pressure=50 | sudo tee -a /etc/sysctl.conf- free -h- cat /proc/sys/vm/swappiness- cat /proc/sys/vm/vfs_cache_pressure- sudo apt update- # Install docker- sudo groupadd docker- sudo gpasswd -a ubuntu docker- sudo apt install docker.io -y- sudo apt install docker-compose -y- # Install openremote-cli tool- sudo apt install python3-pip -y- sudo pip3 install -U openremote-cli- # Prepare deployment files- cd /home/ubuntu # we need this because on arm docker-compose cannot read /- # Create volume for deployment (needed for SWARM due to concurency in compose-file)- docker volume create openremote_deployment-data- docker run --rm -v openremote_deployment-data:/deployment openremote/deployment:latest- wget https://github.com/openremote/openremote/raw/master/mvp/mvp-docker-compose.yml- docker swarm init- if [ ${HostName} ] && [ ${DomainName} ]- then- echo export DOMAINNAME=${HostName}.${DomainName} >> deploy.sh- echo 'export EXTERNAL_URL=https://$DOMAINNAME' >> deploy.sh- elif [ ${DomainName} ]- then- echo export DOMAINNAME=${DomainName} >> deploy.sh- echo 'export EXTERNAL_URL=https://$DOMAINNAME' >> deploy.sh- elif [ ${HostName} ]- then- echo export DOMAINNAME=${HostName} >> deploy.sh- echo export 'EXTERNAL_URL=https://$(curl http://169.254.169.254/latest/meta-data/public-ipv4)' >> deploy.sh- else- echo export DOMAINNAME=localhost >> deploy.sh- echo export 'EXTERNAL_URL=https://$(curl http://169.254.169.254/latest/meta-data/public-ipv4)' >> deploy.sh- fi- echo export SETUP_ADMIN_PASSWORD=${OpenRemotePassword} >> deploy.sh- echo export [email protected] >> deploy.sh- # Running docker-compose before docker stack deploy has to do with AWS- # problems to fetch letsencrypt CERT in the swarm mode.- echo 'docker-compose -f mvp-docker-compose.yml -p openremote up -d' >> deploy.sh- #echo sleep 10 >> deploy.sh # give time to fetch CERT from letsencrypt- #echo 'docker-compose -f demo-basic-no-build.yml -p openremote down' >> deploy.sh- #echo openremote-cli deploy -v >> deploy.sh- chmod +x deploy.sh- chown --recursive 1000:1000 *- ./deploy.sh- InstanceSecurityGroup:- Type: AWS::EC2::SecurityGroup- Properties:- GroupDescription: SSH + HTTP + HTTPS- SecurityGroupIngress:- - IpProtocol: tcp- FromPort: '22'- ToPort: '22'- CidrIp: 0.0.0.0/0- - IpProtocol: tcp- FromPort: '80'- ToPort: '80'- CidrIp: 0.0.0.0/0- - IpProtocol: tcp- FromPort: '443'- ToPort: '443'- CidrIp: 0.0.0.0/0- - IpProtocol: icmp- FromPort: '8'- ToPort: '-1'- CidrIp: 0.0.0.0/0diff --git a/.ci_cd/aws/aws-cloudformation.template.yml b/.ci_cd/aws/aws-cloudformation.template.ymldeleted file mode 100644index f646de221..000000000--- a/.ci_cd/aws/aws-cloudformation.template.yml+++ /dev/null@@ -1,413 +0,0 @@-----AWSTemplateFormatVersion: '2010-09-09'-Description: 'Custom project OpenRemote 3 stack on AWS'-Parameters:- OpenRemotePassword:- Description: Password for OpenRemote admin login- Type: String- Default: secret- DomainName:- Description: DNS domain for created stack (clear it is you want the deployment with an insecure SSL certificate)- Type: String- Default: developers.openremote.io- HostName:- Description: Hostname of the Openremote manager- Type: String- Default: demo- HostedZone:- Description: Use AWS hosted zone- Type: String- Default: true- AllowedValues:- - true- - false- KeyName:- Description: Name of an existing EC2 KeyPair to enable SSH access to the instance- Type: AWS::EC2::KeyPair::KeyName- Default: openremote- ConstraintDescription: must be the name of an existing EC2 KeyPair.- InstanceType:- Description: EC2 instance type (don't change it unless you know what you are doing)- Type: String- # AWS Compute Optimizer reports that this instance type is under-provisioned and advise to use- # more than double the price instance. However, it works in our test setup.- Default: t4g.small- ConstraintDescription: must be a valid EC2 instance type.- InstanceAMI:- Description: Managed AMI ID for EC2 Instance (don't change it unless you know what you are doing)- Type : String- Default: ami-03ec287fa560a6ccc-Conditions:- DnsRecordCreate: !Equals [!Ref HostedZone, true]- DomainName: !Not [ !Equals [!Ref DomainName, ""] ]-Outputs:- InstanceIP:- Description: The Instance public IP- Value: !GetAtt EC2Instance.PublicIp- PublicUrl:- Condition: DnsRecordCreate- Description: OpenRemote Instance URL- Value: !Join [ ".", [ !Ref HostName, !Ref DomainName ] ]- UserId:- Description: SMTP server username- Value: !Ref AccessKeys- UserSecret:- Description: SMTP server password- Value: !GetAtt AccessKeys.SecretAccessKey- SmtpServer:- Value: !Join [ ".", [ "email-smtp", !Ref "AWS::Region", "amazonaws.com"]]-Resources:- EmailUser:- Type: AWS::IAM::User- Properties:- Policies:- - PolicyName: OpenRemoteSendEmail- PolicyDocument:- Version: '2012-10-17'- Statement:- - Effect: Allow- Action:- - ses:SendRawEmail- Resource:- - '*'- AccessKeys:- Type: AWS::IAM::AccessKey- Properties:- UserName: !Ref EmailUser- DNSRecord:- Condition: DnsRecordCreate- Type: AWS::Route53::RecordSet- Properties:- HostedZoneName: !Join [ "", [ !Ref DomainName, "." ] ]- Name: !Join [ ".", [!Ref HostName, !Ref DomainName ] ]- ResourceRecords:- - !GetAtt EC2Instance.PublicIp- TTL: '60'- Type: A- 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:*:*:*- - PolicyName: ECREFSPolicy- PolicyDocument:- Version: '2012-10-17'- Statement:- - Effect: Allow- Action:- - ecr:*- - elasticfilesystem:*- Resource:- - '*'- 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:- 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"- 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- KeyName:- Ref: KeyName- ImageId:- Ref: InstanceAMI- SecurityGroups:- - Ref: InstanceSecurityGroup- Ipv6AddressCount: 1- Tags:- - Key: "Name"- Value: !Join [ ".", [ !Ref HostName, !Ref DomainName ] ]- IamInstanceProfile: !Ref 'LogRoleInstanceProfile'- UserData:- Fn::Base64: !Sub |- #!/bin/bash- apt update- # Install docker newest version- groupadd docker- gpasswd -a ubuntu docker- apt-get update- apt-get install apt-transport-https ca-certificates curl gnupg lsb-release -y- curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg- echo "deb [arch=arm64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null- apt-get update- apt-get install docker-ce docker-ce-cli containerd.io -y- apt install docker-compose -y- # SNMP tools- apt install snmp snmpd snmptt snmptrapd -y- # Install openremote-cli tool- apt install python3-pip -y- pip3 install -U openremote-cli- # Install AWS CLI- apt install awscli -y- # 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- # AWS EFS for map- apt-get -y install git binutils- git clone https://github.com/aws/efs-utils- cd efs-utils/- ./build-deb.sh- apt-get -y install ./build/amazon-efs-utils*deb- mkdir /deployment.local- mount -t efs -o tls fs-0b1f2c29945f8db97:/ /deployment.local- cd ..- # AWS EFS for map- docker swarm init- openremote-cli -V -t- # create environment- export DEPLOYMENT_VERSION=latest- export MANAGER_VERSION=latest- echo export AWS_DEFAULT_REGION=${AWS::Region} > environment.txt- echo export EMAIL_PASSWORD=$(openremote-cli configure_aws --secret ${AccessKeys.SecretAccessKey} --quiet) >> environment.txt- echo export EMAIL_USER=${AccessKeys} >> environment.txt- echo export EMAIL_HOST=email-smtp.${AWS::Region}.amazonaws.com >> environment.txt- echo export AWSLOGS_GROUP=${CloudFormationLogs} >> environment.txt- echo export [email protected] >> environment.txt- echo export DEPLOYMENT_HOST=${HostName}.${DomainName} >> environment.txt- echo export PASSWORD=${OpenRemotePassword} >> environment.txt- echo export [email protected]- source environment.txt- # docker ECR login- `aws ecr get-login --no-include-email`- # 90 days personal access token (repo) Expires on Tue, Jan 11 2022- curl https://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@raw.githubusercontent.com/openremote/k2/main/aws-docker-compose.yml --output docker-compose.yml- docker-compose -p custom up -d- chown ubuntu:ubuntu -R .-- InstanceSecurityGroup:- Type: AWS::EC2::SecurityGroup- Properties:- GroupDescription: SSH + HTTP + HTTPS + MQTT + ping + NFS- SecurityGroupIngress:- - IpProtocol: tcp- FromPort: '22'- ToPort: '22'- CidrIp: 0.0.0.0/0- Description: SSH- - IpProtocol: tcp- FromPort: '80'- ToPort: '80'- CidrIp: 0.0.0.0/0- Description: HTTP- - IpProtocol: tcp- FromPort: '443'- ToPort: '443'- CidrIp: 0.0.0.0/0- Description: HTTPS- - IpProtocol: tcp- FromPort: '8883'- ToPort: '8883'- CidrIp: 0.0.0.0/0- Description: MQTT- - IpProtocol: udp- FromPort: '161'- ToPort: '162'- CidrIp: 0.0.0.0/0- Description: SNMP- - IpProtocol: icmp- FromPort: '8'- ToPort: '-1'- CidrIp: 0.0.0.0/0- Description: Ping- - IpProtocol: tcp- FromPort: '2049'- ToPort: '2049'- CidrIp: 0.0.0.0/0- Description: NFS- - IpProtocol: tcp- FromPort: '2049'- ToPort: '2049'- CidrIpv6: ::/0- Description: NFS- - IpProtocol: tcp- FromPort: '22'- ToPort: '22'- CidrIpv6: ::/0- Description: SSH- - IpProtocol: tcp- FromPort: '80'- ToPort: '80'- CidrIpv6: ::/0- Description: HTTP- - IpProtocol: tcp- FromPort: '443'- ToPort: '443'- CidrIpv6: ::/0- Description: HTTPS- - IpProtocol: tcp- FromPort: '8883'- ToPort: '8883'- CidrIpv6: ::/0- Description: MQTT- - IpProtocol: udp- FromPort: '161'- ToPort: '162'- CidrIpv6: ::/0- Description: SNMP- - IpProtocol: icmpv6- FromPort: '-1'- ToPort: '-1'- CidrIpv6: ::/0- Description: Pingdiff --git a/.ci_cd/aws/cloudformation-standard.yml b/.ci_cd/aws/cloudformation-standard.ymlnew file mode 100644index 000000000..13cb5ccd8--- /dev/null+++ b/.ci_cd/aws/cloudformation-standard.yml@@ -0,0 +1,402 @@+# ---------------------------------------------------------------------------------------------+# Standard OpenRemote CloudFormation template for provisioning the following AWS resources (not+# in order) for running an instance of the OpenRemote stack:+#+# - Provision EC2 instance using Amazon Linux 2 ARM image with standard security group assignments+# - Add IAM user with SES email permissions+# - Add Route 53 DNS record to hosted zone 'mvp.openremote.io' that points to new EC2 instance+# - Configures logging for the EC2 instance+# - Installs docker and docker-compose+# - Optionally adds EFS data mount on /deployment.local+#+# The generated SMTP IAM credentials are output to /etc/environment using the standard OpenRemote+# naming convention SETUP_EMAIL_HOST, SETUP_EMAIL_USER and SETUP_EMAIL_PASSWORD+#+# ---------------------------------------------------------------------------------------------++AWSTemplateFormatVersion: '2010-09-09'+Description: 'AWS Linux 2 with docker compose'+Parameters:+ Host:+ Description: Hostname+ Type: String+ Default: demo+ AllowedPattern: "[a-z0-9]+"+ ConstraintDescription: Host name must be lowercase letters and numbers+ InstanceType:+ Description: EC2 instance type (don't change it unless you know what you are doing)+ Type: String+ # AWS Compute Optimizer reports that this instance type is under-provisioned and advise+ # more than double the price instance. As the requirements vary massively depending on the actual project+ # then we need to be flexible here and bear in mind use case and acceptable cost.+ Default: t4g.small+ AllowedValues:+ - t4g.small+ - t4g.medium+ - t4g.large+ - m6g.medium+ - m6g.large+ ConstraintDescription: must be a valid EC2 instance type.+ MapFileSystemId:+ Description: File system ID of map to mount from EFS (must already exist)+ Type: String+ AllowedPattern: ^$|^(fs-[0-9a-f]{8,40})?$+ ConstraintDescription: Must be a valid file system ID e.g. (fs-ad2321321332132123)++Conditions:+ DoMapMount: !Not [!Equals [!Ref MapFileSystemId, ""]]++Outputs:+ InstanceIP:+ Description: The Instance public IP+ Value: !GetAtt EC2Instance.PublicIp+ Domain:+ Description: OpenRemote Instance URL+ Value: !Join [".", [!Ref Host, "mvp.openremote.io"]]+ SMTPUsername:+ Description: SMTP server username+ Value: !Ref SMTPCredentials+ SMTPSecret:+ Description: SMTP server password+ Value: !GetAtt SMTPCredentials.SecretAccessKey+ SMTPServer:+ Value: !Join [".", ["email-smtp", !Ref "AWS::Region", "amazonaws.com"]]++Resources:+ SMTPUser:+ Type: AWS::IAM::User+ Properties:+ UserName: !Join ["-", ["smtp", !Ref Host ]]+ Policies:+ - PolicyName: OpenRemoteSendEmail+ PolicyDocument:+ Version: '2012-10-17'+ Statement:+ - Effect: Allow+ Action:+ - ses:SendRawEmail+ Resource:+ - '*'++ SMTPCredentials:+ Type: AWS::IAM::AccessKey+ Properties:+ UserName: !Ref SMTPUser++ DNSRecord:+ Type: AWS::Route53::RecordSet+ Properties:+ HostedZoneName: "mvp.openremote.io."+ Name: !Join [ ".", [!Ref Host, "mvp.openremote.io" ] ]+ ResourceRecords:+ - !GetAtt EC2Instance.PublicIp+ TTL: '60'+ Type: A++ 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:Create*+ - logs:PutLogEvents+ - s3:GetObject+ Resource:+ - arn:aws:logs:*:*:*+ - arn:aws:s3:::*++ 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:+ default:+ - docker_setup+ - cron_setup+ - cfn_hup+ - env_vars+ withmap:+ - ConfigSet: default+ - nfs_setup+ - mount++ nfs_setup:+ packages:+ yum:+ nfs-utils: []+ files:+ "/home/ec2-user/post_nfsstat":+ content: !Sub |+ #!/bin/bash++ INPUT="$(cat)"+ CW_JSON_OPEN='{ "Namespace": "EFS", "MetricData": [ '+ CW_JSON_CLOSE=' ] }'+ CW_JSON_METRIC=''+ METRIC_COUNTER=0++ for COL in 1 2 3 4 5 6; do++ COUNTER=0+ METRIC_FIELD=$COL+ DATA_FIELD=$(($COL+($COL-1)))++ while read line; do+ if [[ COUNTER -gt 0 ]]; then++ LINE=`echo $line | tr -s ' ' `+ AWS_COMMAND="aws cloudwatch put-metric-data --region ${AWS::Region}"+ MOD=$(( $COUNTER % 2))++ if [ $MOD -eq 1 ]; then+ METRIC_NAME=`echo $LINE | cut -d ' ' -f $METRIC_FIELD`+ else+ METRIC_VALUE=`echo $LINE | cut -d ' ' -f $DATA_FIELD`+ fi++ if [[ -n "$METRIC_NAME" && -n "$METRIC_VALUE" ]]; then+ INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)+ CW_JSON_METRIC="$CW_JSON_METRIC { \"MetricName\": \"$METRIC_NAME\", \"Dimensions\": [{\"Name\": \"InstanceId\", \"Value\": \"$INSTANCE_ID\"} ], \"Value\": $METRIC_VALUE },"+ unset METRIC_NAME+ unset METRIC_VALUE++ METRIC_COUNTER=$((METRIC_COUNTER+1))+ if [ $METRIC_COUNTER -eq 20 ]; then+ # 20 is max metric collection size, so we have to submit here+ aws cloudwatch put-metric-data --region ${AWS::Region} --cli-input-json "`echo $CW_JSON_OPEN ${!CW_JSON_METRIC%?} $CW_JSON_CLOSE`"++ # reset+ METRIC_COUNTER=0+ CW_JSON_METRIC=''+ fi+ fi++ COUNTER=$((COUNTER+1))+ fi++ if [[ "$line" == "Client nfs v4:" ]]; then+ # the next line is the good stuff+ COUNTER=$((COUNTER+1))+ fi+ done <<< "$INPUT"+ done++ # submit whatever is left+ aws cloudwatch put-metric-data --region ${AWS::Region} --cli-input-json "`echo $CW_JSON_OPEN ${!CW_JSON_METRIC%?} $CW_JSON_CLOSE`"+ mode: '000755'+ owner: ec2-user+ group: ec2-user+ "/home/ec2-user/crontab":+ content: "* * * * * /usr/sbin/nfsstat | /home/ec2-user/post_nfsstat\n"+ owner: ec2-user+ group: ec2-user++ mount:+ commands:+ 01_createdir:+ command: !Sub "mkdir /deployment.local"+ 02_mount:+ command: !Sub >+ echo "${MapFileSystemId}.efs.${AWS::Region}.amazonaws.com:/ /deployment.local nfs4 nfsvers=4.1 0 0" >> /etc/fstab+ mount -a+ 03_permissions:+ command: chown ec2-user:ec2-user /deployment.local++ env_vars:+ files:+ "/home/ec2-user/smtp_credentials_generate.py":+ content: |+ #!/usr/bin/env python3++ import hmac+ import hashlib+ import base64+ import argparse++ SMTP_REGIONS = [+ 'us-east-2', # US East (Ohio)+ 'us-east-1', # US East (N. Virginia)+ 'us-west-2', # US West (Oregon)+ 'ap-south-1', # Asia Pacific (Mumbai)+ 'ap-northeast-2', # Asia Pacific (Seoul)+ 'ap-southeast-1', # Asia Pacific (Singapore)+ 'ap-southeast-2', # Asia Pacific (Sydney)+ 'ap-northeast-1', # Asia Pacific (Tokyo)+ 'ca-central-1', # Canada (Central)+ 'eu-central-1', # Europe (Frankfurt)+ 'eu-west-1', # Europe (Ireland)+ 'eu-west-2', # Europe (London)+ 'sa-east-1', # South America (Sao Paulo)+ 'us-gov-west-1', # AWS GovCloud (US)+ ]++ # These values are required to calculate the signature. Do not change them.+ DATE = "11111111"+ SERVICE = "ses"+ MESSAGE = "SendRawEmail"+ TERMINAL = "aws4_request"+ VERSION = 0x04+++ def sign(key, msg):+ return hmac.new(key, msg.encode('utf-8'), hashlib.sha256).digest()+++ def calculate_key(secret_access_key, region):+ if region not in SMTP_REGIONS:+ raise ValueError(f"The {region} Region doesn't have an SMTP endpoint.")++ signature = sign(("AWS4" + secret_access_key).encode('utf-8'), DATE)+ signature = sign(signature, region)+ signature = sign(signature, SERVICE)+ signature = sign(signature, TERMINAL)+ signature = sign(signature, MESSAGE)+ signature_and_version = bytes([VERSION]) + signature+ smtp_password = base64.b64encode(signature_and_version)+ return smtp_password.decode('utf-8')+++ def main():+ parser = argparse.ArgumentParser(+ description='Convert a Secret Access Key for an IAM user to an SMTP password.')+ parser.add_argument(+ 'secret', help='The Secret Access Key to convert.')+ parser.add_argument(+ 'region',+ help='The AWS Region where the SMTP password will be used.',+ choices=SMTP_REGIONS)+ args = parser.parse_args()+ print(calculate_key(args.secret, args.region))+++ if __name__ == '__main__':+ main()+ owner: ec2-user+ group: ec2-user+ commands:+ 01_output_smtp_credentials:+ command: !Sub |+ PASS=$(python3 /home/ec2-user/smtp_credentials_generate.py ${SMTPCredentials.SecretAccessKey} ${AWS::Region})+ echo "SETUP_EMAIL_HOST=email-smtp.${AWS::Region}.amazonaws.com" >> /etc/environment+ echo "SETUP_EMAIL_USER=${SMTPCredentials}" >> /etc/environment+ echo "SETUP_EMAIL_PASSWORD=$PASS" >> /etc/environment++ docker_setup:+ packages:+ yum:+ docker: []+ commands:+ 01_configure:+ command: |+ usermod -a -G docker ec2-user+ chkconfig docker on+ curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o /usr/bin/docker-compose && chmod +x /usr/bin/docker-compose && docker-compose --version+ services:+ sysvinit:+ docker:+ enabled: "true"+ ensureRunning: "true"++ cron_setup:+ packages:+ yum:+ yum-cron: []+ commands:+ 01_configure:+ command: chkconfig yum-cron on+ services:+ sysvinit:+ yum-cron:+ enabled: "true"+ ensureRunning: "true"++ cfn_hup:+ files:+ /etc/cfn/hooks.d/cfn-auto-reloader.conf:+ content: !Sub |+ [cfn-auto-reloader-hook]+ triggers=post.update+ path=Resources.LaunchConfig.Metadata.AWS::CloudFormation::Init+ action=/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource EC2Instance --region ${AWS::Region}+ runas=root+ mode: "000400"+ owner: "root"+ group: "root"++ Properties:+ InstanceType:+ Ref: InstanceType+ KeyName:+ "openremote"+ ImageId:+ "ami-032afb7a6d14ab941"+ SecurityGroups:+ - "ssh-access"+ - "http-access"+ - "mqtt-access"+ - "ping-access"+ - "snmp-access"+ Ipv6AddressCount: 1+ Tags:+ - Key: "Name"+ Value: !Join [ ".", [ !Ref Host, "mvp.openremote.io" ] ]+ IamInstanceProfile: !Ref 'LogRoleInstanceProfile'+ UserData:+ Fn::Base64: !Sub |+ #!/bin/bash -xe+ # Install latest cfn scripts+ yum -y install aws-cfn-bootstrap+ #sed -i '/update_cmd/s/= .*/= security/' /etc/yum/yum-cron-hourly.conf+ #sed -i '/update_messages/s/= .*/= no/' /etc/yum/yum-cron-hourly.conf+ #sed -i '/apply_updates/s/= .*/= yes/' /etc/yum/yum-cron-hourly.conf+ #sed -i '/download_updates/s/= .*/= yes/' /etc/yum/yum-cron-hourly.conf+ #sed -i '/download_updates/s/= .*/= no/' /etc/yum/yum-cron.conf++ # Execute required config set+ CONFIGSET=default+ if [ ! -z "${MapFileSystemId}" ]; then+ CONFIGSET=withmap+ fi+ /opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource EC2Instance --configsets $CONFIGSET --region ${AWS::Region}++ # Start up the cfn-hup daemon to listen for changes to the EC2 instance metadata+ /opt/aws/bin/cfn-hup || error_exit 'Failed to start cfn-hup'+ # Get the CloudWatch Logs agent+ wget https://s3.amazonaws.com/aws-cloudwatch/downloads/latest/awslogs-agent-setup.py+ # Install the CloudWatch Logs agent+ python awslogs-agent-setup.py -n -r ${AWS::Region} -c /tmp/cwlogs/apacheaccess.conf || error_exit 'Failed to run CloudWatch Logs agent setup'++ # Add NFS/EFS stat cron job+ crontab /home/ec2-user/crontab++ # Swap file necessary?++ # All done so signal success+ /opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackId} --resource EC2Instance --region ${AWS::Region}