Table of contents
1.
Introduction📑
2.
Deploying to Heroku🧑‍💻
2.1.
Deploying to a Remote Git Repository🖥️
2.2.
Install your program using git
2.3.
Make a new Heroku application.
2.4.
Deploy your program
2.5.
Verify the deployment of your application
2.6.
Troubleshooting🤯
2.7.
Deploying Applications that use the most recent Java Versions
3.
Deploying to Cloud Foundry🚀
3.1.
Create your Application
3.2.
Deploy your Application
3.3.
Working with Services
3.3.1.
Auto Reconfiguration
3.3.2.
Connecting to Cloud Foundry Services
3.3.3.
Opting out of Auto Reconfiguration
4.
Deploying to Clever Cloud🌩️
4.1.
Build application on Clever Cloud
4.2.
Deploy your program
4.3.
Verify the application's deployment
4.4.
Configure your application [Optional]
4.5.
Accessing a Database🧊
5.
Deploying to Cloud Captain and AWS
5.1.
Create your application
5.2.
Deploy your application
6.
Frequently Asked Questions
6.1.
What are the advantages of putting your application in a cloud environment?
6.2.
Is it possible to deploy any application on the cloud?
6.3.
What port does the Play application use by default?
6.4.
Why do we use a play framework?
6.5.
What difficulties arise when deploying cloud applications?
7.
Conclusion
Last Updated: Mar 27, 2024
Medium

Deploying a Play Application to a Cloud Service

Author Ishita mishra
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction📑

The emergence of cloud services is one of the primary factors enabling digital transformation. Cloud storage is significantly safer than on-site servers, one of the reasons cloud services have grown so well-liked. A company's data can be easily deployed and saved in the cloud even if a gadget is stolen or damaged. 

Deploying a Play Application to a Cloud Service.

So, without further ado, let's take a deeper look at the deployment of a Play Application to a Cloud Service and briefly understand the four deployment methods. 

Deploying to Heroku🧑‍💻

Deploying to Heroku

Heroku is a platform for building and deploying web applications in the cloud. Heroku is a framework for deploying and operating contemporary apps based on a managed container architecture, integrated data services, and a robust ecosystem. 

To begin:

  1. Setup the Heroku Toolbelt 
  2. Open a Heroku account.

 

There are two ways to deploy to Heroku:

  1. Pushing to a distant Git repository
  2. Using the SBT-Heroku plugin

Deploying to a Remote Git Repository🖥️

Install your program using git

$ git init
$ git add .
$ git commit -m "init"

Make a new Heroku application.

$ heroku create
Creating warm-frost-1289... done, stack is cedar-14
http://warm-frost-1289.herokuapp.com/ | git@heroku.com:warm-frost-1289.git
Git remote heroku added


This creates a new application with your application's HTTP (and HTTPS) and Git endpoints. The Git endpoint configures your Git repository's setup as a new remote called Heroku.

Deploy your program

Use Git to push your application into the Heroku remote repository to deploy it there:

$ git push heroku main
Counting objects: 93, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (84/84), done.
Writing objects: 100% (93/93), 1017.92 KiB | 0 bytes/s, done.
Total 93 (delta 38), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Play 2.x - Scala app detected
remote: -----> Installing OpenJDK 1.8... done
remote: -----> Priming Ivy cache (Scala-2.11, Play-2.4)... done
remote: -----> Running: sbt compile stage
...
remote: -----> Dropping ivy cache from the slug
remote: -----> Dropping sbt boot dir from the slug
remote: -----> Dropping compilation artifacts from the slug
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote:
remote: -----> Compressing... done, 93.3MB
remote: -----> Launching... done, v6
remote:        https://warm-frost-1289.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/warm-frost-1289.git
* [new branch]      main -> main


Heroku will launch sbt stage to get your application ready. All dependencies will be downloaded during the initial deployment, which takes some time to finish (but they will be cached for future implementations).

If your application stalls at this point when using RequireJS:

[info] Optimizing JavaScript with RequireJS


Then try following the instructions in the Heroku Dev Center article Using Node.js to Perform JavaScript Optimization for Play and Scala Applications. The Javascript engine's performance will significantly increase as a result.

Verify the deployment of your application

Let's verify the status of the processes used by the program now:

$ heroku ps
=== web (Free): `target/universal/stage/bin/sample-app -Dhttp.port=${PORT}`
web.1: up 2022/08/14 11:27:51 (~ 5m ago)


The web server is currently operating. To learn more, we can look at the logs:

$ heroku logs
2022-08-13T20:44:49.750860+00:00 app[web.1]: Picked up JAVA_TOOL_OPTIONS: -Xmx384m -Xss512k -Dfile.encoding=UTF-8
2022-08-13T20:44:47.358320+00:00 heroku[web.1]: Starting process with command `target/universal/stage/bin/myapp -Dhttp.port=${PORT}`
2022-08-13T20:44:52.297033+00:00 app[web.1]: [warn] application - Logger configuration in conf files is deprecated and has no effect. Use a logback configuration file instead.
2022-08-13T20:44:54.960105+00:00 app[web.1]: [info] p.a.l.c.ActorSystemProvider - Starting application default Akka system: application
2022-08-13T20:44:55.066582+00:00 app[web.1]: [info] play.api.Play$ - Application started (Prod)
2022-08-13T20:44:55.445021+00:00 heroku[web.1]: State changed from starting to up
2022-08-13T20:44:55.330940+00:00 app[web.1]: [info] p.c.s.AkkaHttpServer - Listening for HTTP on /0:0:0:0:0:0:0:0:9000


The logs can also be followed just like a regular file. For troubleshooting, we can use:

$ heroku logs -t --app warm-frost-1289
2015-07-13T20:44:47.358320+00:00 heroku[web.1]: Starting process with command `target/universal/stage/bin/myapp -Dhttp.port=${PORT}`
2015-07-13T20:44:49.750860+00:00 app[web.1]: Picked up JAVA_TOOL_OPTIONS: -Xmx384m -Xss512k -Dfile.encoding=UTF-8
2015-07-13T20:44:52.297033+00:00 app[web.1]: [warn] application - Logger configuration in conf files is deprecated and has no effect. Use a logback configuration file instead.
2015-07-13T20:44:54.960105+00:00 app[web.1]: [info] p.a.l.c.ActorSystemProvider - Starting application default Akka system: application
2015-07-13T20:44:55.066582+00:00 app[web.1]: [info] play.api.Play$ - Application started (Prod)
2015-07-13T20:44:55.445021+00:00 heroku[web.1]: State changed from starting to up
2015-07-13T20:44:55.330940+00:00 app[web.1]: [info] p.c.s.AkkaHttpServer - Listening for HTTP on /0:0:0:0:0:0:0:0:9000
...


It looks good. Now, we can access the app by launching:

$ heroku open

Troubleshooting🤯

Heroku will detect the presence of a build.gradle file in your app and attempt to build it as a Gradle project rather than a Scala sbt project. By executing the following command, you may compel Heroku to use using sbt:

$ heroku buildpacks:set heroku/scala


Your repository's build.sbt file will be used by the Scala build pack to create the application.

Deploying Applications that use the most recent Java Versions

Heroku runs Java apps by default with OpenJDK 8. Deploying an application that utilizes a newer version of Java results in a compilation error on the server since it cannot automatically detect whether another version is required. Java 8 and later versions should be declared in your system if you utilize them. in the project root directory's properties file, for instance:

java.runtime.version=11

Deploying to Cloud Foundry🚀

Deploying to Cloud Foundry

Create your Application

The following command will install or update the Cloud Foundry command line tool, VMC, to the most recent version (0.3.18 or higher), after creating a free Cloud Foundry account:

gem install vmc

 

You can package your software by entering the dist command in the play prompt.

Deploy your Application

Utilize the VMC push command to deploy the generated zip file to Cloud Foundry. Cloud Foundry will immediately start using your database updates if you decide to build a database service.

myapp$ vmc push --path=dist/myapp-1.0.zip
Application Name: myapp
Detected a Play Framework Application, is this correct? [Yn]:
Application Deployed URL [yourapp.cloudfoundry.com]:
Memory reservation (128M, 256M, 512M, 1G, 2G) [256M]:
How many instances? [1]:
Create services to bind to 'myapp'? [yN]: y
1: mongodb
2: mysql
3: postgresql
4: rabbitmq
5: redis
What kind of service?: 3
Specify the name of the service [postgresql-38199]: my-db
Create another? [yN]:
Would you like to save this configuration? [yN]: y
Manifest written to manifest.yml.
Creating Application: OK
Creating Service [my-db]: OK
Binding Service [my-db]: OK
Uploading Application:
  Checking for available resources: OK
  Processing resources: OK
  Packing application: OK
  Uploading (186K): OK
Push Status: OK
Staging Application 'myapp': OK
Starting Application 'myapp': OK

Working with Services

Auto Reconfiguration

Your Play application will immediately connect to a relational database service using Cloud Foundry's auto-reconfiguration method. Cloud Foundry will automatically override the connection properties in the configuration to point to the PostgreSQL or MySQL service bound to the application if a single database configuration is found in the Play configuration (for instance, default) and a single database service instance is attached to the application.

This is an excellent way to launch small apps quickly. But your application will likely use SQL that is particular to the kind of database you're utilizing. You may forego auto-reconfiguration in certain circumstances or if your app has to bind to numerous services and explicitly provide the service connection settings.

Connecting to Cloud Foundry Services

As usual, Cloud Foundry gives your application access to all of your service connection data in JSON format via the VCAP_SERVICES environment option. However, you can also use several properties to access connection information in your Play configuration. Here is an illustration of how to connect to the tasks-db PostgreSQL service from an application.conf file:

db.default.driver=${?cloud.services.tasks-db.connection.driver}
db.default.url=${?cloud.services.tasks-db.connection.url}
db.default.password=${?cloud.services.tasks-db.connection.password}
db.default.username=${?cloud.services.tasks-db.connection.username}

 

All services, including NoSQL and messaging services, have access to this data. Additionally, if a type (such as postgresql) only consists of one service, you can refer to it just by type rather than by name, as seen below:

db.default.driver=${?cloud.services.postgresql.connection.driver}
db.default.url=${?cloud.services.postgresql.connection.url}
db.default.password=${?cloud.services.postgresql.connection.password}
db.default.username=${?cloud.services.postgresql.connection.username}

 

When creating a distribution for Cloud Foundry, we advise maintaining these properties in a separate file (like cloud.conf) and including them afterward. The -Dconfig.file option allows you to play dist with a different configuration file.

Opting out of Auto Reconfiguration

You will automatically be opted out if you use the above properties. To explicitly opt out, add the line autoconfig=false to the "cloudfoundry.properties" file in your application's conf directory.

Deploying to Clever Cloud🌩️

Deploying to Clever Cloud

Build application on Clever Cloud

A platform as a service product is a clever Cloud. Applications written in Scala, Java, PHP, Python, and Node.js can be deployed on it. It offers automated vertical and horizontal scaling, its key unique feature.

Applications for Play! 2 are natively supported by Clever Cloud. This guide explains how to launch your application on Clever Cloud.

Use the Clever Cloud dashboard to create your Play! Application.

Deploy your program

Push your code to the remote application repository using git to deploy your application on Clever Cloud.

$ git remote add <your-remote-name> <your-git-deployment-url>
$ git push <your-remote-name> main

 

Important Tip🎯  Don't forget to push to the remote main branch.

If you are employed in a different branch, use:

$ git remote add <your-remote-name> <your-git-deployment-url>
$ git push <your-remote-name> <your-branch-name>:main

 

To prepare your application, Clever Cloud will perform sbt update stage. All dependencies will be downloaded during the initial deployment, which takes some time to finish (but will be cached for future implementations).

Verify the application's deployment

You may look at the deployment of your application by heading to the logs area of your application on the dashboard.

Configure your application [Optional]

clevercloud/sbt.json file allows you to customize your application.

The following fields should be present in the file:

{
    "deploy": {
        "goal": <string>
    }
}

 

The following additional setting is possible in that field:

"-Dconfig.resource=clevercloud.conf", "-Dplay.version=2.0.4" or "-Dplay.evolutions.autoApply=true"

Accessing a Database🧊

To add the database you require, such as MySQL, PostgreSQL, or Couchbase, browse to the Services area of the Clever Cloud dashboard.

The only thing you should change, as with every Play! 2 application, is your conf/application.conf file.

An illustration would be to set up a MySQL database.

db.default.url="jdbc:mysql://{yourcleverdbhost}/{dbname}"
db.default.driver=com.mysql.jdbc.Driver
db.default.username={yourcleveruser}
db.default.password={yourcleverpass}

Deploying to Cloud Captain and AWS

Deploying on AWS

You may deploy your Play applications on AWS using CloudCaptain.  Minimal Images, Immutable Infrastructure, and Blue/Green deployments serve as its three guiding concepts.

CloudCaptain converts your Play dist zip into a simple VM image that can be deployed unchanged on VirtualBox or AWS and comes with native Play application functionality. This Image is 100 times smaller than a typical Linux system and is created instantly. The Linux kernel, a JRE, and your Play application are essentially the only components, minimizing the security attack surface to the bare minimum.

With the help of your AWS account, CloudCaptain automatically creates all the AWS resources your application needs, such as EC2 instances, Elastic IPs, Elastic Load Balancers, Security Groups, and Auto-Scaling Groups.

Create your application

Install the CloudCaptain command line software and register for a free CloudCaptain and AWS account.

For CloudCaptain to interact with your AWS account, it must first have access to the required permissions. To connect your AWS account, immediately go to the CloudCaptain Console if you haven't already.

Use the sbt dist command in your project directory to build your application.

Deploy your application

There are three environments—dev, test, and prod—included with each new CloudCaptain account. Dev uses a VirtualBox environment for quick roundtrips locally, during trial and production, use AWS.

Let's deploy your application's updated zip file to the production environment on AWS now:

myapp$ boxfuse run -env=prod

Fusing Image for myapp-1.0.zip ...
Image fused in 00:09.817s (75949 K) -> myuser/myapp:1.0
Pushing myuser/myapp:1.0 ...
Verifying myuser/myapp:1.0 ...
Waiting for AWS to create an AMI for myuser/myapp:1.0 in eu-central-1 (this may take up to 50 seconds) ...
AMI created in 00:34.152s in eu-central-1 -> ami-8b988be7
Creating security group boxsg-myuser-prod-myapp-1.0 ...
Launching t2.micro instance of myuser/myapp:1.0 (ami-8b988be7) in prod (eu-central-1) ...
Instance launched in 00:35.372s -> i-ebea4857
Waiting for AWS to boot Instance i-ebea4857 and Payload to start at http://52.29.129.239/ ...
Payload started in 00:50.316s -> http://52.29.129.239/
Remapping Elastic IP 52.28.107.167 to i-ebea4857 ...
Waiting 15s for AWS to complete Elastic IP Zero Downtime transition ...
Deployment completed successfully. myuser/myapp:1.0 is up and running at http://myapp-myuser.boxfuse.io/ 

 

You may now access your app that was installed on AWS by executing:

myapp$ boxfuse open -env=prod

Frequently Asked Questions

What are the advantages of putting your application in a cloud environment?

The ability for users to easily access data is a cloud-based system's most important benefit.

Is it possible to deploy any application on the cloud?

You can quickly deploy your application across as many regions as your vendor supports if you have the infrastructure in the cloud.

What port does the Play application use by default?

The default configuration is to listen at address 0.0 on port 9000. (all addresses).

Why do we use a play framework?

Scala & Java developers may create web applications quickly and easily with Play Framework. Play's architecture is lightweight, stateless and suitable for the web.

What difficulties arise when deploying cloud applications?

The biggest problems include data loss, issues with confidentiality, and security lapses. Enough comfort will result from the correct application's implementation for cloud migration.

Conclusion

This article has extensively discussed how to deploy a Play Application to a Cloud Service and also covers the key points such as Deploying to Heroku, Deploying to Cloud Foundry, Deploying to Clever Cloud, and Deploying to Cloud Captain and AWS.

Be Curious Image

Visit our website Coding Ninjas Studio to read more interesting blogs and upskill yourself in Android DevelopmentCoding Ninjas Studio ProblemsCoding Ninjas Studio Interview BundleCoding Ninjas Studio Interview ExperiencesCoding Ninjas CoursesCoding Ninjas Studio Contests, and Coding Ninjas Studio Test Series.

You may consider our paid courses to give your career an edge over others!

Happy Learning, ninjas!

Live masterclass