Skip to content

armgit / core

Public Imported from GitHub
0

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

12.0 KB
Raw Blame
# ---------------------------------------------------------------------------------------------------------------------
#
# OpenRemote AWS Marketplace CloudFormation template
# This template will be used for deploying OpenRemote via the AWS Marketplace
#
# ---------------------------------------------------------------------------------------------------------------------

AWSTemplateFormatVersion: "2010-09-09"
Description: "Deploy OpenRemote via the AWS Marketplace"
Metadata:
  AWS::CloudFormation::Interface:
    ParameterGroups:
      - Label:
          default: Instance Configuration
        Parameters:
          - Host
          - InstanceType
          - ImageId
          - DiskSize
          - Keypair
          - ElasticIP
      - Label:
          default: OpenRemote Configuration (Optional)
        Parameters:
          - Password
          - RepeatPassword
      - Label:
          default: E-mail Configuration (Optional)
        Parameters:
          - SMTPHost
          - SMTPUser
          - SMTPPassword
          - SMTPFrom
    ParameterLabels:
      Host:
        default: Would you like to use your own domain name? If so, please enter it here.
      InstanceType:
        default: Which instance would you like to use?
      ImageId:
        default: Which AMI (Amazon Machine Image) do you want to use for this instance?
      DiskSize:
        default: How much storage would you like to allocate to the virtual machine?
      Keypair:
        default: Which key pair would you like to use for this instance?
      ElasticIP:
        default: Would you like to assign an Elastic IP?
      Password:
        default: Would you like to change the default administrator password?
      RepeatPassword:
        default: If changed the password, please repeat the password entered above.
      SMTPHost:
        default: Please enter the SMTP Hostname.
      SMTPUser:
        default: Please enter the SMTP Username.
      SMTPPassword:
        default: Please enter the SMTP Password.
      SMTPFrom:
        default: Please enter the e-mail address you would like to use for sending e-mails.
Parameters:
  Host:
    Description: Enter the Fully Qualified Domain Name (FQDN) e.g. example.com. Leave blank to use the IPv4 address.
    Type: String
  InstanceType:
    Description: Prices vary depending on the selected instance. All instances are using the ARM architecture.
    Type: String
    Default: t4g.medium
    AllowedValues:
      - t4g.small
      - t4g.medium
      - t4g.large
      - t4g.xlarge
      - m6g.large
      - m6g.xlarge
  ImageId:
    Description: Amazon AMI ID that will be used for this instance.
    Type: AWS::EC2::Image::Id
  DiskSize:
    Description: Choose the amount of storage (in GB)
    Type: String
    Default: 32GB
    AllowedValues:
      - 32GB
      - 48GB
      - 64GB
  Keypair:
    Description: With the selected key pair, you can SSH into the machine.
    Type: AWS::EC2::KeyPair::KeyName
    AllowedPattern: ".+"
    ConstraintDescription: You must select an key pair.
  ElasticIP:
    Description: Enabling this option ensures that your IPv4 address remains the same after stopping the virtual machine. Additional charges may apply.
    Type: String
    Default: "No"
    AllowedValues:
      - "Yes"
      - "No"
  Password:
    Description: The password you enter here will be used for the administrator account within the OpenRemote manager. Leave empty to use the default one.
    Type: String
    NoEcho: true
  RepeatPassword:
    Description: Repeat the password you have entered above.
    Type: String
    NoEcho: true
  SMTPHost:
    Description: The hostname you enter here will be used for sending e-mails e.g. mail.example.com.
    Type: String
  SMTPUser:
    Description: The username for authenticating with the SMTP server. In most cases this is the e-mail address of the sending account.
    Type: String
  SMTPPassword:
    Description: The password for authenticating with the SMTP server.
    Type: String
    NoEcho: true
  SMTPFrom:
    Description: The e-mail address is visible for the receivers. e.g. [email protected].
    Type: String

Mappings:
  DiskSize:
    32GB:
      DiskSize: 32
    48GB:
      DiskSize: 48
    64GB:
      DiskSize: 64

  ElasticIP:
    "Yes":
      ElasticIP: true
    "No":
      ElasticIP: false

Conditions:
  ElasticIP: !Equals [!Ref ElasticIP, "Yes"]

Rules:
  IsPasswordProvided:
    RuleCondition: !Not [!Equals [!Ref Password, .+]]
    Assertions:
      - Assert: !Equals
          - !Ref Password
          - !Ref RepeatPassword
        AssertDescription: The password and repeat password field must be the same.

Resources:
  VPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.0.0.0/16
      EnableDnsHostnames: true
      EnableDnsSupport: true
      InstanceTenancy: default
      Tags:
        - Key: Name
          Value: !Sub ${AWS::StackName}-or-vpc

  IPv6CIDRBlock:
    Type: AWS::EC2::VPCCidrBlock
    Properties:
      VpcId: !Ref VPC
      AmazonProvidedIpv6CidrBlock: true

  InternetGateway:
    Type: AWS::EC2::InternetGateway
    Properties:
      Tags:
        - Key: Name
          Value: !Sub ${AWS::StackName}-or-internet-gateway

  InternetGatewayAttachment:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      VpcId: !Ref VPC
      InternetGatewayId: !Ref InternetGateway

  PublicSubnet:
    Type: AWS::EC2::Subnet
    DependsOn: IPv6CIDRBlock
    Properties:
      VpcId: !Ref VPC
      AvailabilityZone: !Select [0, !GetAZs ""]
      CidrBlock: !Select [1, !Cidr [!GetAtt VPC.CidrBlock, 6, 8]]
      Ipv6CidrBlock: !Select [1, !Cidr [!Select [0, !GetAtt VPC.Ipv6CidrBlocks], 7, 64]]
      MapPublicIpOnLaunch: true
      Tags:
        - Key: Name
          Value: !Sub ${AWS::StackName}-or-public-subnet

  PublicRouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref VPC
      Tags:
        - Key: Name
          Value: !Sub ${AWS::StackName}-or-routetable

  PublicRouteIPv4:
    Type: AWS::EC2::Route
    DependsOn: InternetGatewayAttachment
    Properties:
      DestinationCidrBlock: 0.0.0.0/0
      GatewayId: !Ref InternetGateway
      RouteTableId: !Ref PublicRouteTable

  PublicRouteIPv6:
    Type: AWS::EC2::Route
    DependsOn: InternetGatewayAttachment
    Properties:
      RouteTableId: !Ref PublicRouteTable
      GatewayId: !Ref InternetGateway
      DestinationIpv6CidrBlock: ::/0

  PublicRouteTableAssociate:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      RouteTableId: !Ref PublicRouteTable
      SubnetId: !Ref PublicSubnet

  SecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Access OpenRemote Services
      GroupName: !Sub ${AWS::StackName}-or-securitygroup
      VpcId: !Ref VPC
      SecurityGroupIngress:
        - Description: HTTP Access IPv4
          IpProtocol: tcp
          FromPort: 80
          ToPort: 80
          CidrIp: 0.0.0.0/0
        - Description: HTTP Access IPv6
          IpProtocol: tcp
          FromPort: 80
          ToPort: 80
          CidrIpv6: ::/0
        - Description: HTTPS Access IPv4
          IpProtocol: tcp
          FromPort: 443
          ToPort: 443
          CidrIp: 0.0.0.0/0
        - Description: HTTPS Access IPv6
          IpProtocol: tcp
          FromPort: 443
          ToPort: 443
          CidrIpv6: ::/0
        - Description: MQTT Access IPv4
          IpProtocol: tcp
          FromPort: 8883
          ToPort: 8883
          CidrIp: 0.0.0.0/0
        - Description: MQTT Access IPv6
          IpProtocol: tcp
          FromPort: 8883
          ToPort: 8883
          CidrIpv6: ::/0
        - Description: SNMP Access IPv4
          IpProtocol: udp
          FromPort: 161
          ToPort: 162
          CidrIp: 0.0.0.0/0
        - Description: SNMP Access IPv6
          IpProtocol: udp
          FromPort: 161
          ToPort: 162
          CidrIpv6: ::/0

  SSHSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: OpenRemote SSH whitelist
      GroupName: !Sub ${AWS::StackName}-or-ssh-whitelist
      VpcId: !Ref VPC

  EC2Instance:
    Type: AWS::EC2::Instance
    CreationPolicy:
      ResourceSignal:
        Count: 1
        Timeout: PT10M
    Metadata:
      AWS::CloudFormation::Init:
        configSets:
          openremote:
            - docker_installation
            - openremote_installation
            - start_openremote

        docker_installation:
          packages:
            yum:
              docker: []
          commands:
            01_docker_compose_installation:
              command: |
                usermod -a -G docker ec2-user
                chkconfig docker on
                curl -sL 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
              test: docker-compose --version >/dev/null 2>&1; (( $? != 0 ))
          services:
            systemd:
              docker:
                enabled: true
                ensureRunning: true

        openremote_installation:
          commands:
            01_openremote_installation:
              command: |
                curl -sL https://raw.githubusercontent.com/openremote/core/refs/heads/main/docker-compose.yml -o /home/ec2-user/docker-compose.yml
              test: ls /home/ec2-user/docker-compose.yml >/dev/null 2>&1; (( $? != 0 ))

        start_openremote:
          commands:
            01_start_openremote:
              command: !Sub |

                # If hostname is not provided, use the IPv4 address of the EC2 instance, otherwise use the hostname.
                export OR_HOSTNAME=$( [ -z "${Host}" ] && curl -s v4.ident.me || echo "${Host}" )

                # Avoid parameter expansion if the password contains `$`
                if [ -n "${Password}" ]; then
                  export OR_ADMIN_PASSWORD=$(cat << 'EOF'
                ${Password}
                EOF
                )
                fi

                if [ -n "${SMTPPassword}" ]; then
                  export OR_EMAIL_PASSWORD=$(cat << 'EOF'
                ${SMTPPassword}
                EOF
                )
                fi

                # Configure SMTP credentials if provided.
                if [ -n "${SMTPHost}" ]; then
                  export OR_EMAIL_HOST="${SMTPHost}"
                fi
                if [ -n "${SMTPUser}" ]; then
                  export OR_EMAIL_USER="${SMTPUser}"
                fi
                if [ -n "${SMTPFrom}" ]; then
                  export OR_EMAIL_FROM="${SMTPFrom}"
                fi

                # Start OpenRemote using the Docker Compose file.
                docker-compose -f /home/ec2-user/docker-compose.yml -p or up -d

    Properties:
      ImageId: !Ref ImageId
      InstanceType: !Ref InstanceType
      KeyName: !Ref Keypair
      SubnetId: !Ref PublicSubnet
      SecurityGroupIds:
        - !Ref SecurityGroup
        - !Ref SSHSecurityGroup
      Ipv6AddressCount: 1
      Tags:
        - Key: Name
          Value: OpenRemote
      BlockDeviceMappings:
        - DeviceName: /dev/xvda
          Ebs:
            VolumeSize: !FindInMap [DiskSize, !Ref DiskSize, DiskSize]
            VolumeType: gp3
            DeleteOnTermination: true
      UserData:
        Fn::Base64:
          Fn::Sub: |
            #!/bin/bash -xe
            # Install CFN scripts.
            yum -y install aws-cfn-bootstrap

            # Execute OpenRemote configset.
            /opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --configsets openremote --region ${AWS::Region} --resource EC2Instance

            # Wait for signal that OpenRemote is successfully installed.
            /opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --region ${AWS::Region} --resource EC2Instance

  IPAddress:
    Type: AWS::EC2::EIP
    Condition: ElasticIP
    Properties:
      Tags:
        - Key: Name
          Value: !Sub ${AWS::StackName}-host

  ElasticIPAssoc:
    Type: AWS::EC2::EIPAssociation
    Condition: ElasticIP
    Properties:
      InstanceId: !Ref EC2Instance
      EIP: !Ref IPAddress

Outputs:
  PublicIP:
    Description: The Public IP of the OpenRemote instance
    Value: !GetAtt EC2Instance.PublicIp