amazon-web-servicesamazon-ec2cloud-init

what is incorrect with cloud-init script for ec2


I have written a basic loud init script, but when I pasted it in ec2 user-data section , nothing happened , I am trying to install docker on ec2 and run a script , later i will replace script with docker pull and docker run commands.

I can see my myScript is created in /var/lib/cloud/.. but other than that docker is not installed , neither my script is executed on initialization

Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0

--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"

#cloud-config
runcmd:
 - yum update -y
 - amazon-linux-extras install docker
 - service docker start
 - usermod -a -G docker ec2-user
 - chkconfig docker on
 - yum install cronie cronie-anacron

cloud_final_modules:
 - [scripts-user, always]
 - scripts-per-boot

write_files:
- path: /var/lib/cloud/scripts/per-boot/myScript.sh
  permissions: "0755"
  content: |
    #!/bin/bash
    echo "Hello World.  The time is now $(date -R)!" >> $HOME/out.txt
    
--//--

I can see following error :

[   21.351635] cloud-init[2073]: /var/lib/cloud/instance/scripts/runcmd: line 3: amazon-linux-extras: command not found
[   21.401571] cloud-init[2073]: Redirecting to /bin/systemctl start docker.service
[   21.413452] cloud-init[2073]: Failed to start docker.service: Unit docker.service not found.
[   21.435199] cloud-init[2073]: usermod: group 'docker' does not exist
[   21.461179] cloud-init[2073]: error reading information on service docker: No such file or directory
[   22.101289] cloud-init[2073]: Last metadata expiration check: 0:00:03 ago on Tue May  9 11:12:15 2023.
[   22.194901] cloud-init[2073]: Dependencies resolved.
[   22.211092] cloud-init[2073]: ================================================================================
[   22.216627] cloud-init[2073]:  Package            Arch       Version                    Repository       Size
[   22.241169] cloud-init[2073]: ================================================================================
[   22.247514] cloud-init[2073]: Installing:
[   22.250614] cloud-init[2073]:  cronie             x86_64     1.5.7-1.amzn2023.0.2       amazonlinux     115 k
[   22.301177] cloud-init[2073]:  cronie-anacron     x86_64     1.5.7-1.amzn2023.0.2       amazonlinux      32 k
[   22.308338] cloud-init[2073]: Transaction Summary
[   22.324286] cloud-init[2073]: ================================================================================
[   22.341349] cloud-init[2073]: Install  2 Packages
[   22.344540] cloud-init[2073]: Total download size: 147 k
[   22.353227] cloud-init[2073]: Installed size: 341 k

Solution

  • The error is very clear. The first line is telling you exactly what is wrong:

    line 3: amazon-linux-extras: command not found

    You are trying to use a command (amazon-linux-extras) which does not exist on the server.

    The amazon-linux-extras library was something for older Amazon Linux versions. The newer versions of Amazon Linux do not have an amazon-linux-extras library.

    To install Docker on newer versions, you simply need to run yum install -y docker instead of amazon-linux-extras install docker.