Run a Python code on AWS Batch Part — 1: Creation of Python Script and Docker Container.
Published: 2022-07-19 07:24:53


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

![](https://cdn-images-1.medium.com/max/2000/1*ww8k8sMq4_FexQaFg34H5Q.png)

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.**
{% gist https://gist.github.com/shoeb370/7171b8e78fdd5c86fd219dc0a58a5475.js %}
The output of the above code looks like the one given below here.

![](https://cdn-images-1.medium.com/max/2000/1*cI6Og_8f_ja2TProZ8m4JQ.png)

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:
{% gist https://gist.github.com/shoeb370/bc5ef537f4d54ea0a4385ace0aabdcfd.js %}
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](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](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:

![](https://cdn-images-1.medium.com/max/2000/1*LNr9hIfMXE3j0Mj7JmxbGA.png)

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

![](https://cdn-images-1.medium.com/max/2000/1*2ylbn1wpvaWyiYHnfPNEnw.png)

* 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.

![](https://cdn-images-1.medium.com/max/2000/1*WrLdJ10cS7xV2DApyguOFQ.png)

I open cmd on that location and type:
>  **docker builds -t sample-aws-code .**

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

![](https://cdn-images-1.medium.com/max/2000/1*XFxciCKAgMwGkUnwC5zffQ.png)

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**

![](https://cdn-images-1.medium.com/max/2000/1*P96VZ0fOjMgakUsA93qBAQ.png)

So this is the output of the Sample code.

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