Full LAMP Docker and AWS Introduction Tutorial

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.

Coding and Software Engineering

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 \
     --master-user-password putyourownpassword
$ 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
FROM php:7.2.4-apache
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
{
   "Version": "2012-10-17",
   "Statement": [
     {
       "Sid": "",
       "Effect": "Allow",
       "Principal": {
         "Service": "ecs-tasks.amazonaws.com"
       },
       "Action": "sts:AssumeRole"
     }
   ]
 }
$ aws iam create-role \
     --role-name aws_tutorial_role \
     --assume-role-policy-document file://policy-document.json
$ aws iam attach-role-policy \
     --role-name aws_tutorial_role \
     --policy-arn arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy

This tutorial goes on, for the second part of it click on Page 2 here below.

10 thoughts on “Full LAMP Docker and AWS Introduction Tutorial

  1. Many thanks Mary,
    I’m looking forward to seeing you again on this blog!

  2. I don’t know where you are getting your information, however great topic. I need to spend some time learning and understanding more. Thanks for this wonderful information, I was searching for this information for my purposes.

  3. Thank you Paul,
    the content of this post and this blog in general is gathered around the internet, on books and other sources.
    Code examples are my own.
    Keep learning this stuff,
    Mirko

  4. I would like to thank you for the efforts you have put in writing this blog. I am hoping the same high-grade posts from you in the upcoming also. Actually your creative writing abilities has encouraged me to get my own site now. Blogging is spreading its wings rapidly, your write up is a good example of it.

  5. Thank you Alessandra,
    this blog would not be possible without your support and encouragement.
    See you

  6. I do agree with all the ideas you’ve introduced in your post. They are really convincing and can certainly work. Still, the posts are too quick for beginners. May just you please extend them a little from subsequent time? Thanks for the post.

  7. Hello Louis,
    I appreciate your feedback and thanks for getting straight to the point. My endevour is to explain as simple as possible, I’ll do my best to keep improving.
    Bye

  8. I was just seeking this info for some time. After many hours of continuous Googleing, finally I got it at your web site. I wonder what is the lack of Google strategy that don’t rank this kind of informative web sites at top of the list. Sometimes top sites are full of garbage.

  9. Hello Xin,
    this website is well ranked for many keywords that regards it. The strategy is to actually putting very meaningful and useful content, in a way that Google cannot ignore it. Sometimes we achieve this result.

Leave a Reply

Give me your opinion, I will be grateful.