Creating an Image Pipeline with EC2 Image Builder

Credits Educative.io

Let’s create our own image that we can use to create EC2 instances with pre-installed Python and boto3. We will do this via EC2 Image Builder.

name: LinuxWithBoto3
schemaVersion: 1.0
phases:
  - name: build
    steps:
      - name: Boto3
        action: ExecuteBash
        inputs:
          commands:
            - 'sudo yum install python3 -y'
            - 'sudo yum install gcc openssl-devel bzip2-devel libffi-devel zlib-devel -y'
            - 'cd /opt'
            - 'sudo wget https://www.python.org/ftp/python/3.8.12/Python-3.8.12.tgz'
            - 'sudo tar xzf Python-3.8.12.tgz'
            - 'cd Python-3.8.12'
            - 'sudo ./configure --enable-optimizations'
            - 'sudo make altinstall'
            - 'pip3.8 install pip --upgrade'
            - 'pip3.8 install boto3'

We can now go to EC2, create instance, and select image that we created in a previous step. If we now connect to the instance we can see that Python, boto3 are pre-installed, which was the goal.

To add more packages, we can add them to the build component definition document (save it as a new version), and then rebuild the image.