Using PHP and Git on AWS Elastic Beanstalk
Using PHP and Git on AWS Elastic Beanstalk
Join the DZone community and get the full member experience.Join For Free
Learn how to migrate and modernize stateless applications and run them in a Kubernetes cluster.
Elastic Beanstalk, which launched in January 2011, is a Platform service that simplifies application deployment in AWS. Amazon has recently extended Beanstalk with PHP language support. By adding PHP in Beanstalk, Amazon has made clear their move into the platform service landscape. Amazon wants to evolve as an IaaS that offers flexible PaaS, a direction which will undoubtedly tighten the competition in the platform cloud service market.
When Amazon announced Beanstalk’s support for PHP I was curious to know what it would look like. So I decided to give it a try. I hadn’t used my AWS account for a while, so I had to do some account setup tasks, configuring command line tools, etc. In this article I will explain how to set up a PHP application in Beanstalk from scratch.
Sign Up for AWS
First sign up for an AWS account if you don’t have one already. It will ask you to enter Credit Card details to complete the sign-up process. You will get a verification call before activating your account.
Setup AWS Command line tools
AWS provides a set of command line tools for managing the infrastructure. These tools serve as the interface to all the AWS services . You need to set up AWS Developer tools if you want to use Git for deploying applications. Otherwise you can upload the zipped code and deploy from the console. First download Beanstalk CLI to your machine and unzip it to a location of your choice. Navigate to the elasticbeanstalk-cli folder where you will see elasticbeanstalk-cli and AWSDevTools . Elastic Beanstalk CLI is a set of ruby scripts which don’t need to be installed. But you do need to have Ruby (version > 1.8.7 ), JSON Gem, and Git installed on your machine. To install JSON:
shameer@ubuntu$ gem install json
Next you need to configure CLI tools with AWSAccessKeyId and AWSSecretKey which are located in your account dashboard.
Create an AWS credential file to store the account credentials:
AWSAccessKeyId=<your key id>
AWSSecretKey=<your secret key>
Next you need to export the environment variable to the credential file:
export AWS_CREDENTIAL_FILE=<file path>
Create our first PHP application
To set up our PHP application in Elastic Beanstalk, log in to the AWS console and select the Beanstalk tab where you can find the wizard for creating an application
You can either upload the zipped application folder or launch a demo application from here. To start with, select 32 bit Amazon Linux running PHP 5.3 from the drop down and click Launch Application. It will launch an application named “My First Elastic Beanstalk Application”.
You can click on the View Running Version link to see the demo application running. As I have mentioned previously, you can always update the application by uploading it from the console. It’s easier to use Git for application deployment as you can manage it from the command line. When you develop an application, you’ll be continuously modifying and testing it in a local sandbox; once it gets to a comfortable stage, you deploy it to the server. Using Git you can keep a revision of each of the changes and revert back to an earlier version if something goes wrong.
Using Git for deploying application
Before you can actually use Git for deploying applications from a local repository, you need to setup AWSDevTools in the Git repository.
1. Move to the local repository from command line.
2. If you have not initialized Git already, do it using following command:
shameer@ubuntu:/var/www/beanstalk$ git init
3. Run repository setup script from inside the repository:
shameer@ubuntu:/var/www/beanstalk$ sh /home/elasticbeanstalk-cli/AWSDevTools/Linux/AWSDevTools-RepositorySetup.sh
4. Run the following command to set up AWS credentials for this application:
shameer@ubuntu:/var/www/beanstalk$ git aws.config
5. This will prompt you to enter the AWS access key, secret Key, and details of the application you’ve just launched from the console:
AWS Access Key: <access-key> AWS Secret Key: <secret-key> AWS Region [default to us-east-1]: us-east-1 AWS Host [default to git.elasticbeanstalk.us-east-1.amazonaws.com]: git.elasticbeanstalk.us-east-1.amazonaws.com AWS Elastic Beanstalk Application: My First Elastic Beanstalk Application AWS Elastic Beanstalk Environment: Default-Environment
Remember that the application name and environment should match the name and application environment you were given when creating it from the console. Once you enter these details correctly, the application is ready to deploy using Git. Make the required changes to the application, commit it, and push, done!
shameer@ubuntu:/var/www/beanstalk$ git add . shameer@ubuntu:/var/www/beanstalk$ git commit -m "First commit" shameer@ubuntu:/var/www/beanstalk$ git aws.push
Now, if you look at the application URL from your browser, it shows the updated version of your application. Each version you deploy will be listed in the application dashboard, from there you can easily change it to the version of your choice.
The default application we just created will have a predefined configuration which includes:
- t1.micro EC2 server instance
- Load balancer
- Auto scaling with minimum 1 and maximum 4 instances
You can edit these configurations by clicking on “Edit Configuration” link, which will display the following window.
Beanstalk does not have a bundled database support since it runs on EC2 instance which does not have any persistent storage. You will need to follow separate steps if you want to enable databases for the application.
If your application needs environmental settings would normally be set at the OS level, these can now be set from the “Container tab”. There you can set document root, memory limit, etc.
Beanstalk provides a reliable, scalable, cost effective and easy to use way of application deployment in AWS infrastructure. In-fact, it’s nothing but an abstraction of some existing services like EC2, Load balancer, Auto scaling, and SNS. The advantage of Beanstalk over other platform services is that you have the full control of underlying resources. Though platforms like Heroku and PHPFog provide a much simpler interface for managing applications, we will need to rely on AWS for many other services like, SES, DynamoDb, etc. On the other hand, Beanstalk doesn’t add additional charges so you’ll only be paying for the underlying services you use. What is more interesting is the support for Git – the de facto standard for application deployment in PaaS. Git will make the development workflow faster and easier as you deploy your files to AWS Elastic Beanstalk. The move to support PHP applications and Git repositories is a clear indicator of AWS’ ambitions in the PaaS market.
Published at DZone with permission of Shameer Cee . See the original article here.
Opinions expressed by DZone contributors are their own.