DEV Community

Cover image for How To Implement Microservices by Using AWS Containers
Thu Kha Kyawe
Thu Kha Kyawe

Posted on

How To Implement Microservices by Using AWS Containers

Step 1. Create an ECR repository

  • In the AWS navigation bar, type ECR into the search box, and then select Elastic Container Registry service

  • Click Repositories

  • Click Create repository

  • Write Repository name

  • Click Create repository

  • In the AWS navigation bar, type cloudshell into the search box, and then click

  • Click Close

  • Create new folder by typing the following command
mkdir ~/labmicroservice
Enter fullscreen mode Exit fullscreen mode

  • type the following command
cd ~/labmicroservice
Enter fullscreen mode Exit fullscreen mode

  • Create Dockerfile by using VIM Editor
vim Dockerfile
Enter fullscreen mode Exit fullscreen mode
  • Copy and paste the following command in Dockerfile and save
FROM public.ecr.aws/amazonlinux/amazonlinux:latest

# Install dependencies
RUN yum update -y && \
yum install -y httpd

# Install the cowsay binary
RUN dnf install --assumeyes cowsay

# Install apache and write a web page
RUN echo "<html><pre style=\"line-height:100%\">" > /var/www/html/index.html
RUN cowsay 'We have a LOT of milk in stock!' >> /var/www/html/index.html
RUN echo "</pre></html>" >> /var/www/html/index.html

# Configure apache
RUN echo 'mkdir -p /var/run/httpd' >> /root/run_apache.sh && \
echo 'mkdir -p /var/lock/httpd' >> /root/run_apache.sh && \
echo '/usr/sbin/httpd -D FOREGROUND' >> /root/run_apache.sh && \
chmod 755 /root/run_apache.sh

EXPOSE 80

CMD /root/run_apache.sh
Enter fullscreen mode Exit fullscreen mode

  • Run the following command
docker build --tag labmicroservice .
Enter fullscreen mode Exit fullscreen mode

Step 2. Deploy a Docker container to ECR

  • Type the following command and press Enter
REPO=labmicroservice

ACCOUNT=$(aws sts get-caller-identity --query "Account" --output text)

ECR=${ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com
aws ecr get-login-password | docker login --username AWS --password-stdin ${ECR}

docker tag labmicroservice ${ECR}/${REPO}

docker push ${ECR}/${REPO}

Enter fullscreen mode Exit fullscreen mode

  • Type the following command
aws ecr list-images --repository-name labmicroservice --region ${AWS_REGION}
Enter fullscreen mode Exit fullscreen mode

  • Click Repositories

  • Click labmicroservice

  • Click latest

  • Copy this URL

Step 3. Create an ECS cluster using Amazon Fargate

  • Type Elastic Container Service in Services and click

  • Click Create cluster

  • Type Cluster name and Choose AWS Fargate (serverless) then Click Create

  • Click Task definitions

  • Click Create new task definition and choose Create new task definition

  • Type name and paste ULR you copied earlier

  • Clear Use log collection

  • Click Create

  • Select Create service

  • Select Launch type

  • Type Service name

  • Select VPC and Choose All subnets and Security Group

  • Click Create

Step 4. Test the container application

  • Click Task and Click on task name

  • Copy Public ip and open in new browser


Resources & Next Steps


Top comments (0)