Elastic Beanstalk Command Line Interface (EB CLI)
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed environment configuration options in Elastic Beanstalk.
https://cloudaffaire.com/environment-configuration-options-in-elastic-beanstalk/
In this blog post, we will discuss command-line interface (EB CLI) in Elastic Beanstalk.
Elastic Beanstalk Command Line Interface (EB CLI):
The EB CLI is a command-line interface for Elastic Beanstalk that provides interactive commands that simplify creating, updating and monitoring environments from a local repository. You can use EB CLI as part of your everyday development and testing cycle as an alternative to the AWS Management Console.
Next, we are going to create an Elastic Beanstalk application using EB CLI.
Prerequisite for this demo:
- One EC2 AWS Linux 2 instance with proper access.
Step 1: Install and configure EB CLI.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
## Create an EC2 instance with AWS Linux 2 AMI ## Create a role with admin access and attach to your instance ## Create a script to configure AWS cli vi assume_role.sh ----------------------- ## Replace #!/bin/bash curl http://169.254.169.254/latest/meta-data/iam/security-credentials/ export AWS_ACCESS_KEY_ID=$(cat cred.json| jq .AccessKeyId | xargs) export AWS_SECRET_ACCESS_KEY=$(cat cred.json| jq .SecretAccessKey| xargs) export AWS_SESSION_TOKEN=$(cat cred.json| jq .Token| xargs) export AWS_EXPIRATION=$(cat cred.json| jq .Credentials.Expiration| xargs) rm -f cred.json ------------------------ :wq ## Install and update packages sudo yum update -y sudo yum install jq -y sudo yum install git -y sudo yum install python-pip -y ## Execute the script to get access chmod +x assume_role.sh sh assume_role.sh ## Check if access is working aws sts get-caller-identity ## Install AWS EB CLI sudo pip install awsebcli ## Check the eb cli version eb --version |
Step 2: Setup your application root directory.
1 2 3 4 5 6 7 8 9 |
## Set up your project directory and clone a sample php website ## clone a public git repo with sample php website git clone https://github.com/CloudAffaire/Elastic-Beanstalk.git ## Create the root directory for your application and get inside it mkdir -p MyShoppingList/MyShoppingListV1 && cd MyShoppingList/MyShoppingListV1 ## Unzip MyShoppingListV1.zip files to your application root directory unzip ../../Elastic-Beanstalk/MyShoppingListV1.zip |
Step 3: Create an application.
1 2 3 4 5 6 7 |
## Create an application ## eb init cd .. eb init MyShoppingList --platform "PHP 7.2" --region ap-south-1 --keyname ## .elasticbeanstalk file created ls -al |
Observe: If you list hidden files, you can observe one file named .elasticbeanstalk automatically created. .elasticbeanstalk file is used by AWS Elastic Beanstalk to save your environment configuration in the form of config.yml file.
Next, we will create a custom configuration for our PHP environment.
Step 4: Create a custom configuration for your environment.
1 2 3 4 5 6 7 8 |
mkdir .ebextensions vi .ebextensions/platform.config -------------------------- option_settings: "aws:elasticbeanstalk:container:php:phpini" : document_root : "/MyShoppingListV1" -------------------------- :wq |
Note: You can create a custom configuration for your environment using .config files located in .ebextensions directory. We have placed our code in MyApplicationV1 directory, hence we are updating the root directory of our application so that it can serve web request from MyShoppingListV1 directory.
Step 5: Create an environment.
1 2 |
## Create an environment eb create MyShoppingListENV1 --single --cname MyShoppingList |
Our application successfully created.
You can check your AWS Elastic Beanstalk console to validate the same.
Next, execute some basic eb cli commands to get your environment details.
Step 5: EB CLI basic commands.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
## See the current status of your environment eb status ## See the current health status of your environment eb health ## See the current events of your environment eb events ## Prints all the environment properties eb printenv MyShoppingListENV1 ## Displays the versions of the application eb appversion ## List all environments eb list --verbose |
Next, we are going to deploy a new environment version to this existing application.
Step 6: Deploy a new environment version.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
## Deploy a new application version ## Create the new root directory for your application and get inside it mkdir MyShoppingListV2 && cd MyShoppingListV2 ## Unzip MyShoppingListV2.zip files to your application root directory unzip ../../Elastic-Beanstalk/MyShoppingListV2.zip ## Set web server root directory to point to MyShoppingListV2 directory cd .. vi .ebextensions/platform.config -------------------------- option_settings: "aws:elasticbeanstalk:container:php:phpini" : document_root : "/MyShoppingListV2" -------------------------- :wq ## Deploy the new code eb deploy |
Our new application environment version created successfully.
Cleanup (Do not terminate if you continue with the next blog demo):
1 2 3 4 5 |
## Terminate your application eb terminate MyShoppingListENV1 --force --all ## Clear application directory cd .. && rm -rf MyShoppingList Elastic-Beanstalk |
Hope you have enjoyed this article. In the next blog post, we will saved configuration in Elastic Beanstalk.
To get a complete list of EB CLI commands, please refer below AWS documentation.
https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb3-cmd-commands.html
To get more details on AWS Elastic Beanstalk, please refer below AWS documentation
https://docs.aws.amazon.com/elastic-beanstalk/index.html