Skip to main content

Command Palette

Search for a command to run...

How To Implement Microservices by Using AWS Containers

Updated
3 min read
How To Implement Microservices by Using AWS Containers
T

Cloud & SRE specializing in AWS and DevOps. I share my ongoing learning journey through practical tutorials and insights. Let's grow together.

Step 1. Create an ECR repository

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

Image description

  • Click Repositories

Image description

  • Click Create repository

Image description

  • Write Repository name

Image description

  • Click Create repository

Image description

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

Image description

  • Click Close

Image description

  • Create new folder by typing the following command
mkdir ~/labmicroservice

Image description

  • type the following command
cd ~/labmicroservice

Image description

  • Create Dockerfile by using VIM Editor
vim Dockerfile
  • 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

Image description

  • Run the following command
docker build --tag labmicroservice .

Image description

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}

Image description

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

Image description

  • Click Repositories

Image description

  • Click labmicroservice

Image description

  • Click latest

Image description

  • Copy this URL

Image description

Step 3. Create an ECS cluster using Amazon Fargate

  • Type Elastic Container Service in Services and click

Image description

  • Click Create cluster

Image description

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

Image description

Image description

Image description

  • Click Task definitions

Image description

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

Image description

Image description

  • Type name and paste ULR you copied earlier

Image description

  • Clear Use log collection

Image description

  • Click Create

Image description

  • Select Create service

Image description

  • Select Launch type

Image description

  • Type Service name

Image description

  • Select VPC and Choose All subnets and Security Group

  • Click Create

Image description

Image description

Step 4. Test the container application

  • Click Task and Click on task name

Image description

  • Copy Public ip and open in new browser

Image description

Image description


Resources & Next Steps


More from this blog

W

Whispering Cloud Insights

88 posts

Documenting my path to cloud journey. Sharing lessons, tutorials, and insights from my continuous learning journey. Learn with me.