DEV Community

Shoeb Ahmed
Shoeb Ahmed

Posted on

Run a Python code on AWS Batch Part — 1: Creation of Python Script and Docker Container.

Run a Python code on AWS Batch Part — 1: Creation of Python Script and Docker Container.

A prerequisite for running python code on AWS Batch is that you already have Python and Docker installed on your system. No matter whether you know Docker or not, our goal is to run the python code in AWS Batch.

We will start with running the basic python code and that will print messages.

Steps are given below:

  1. Create a python script.

  2. Create a Docker File.

  3. Create Docker Container.

  4. Run Docker Container.

First, start writing the python script.


The output of the above code looks like the one given below here.

The next step is to create a Docker File. In that docker file, we need to insert a particular command which I had given below here. Make sure that the file we are creating is the name should be “Dockerfile”. *And the python script which I had made the filename I have taken is *“myfunction.py”.

The command for Docker is given below:


If you have external packages just like I have taken pandas, same way you can also add the command line which is mentioned on line number 5 in the above code.

RUN pip install pandas numpy more-packages-you-can-add-with-whitespace-seperated

Till now we had created a Docker file and Python script, our next goal is to create a Docker Container.

Before creating we need to set up our AWS CLI because we need to upload the Docker file and Python script on the AWS platform.

Make sure you had already installed AWS CLI from these links and click on that links: https://awscli.amazonaws.com/AWSCLIV2.msi

Now we will configure the AWS CLI by running a certain command.

Make sure you have an AWS ACCESS KEY ID and AWS SECRET KEY by visiting the Security Credential page.

If you want to generate that key please visit my old article: https://medium.com/codex/aws-s3-with-python-by-shoeb-ahmed-923c1cf47110

After getting the key let’s jump onto the AWS CLI and Open the command prompt.

  1. Open CMD

  2. Type “aws” and it will show some message as shown below that means AWS CLI is installed on your system:

  1. Now type “aws configure” on the command prompt:

  • Enter your access key and press Enter button.

  • Enter your secret key and press Enter button.

  • After that, it will ask for the region and default output format we need to keep as it is blank and presses Enter.

Once AWS CLI is done. Now we move to the creation of a container.

Make sure the docker file and python file are in the same locations.

I open cmd on that location and type:

docker builds -t sample-aws-code .

Please give whitespace and dot after “sample-aws-code”.

In the above screenshot, we saw that our Image file is built.

We will test that image file in the local system by running the command:

docker run sample-aws-code

So this is the output of the Sample code.

I will publish part 2 which consists of AWS Batch setup and run.

Top comments (0)