Wednesday, March 8, 2017

Running a digital card store on AWS

In my previous post, I've written about registering an AWS Free Tier account and about the things you should beware of.

As I promised in my previous post, I will show how to run a simple Java Spring Boot application on AWS.

As an example, I have developed a digital card store application using Spring Boot framework. Digital trade cards are the digital versions of classic trade cards. There are so popular digital cards that it is hard to believe a simple JPG file worth $225, $440 and even $1524.

So our application is a prototype digital card store that manages cards. Source code for the application can be found here. In the future posts, I will add various features that use different AWS services.

The prototype allows you  to add a new card, list existing cards and get details of a specific card using REST requests and responses.

Prerequisites

1. An AWS account
2. AWS CLI Tools
3. Git
4. Maven

In my previous post, I showed how to register and install AWS CLI Tools.

Steps

The steps to build and run the application are below.



1. Clone the application from GitHub
2. Build and test the application
3. Put the Spring Boot fat jar to S3.
4. Launch a new EC2 instance that pulls the jar from S3 and starts the application.

Let's start.

1. Clone the application from GitHub
Execute the git clone command to clone the application.

git clone https://github.com/ceyhunozgun/cardstore1

2. Build and test the application

cd cardstore1
mvn package
mvn spring-boot:run

After the application starts, direct your browser to http://localhost:8080

You should see the home page of the card store app.



In the home page you can create a new card and list the cards using the link below.

3. Put the Spring Boot Fat Jar to S3

When we package the application, Maven will create the fat jar file in the target directory.

$ ls target/*.jar
target/cardstore-0.0.1-SNAPSHOT.jar

Before copying the file to S3, we should create a S3 bucket. We can create a new bucket using AWS CLI.

$ aws s3 mb s3://cardstoredeploy
make_bucket: cardstoredeploy

To put the jar to the S3 bucket, execute the following command.

$ aws s3 cp target/cardstore-0.0.1-SNAPSHOT.jar s3://cardstoredeploy
upload: target\cardstore-0.0.1-SNAPSHOT.jar to s3://cardstoredeploy/cardstore-0.0.1-SNAPSHOT.jar

Now you should see the jar in AWS S3 Console.



4. Launch the EC2 Instance

In this step, we will use AWS CLI to create an EC2 instance. Before we create an EC2 instance we should do some preparations. The preparations can be done by AWS CLI, but to keep the things simple we will use the AWS Console.

To launch an EC2 instance we should do the following:

1. Create an IAM Role for EC2 service to read the jar file from S3. 

Although full access is not needed in this application, we will create a role with AmazonS3FullAccess policy to use it in future posts.
  • Sign in to the AWS Console. Under AWS Services choose IAM
  • Select Roles from the navigation pane and then click Create Role.
  • Enter S3-Admin-Role as Role Name and click Next Step.
  • Expand AWS Service Roles section and select  Amazon EC2. 
  • Enter S3 to filter the policies and select the check box for AmazonS3FullAccess
  • Click Next Step to review the role and then click Create Role.

2. Generate EC2 key pair that will be used to access the EC2 instance.

A key pair is required to create an EC2 instance. This key pair is normally used to connect to the instance using SSH. In this post we won't connect to the EC2 instance over SSH.
  • At the AWS console choose EC2.  
  • Select Key Pairs under Network & Security from the navigation pane. Click Create Key Pair. 
  • Enter CardStoreKP as key pair name and click Create. 
After the key pair is created the private key file is automatically downloaded by your browser. Save this private key file in a safe place. This key file is required to connect to EC2 instances via SSH. You can find the detailed information here

3. Create a security group to allow inbound SSH and HTTP traffic (ports 22 and 8080)

Security groups are used to allow incoming and outgoing traffic. For our application we will create a security group to allow inbound access for SSH and HTTP protocols.
  • In AWS Console, click Services and select EC2.  
  • Under Network & Security, choose Security Groups. Click Create Security Group. 
  • Enter CardStoreSG as name and 'Card Store Security Group to allow SSH and 8080 Http traffic' as description and leave the VPC as default. 
  • Under Inbound rules click Add Rule. 
  • Select SSH from Type combo. Choose Anywhere under Source. 
  • Click Add Rule again and select Custom TCP Rule and enter 8080 in Port Range. Choose Anywhere under Source. 
  • Click Create to create security group. 
All the outbound traffic is allowed by default.

Please note that to keep it simple, we have allowed any source. In real usage scenarios, you should limit the access accordingly.

Note Security group Id. It should be like ' sg-8567a2ee'.

4. Create the EC2 instance that gets the jar from S3 and runs it.

We will use AWS CLI to create an EC2 instance that runs Amazon Linux. To get the jar file from S3 and run the application when the instance starts, the EC2 init script mechanism is used. 

Init script is in cardstore1_ec2_init_script.txt file.


#!/bin/bash

yum install java-1.8.0 -y
yum remove java-1.7.0-openjdk -y

mkdir app

aws s3 cp --region eu-central-1 s3://cardstoredeploy/cardstore-0.0.1-SNAPSHOT.jar app/

java -jar app/cardstore-0.0.1-SNAPSHOT.jar

The current Amazon Linux comes with Java 7 installed. Because Spring Boot requires Java 8, first we install Java 8. After that we remove Java 7 to make Java 8 default. Once Java installation is done, we get the jar file from S3.

Please note that --region parameter should point to the region that your S3 bucket is created in. Finally, the application is launched by java command.


Now we are ready to launch the Amazon Linux EC2 instance. We create the EC2 instance with aws ec2 run-instances command.

$ aws ec2 run-instances --image-id ami-af0fc0c0 --count 1 --instance-type t2.micro --key-name CardStoreKP --user-data file://cardstore1_ec2_init_script.txt --associate-public-ip-address --security-group-ids sg-8567a2ee  --iam-instance-profile Name=S3-Admin-Role

The meaning of the parameters for the run-instances command is explained below.

--image-id ami-af0fc0c0
The Amazon Machine Image (AMI) id for current Amazon Linux operating system.

--count 1
We want to launch one instance.

--instance-type t2.micro
We use t2.micro to stay in the AWS Free Tier limits.

--key-name CardStoreKP 
The key name of the key pair we have created before.

--user-data file://cardstore1_ec2_init_script.txt
The file to specify the commands that will be executed when the server starts.

--associate-public-ip-address
Required for instance to get a public ip address, so we can connect from outside.

--security-group-ids sg-8567a2ee
The id of the security group we have created before.

--iam-instance-profile Name=S3-Admin-Role
The name of the IAM role that will be used by instance. Required for instance to access S3 services in init script.


After this command, a new EC2 instance will be created. It will take some time to instance to be created and get an public ip address. To connect to instance, we will need the public ip address that will be assigned to the instance. We can get the public ip address by executing the describe-instances command.

$ aws ec2 describe-instances|grep PublicIpAddress
"PublicIpAddress": "35.157.111.179"

For my instance 35.157.111.179 was assigned. Your ip address will  be different. You should use the ip address assigned to your instance.

After the application started you can connect to the instance using your browser at http://<ip address>:8080

The home page of the application should look like the below.




You can enter the card details and click 'Create Card' button to create a new card. The id of the created card will be displayed.




You can use this id to get its details at http://<ip address>:8080/cards/<id>



And finally you can list all the cards by using http://<ip address>:8080/cards/



Summary

In this post, I showed how to run a simple Java Spring Boot application on AWS. Source code for the application can be found here.

In my next posts, I will show how to use other services on Amazon Web Services.



36 comments:

  1. I feel really happy to have seen your webpage and look forward to so many more entertaining times reading here. Thanks once more for all the details.
    aws training in marathahalli|

    ReplyDelete
    Replies
    1. Computer Repair Service Madurai Chittakkur
      https://123just.com/ad/67/computer-repair-service-in-madurai

      Delete
  2. Everyone wants to get unique place in the IT industry’s for that you need to upgrade your skills, your blog helps me improvise my skill set to get good career, keep sharing your thoughts with us.

    selenium training in chennai|
    selenium training in bangalore|

    ReplyDelete
  3. I feel really happy to have seen your webpage and look forward to so
    many more entertaining times reading here. Thanks once more for all
    the details.



    AWS Training in Bangalore


    AWS Training in Bangalore

    ReplyDelete
  4. Those guidelines additionally worked to become a good way to recognize that other people online have the identical fervor like mine to grasp great deal more around this condition.
    "Java Training in Marathahalli"

    ReplyDelete
  5. the blog on the aws field and their uses are written well and the analysis are awesome.. thank u for the writeup..


    aws training in bangalore

    ReplyDelete
  6. Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site.
    aws training in chennai
    Advanced aws training in chennai

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. AWS Training in Bangalore - Live Online & Classroom
    myTectra Amazon Web Services (AWS) certification training helps you to gain real time hands on experience on AWS. myTectra offers AWS Training in Bangalore
    using classroom and AWS Online Training globally.

    IOT Training in Bangalore - Live Online & Classroom
    JKcourse observes iot as the platform for networking of different devices on the internet and their inter related communication. Iot Training in Bangalore

    ReplyDelete
  9. It has been basically extraordinarily liberal with you to give straightforwardly what precisely numerous people would've promoted for an eBook to wind up making some money for their end, principally given that you could have attempted it in the occasion you needed. Keep sharing
    Tableau online training in India, Australia
    Tableau online training in USA, UK

    ReplyDelete
  10. Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. AWS Online Course Hyderabad

    ReplyDelete
  11. I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page!Really very informative and creative contents. This concept is a good way to enhance the knowledge.thanks for sharing.
    please keep it up.Java training in Chennai

    Java Online training in Chennai

    Java Course in Chennai

    Best JAVA Training Institutes in Chennai

    Java training in Bangalore

    Java training in Hyderabad

    Java Training in Coimbatore

    Java Training

    Java Online Training

    ReplyDelete
  12. It is perfect time to make some plans for the future and it is time to be happy. I’ve read this post and if I could I desire to suggest you few interesting things or tips.highly informative and professionally written and I am glad to be a visitor of this perfect blog, thank you
    selenium training in chennai

    selenium training in chennai

    selenium online training in chennai

    software testing training in chennai

    selenium training in bangalore

    selenium training in hyderabad

    selenium training in coimbatore

    selenium online training

    selenium training

    ReplyDelete
  13. It is perfect time to make some plans for the future and it is time to be happy. I’ve read this post and if I could I desire to suggest you few interesting things or tips.highly informative and professionally written and I am glad to be a visitor of this perfect blog, thank you
    selenium training in chennai

    selenium training in chennai

    selenium online training in chennai

    software testing training in chennai

    selenium training in bangalore

    selenium training in hyderabad

    selenium training in coimbatore

    selenium online training

    selenium training

    ReplyDelete
  14. It has been basically extraordinarily liberal with you to give straightforwardly what precisely numerous people would've promoted for an eBook to wind up making some money for their end, principally given that you could have attempted it in the occasion you needed. Keep sharing..


    angular js training in chennai

    angular training in chennai

    angular js online training in chennai

    angular js training in bangalore

    angular js training in hyderabad

    angular js training in coimbatore

    angular js training

    angular js online training

    ReplyDelete
  15. Nice blog Post ! This post contains very informative and knowledgeable. Thanks for sharing the most valuable information.Good content. The explanation of content explained very neat.
    DevOps Training in Chennai

    DevOps Online Training in Chennai

    DevOps Training in Bangalore

    DevOps Training in Hyderabad

    DevOps Training in Coimbatore

    DevOps Training

    DevOps Online Training

    ReplyDelete
  16. I feel really happy to have seen your webpage.I am feeling grateful to read this.you gave a nice information for us.please updating more stuff content...thanks lot!!

    Android Training in Chennai

    Android Online Training in Chennai

    Android Training in Bangalore

    Android Training in Hyderabad

    Android Training in Coimbatore

    Android Training

    Android Online Training

    ReplyDelete
  17. I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page!Really very informative and creative contents.



    AWS Course in Bangalore

    AWS Course in Hyderabad

    AWS Course in Coimbatore

    AWS Course

    AWS Certification Course

    AWS Certification Training

    AWS Online Training

    AWS Training

    ReplyDelete
  18. Am really impressed about this blog because this blog is very easy to learn and understand clearly.This blog is very useful for the college students and researchers to take a good notes in good manner,I gained many unknown information.Data Science Training In Chennai

    Data Science Online Training In Chennai

    Data Science Training In Bangalore

    Data Science Training In Hyderabad

    Data Science Training In Coimbatore

    Data Science Training

    Data Science Online Training

    ReplyDelete
  19. This is the exact information I am been searching for, Thanks for sharing the required infos with the clear update and required points.

    Data Science Training in Hyderabad

    ReplyDelete
  20. we offer best AI Training in Hyderabad
    https://www.analyticspath.com/artificial-intelligence-training-in-hyderabad

    ReplyDelete
  21. This is the exact information I am been searching for, Thanks for sharing the required infos with the clear update and required points.
    Data Science Training in hyderabad

    ReplyDelete


  22. Digital Lync offers one of the best Online Courses Hyderabad with a comprehensive course curriculum with Continuous Integration, Delivery, and Testing. Elevate your practical knowledge with quizzes, assignments, Competitions, and Hackathons to give a boost to your confidence with our hands-on Full Stack Training. An advantage of the online Cources development course in Hyderabad from Digital Lync is to get industry-ready with Career Guidance and Interview preparation.
    DevOps Training Institute
    Python Training Institute
    AWS Training Institute
    Online Full Stack Developer Course Hyderabad
    Python Course Hyderabad
    Online AWS Training Course Hyderabad
    devops training in hyderabad
    angular training in hyderabad

    ReplyDelete
  23. It is not my first time to pay a visit this web page, i
    am browsing this website dailly and get fastidious facts from here all the time.Click Me Here슬롯머신


    3YANGSKIE

    ReplyDelete
  24. excellent submit, very informative. I wonder why the other specialists of this sector do
    not realize this. You must continue your writing. I am sure,
    you have a huge readers’ base already!Click Me Here사설경마


    6YAGKIE

    ReplyDelete
  25. "Looking to kickstart your career in SAP EWM? Bangalore offers a plethora of options for SAP EWM training.
    Here’s what to consider:
    SAP EWM Training in Bangalore at SAP Masters
    1.Quality Training:
    Opt for best institutes with a solid reputation and accreditation from SAP Masters Training institute.
    Look for expert faculty who can provide comprehensive insights.
    2.Curriculum:Ensure the curriculum covers core EWM concepts and offers hands-on projects for practical learning.
    3.Infrastructure: Check for modern facilities and labs equipped with the latest software to support your training needs.
    4.Placement Support: Choose institutes that offer robust placement assistance, including resume building and interview preparation.
    Consider institutes like SAP Masters Institute of Technology, sap masters Academy, and sapmasters training institute bangalore,
    known for their quality training and successful placements. Choose wisely, and jumpstart your SAP EWM journey in Bangalore!



    Visit SAP Masters - Best SAP Training in Bangalore"
    Visit SAP Masters - SAP EWM Training in Bangalore"

    ReplyDelete