Cloudformation for installing Postgresql and Docker

AWS PostgreSQL

Click this link to download script  docker-postgresql

Parameters: 
  InstanceType: 
    Type: String
    Default: t2.micro
    Description: Enter t2.micro, m1.small, or m1.large. Default is t2.micro.
  KeyPairName: 
    Description: Amazon EC2 Key Pair
    Type: "AWS::EC2::KeyPair::KeyName"
  ImageId: 
    Type: "AWS::EC2::Image::Id"
    Default: ami-81cefcfd
Resources:
  AppNode:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: !Ref InstanceType
      ImageId: !Ref ImageId
      KeyName: !Ref KeyPairName
      SecurityGroups:
        - !Ref AppNodeSG
      UserData: !Base64 |
        #!/bin/bash
        sudo su
        
        echo "installing docker...."
        apt-get install apt-transport-https ca-certificates curl software-properties-common
        curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
        add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable"
        apt-get update
        apt-cache policy docker-ce
        apt-get install -y docker-ce
        
        echo "enabel remote api...."
        if [ ! -d "/etc/systemd/system/docker.service.d" ]; then
          mkdir /etc/systemd/system/docker.service.d
        fi
        printf "# /etc/systemd/system/docker.service.d/override.conf\n[Service]\nExecStart=\nExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:2376\n" > /etc/systemd/system/docker.service.d/startup_options.conf
        systemctl daemon-reload
        systemctl restart docker.service
        systemctl enable docker

        
        echo "Installing postgresql 9.6"
        add-apt-repository "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -sc)-pgdg main"
        wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
        apt-get update
        apt-get install postgresql-9.6 -y
        #-u postgres psql
        #change password postgres to underadmin
        #ALTER USER postgres PASSWORD 'underadmin';
        #\q
        sed -i -e 's#127.0.0.1/32#0.0.0.0/0#g' /etc/postgresql/9.6/main/pg_hba.conf
        sed -i -e "s/#listen_addresses = 'localhost'/listen_addresses = '*'/g" /etc/postgresql/9.6/main/postgresql.conf

  AppNodeSG:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: for the app nodes that allow ssh, http
      SecurityGroupIngress:
      - 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: tcp
        FromPort: '22'
        ToPort: '22'
        CidrIp: 0.0.0.0/0
      - IpProtocol: tcp
        FromPort: '5432'
        ToPort: '5432'
        CidrIp: 0.0.0.0/0
      - IpProtocol: tcp
        FromPort: '2376'
        ToPort: '2376'
        CidrIp: 0.0.0.0/0  

 

Leave a Reply

Your email address will not be published. Required fields are marked *