Install Docker via CloudFormation

On Ubuntu

  EC2Server:
    Type: AWS::EC2::Instance
    Metadata:
      Comment1: Configure the bootstrap helpers
      AWS::CloudFormation::Init:
        configSets:
          default:
            - "04_install_docker"
            
		04_install_docker:
          commands:
            a_get_certificates:
              command: 'sudo apt-get install -y apt-transport-https ca-certificates'
            b_set_gpg_key:
              command: 'curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg'
            c_add_docker_repo:
              command: 'echo "deb [arch=arm64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list'
            d_update_aptget:
              command: 'sudo apt-get update'
            e_install_docker:
              command: !Sub 'sudo apt-get install -y docker-ce docker-ce-cli containerd.io'
            f_create_service:
              command: 'sudo service docker start'
            g_add_ubuntu_user_to_docker_group:
              command: 'sudo usermod -aG docker ubuntu'
            h_verify_installation:
              command: 'docker run hello-world'
              
              
              
              
      UserData:
        Fn::Base64:
          !Sub |
            #!/bin/bash -xe
            apt-get update
            apt-get install -y python-setuptools
            apt-get install -y net-tools
            mkdir -p /opt/aws/bin
            apt-get install -y wget
            wget https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-py3-latest.tar.gz
            python3 -m easy_install --script-dir /opt/aws/bin aws-cfn-bootstrap-py3-latest.tar.gz

            wget https://s3.amazonaws.com/amazoncloudwatch-agent/ubuntu/arm64/latest/amazon-cloudwatch-agent.deb -O /tmp/amazon-cloudwatch-agent.deb
            dpkg -i /tmp/amazon-cloudwatch-agent.deb
            /opt/aws/bin/cfn-init -v --stack ${AWS::StackId} --resource EC2Server --region ${AWS::Region} --configsets default
            /opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackId} --resource EC2Server --region ${AWS::Region}