amazon-ec2terraformaws-session-managerec2-userdata

Terraform - EC2 user_data and Sessions Manager (ssm-user)


I am creating an EC2 instance on AWS with Terraform that contains the user_data parameter to install docker and assign it to the user ssm-user.

I don't want to enable the ssh connection (22) and I have only enabled access via Session Manager in System Manager.

This is the shell script:

#!/bin/bash
set -ex
sudo yum update -y
sudo amazon-linux-extras install docker -y
sudo usermod -a -G docker ssm-user
sudo chown ssm-user:ssm-user /home/ssm-user/.docker -R
sudo chmod g+rwx "/home/ssm-user/.docker" -R
sudo chown ssm-user:docker /var/run/docker.sock
sudo systemctl enable docker
sudo systemctl start docker

However, when I access the EC2 instance via Session Manager and run the docker ps command I get the following error:

Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

Why is this happening as if nothing is being executed?


Solution

  • You need to look at /var/log/cloud-init-output.log. it captures console output and probably has an error in your script. See more here.

    It probably says that user 'ssm-user' does not exist

    By the way - you don't need sudo in user data. The commands are run as root