Categories
Admin AWS Docker Laravel PHP Programming Tutorials Web Dev

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

By Mirko Benedetti

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.

4 replies on “Full LAMP Docker and AWS Introduction Tutorial”

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

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.

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

Leave a Reply

Give me your opinion, I will be grateful.