During this short exercise you will learn how to create a simple LAMP example application and deploy it using Docker on the Amazon Web Services ECS platform, with the help of RDS for a MySQL database.
All the following example is based on the AWS CLI, though we could have done faster and easier using the web based console. Instead, having gained a skill with the terminal interface, allows us to better understand how each step and parameter is working. In addition we will be able to create our own automation scripts based on the CLI.
The prerequites for this tutorial are the installation of the AWS CLI and the ECS CLI. If you haven’t them installed on your system, please refer to these links on how to do it: install the AWS CLI and the ECS CLI.
This tutorial is based on Fedora 29, some minor differences might occour on other operating systems. If you’re new to Docker this tutorial might help.
The second thing you need to do is to setup a suitable region for your AWS CLI, in that this tutorial is based on FARGATE, which is not available on all regions so far.
FARGATE allow us to spin up Docker images without the need to create EC2 instances to manage them, with the downside of a minor flexibility and control over your instances.
Let’s configure our AWS CLI with those regions, for example: ‘eu-central-1‘, which is Frankfurt. When asked put in your credentials, region, and ‘json‘ as output format.
$ aws configure
Let’s now create a working directory and go into it:
$ mkdir aws_lamp_tutorial
$ cd aws_lamp_tutorial
Then create a MySQL database on RDS and get the relative address:
$ aws rds create-db-instance \
--db-name aws_lamp_tutorial \
--db-instance-identifier mysql-tutorial-db \
--db-instance-class db.t2.micro \
--engine MySQL \
--allocated-storage 20 \
--master-username dbmaster \
$ aws rds describe-db-instances --db-instance-identifier mysql-tutorial-db | grep Address
Let’s create an index file for our application, which only connects to the database and does nothing else, you will have to put the address you read in the output of the commad above:
$ vi index.php
$conn = mysqli_connect("mysql-tutorial-db.cvrs0klpxnsi.eu-central-1.rds.amazonaws.com:3306",
"dbmaster", "putyourownpassword", "aws_lamp_tutorial");
if($conn) echo "Connected to the database.";
Let’s pull an Apache and PHP Docker image and put that ‘index.php‘ inside its document root, then build a new image based on it.
$ vi Dockerfile
COPY index.php /var/www/html
RUN docker-php-ext-install mysqli
But first, we create a registry on ECR and get its address:
$ aws ecr create-repository --repository-name aws_lamp_tutorial
$ aws ecr describe-repositories --repository-names aws_lamp_tutorial | grep repositoryUri
Let’s build it, be careful of putting your own Account ID instead of the zeroes:
$ docker build -t="000000000000.dkr.ecr.eu-central-1.amazonaws.com/aws_lamp_tutorial" .
Again, let’s login into our ECR and push our image into the registry, it might take a while, depending on your internet connection:
$ aws ecr get-login --no-include-email --region eu-central-1 | bash
$ docker push 000000000000.dkr.ecr.eu-central-1.amazonaws.com/aws_lamp_tutorial
Once again, please replace the zeroes. Now we have to create an IAM role and attach the right policy to it, let’s begin by creating a policy file:
$ vi policy-document.json
$ aws iam create-role \
--role-name aws_tutorial_role \
$ aws iam attach-role-policy \
--role-name aws_tutorial_role \
This tutorial goes on, for the second part of it click on Page 2 here below.
Hi there, my name is Mirko Benedetti and I’m a Software Developer. Excellence is what I consider to be our ultimate goal, and passion for technology constantly drives me to it. I began programming self-taught at a very young age; since then I learned a lot and every day I love learning new things. Here is my Linkedin Curriculum, feel free to connect.