Creating an Image Pipeline with EC2 Image Builder


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
  - name: build
      - name: Boto3
        action: ExecuteBash
            - 'sudo yum install python3 -y'
            - 'sudo yum install gcc openssl-devel bzip2-devel libffi-devel zlib-devel -y'
            - 'cd /opt'
            - 'sudo wget'
            - '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.