Elastic Beanstalk environment customization with configuration files (.ebextensions)
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed Elastic Beanstalk monitoring.
https://cloudaffaire.com/elastic-beanstalk-monitoring/
In this blog post, we will discuss Elastic Beanstalk advanced environment customization with configuration files (.ebextensions).
. ebextensions:
You can add AWS Elastic Beanstalk configuration files (.ebextensions) to your web application’s source code to configure your environment and customize the AWS resources that it contains. Configuration files are YAML- or JSON-formatted documents with a .config file extension that you place in a folder named .ebextensions and deploy in your application source bundle. The option_settings section of a configuration file defines values for configuration options. Configuration options let you configure your Elastic Beanstalk environment, the AWS resources in it, and the software that runs your application.
Next, we are going to create a PHP web application using custom VPC and RDS instance configured using .config file placed in .ebextensions directory.
Prerequisite for this demo:
- One EC2 AWS Linux 2 instance with proper access.
Step 1: Login to the EC2 instance and create your application root directory.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
## Login to the EC2 instance ## Assume assess and set up your project directory by cloning a sample php website cd sh assume_role.sh ## clone 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/MyShoppingListV3 && cd MyShoppingList/MyShoppingListV3 ## Unzip MyShoppingListV3.zip files to your application root directory unzip ../../Elastic-Beanstalk/MyShoppingListV3.zip |
Step 2: Create .ebextentions directory and copy .config and .yml files.
1 2 3 4 5 6 7 8 |
## Create .ebextensions directory in your application root ## Copy the files that will be used to create and configure your custom VPC ## aws_resource.yml file is used to create your custom vpc and subnets ## aws_configuration.config file is used to configure your application to use the custom vpc cd .. cp ../Elastic-Beanstalk/aws_resource.yml . mkdir .ebextensions cp ../Elastic-Beanstalk/.ebextensions/aws_configuration.config .ebextensions/ |
Step 3: Create your custom VPC and get the resource ID’s.
1 2 3 4 5 6 7 8 |
## Create VPC, 2 private and private subnets, EIP and subnet groups for your RDS aws cloudformation create-stack --stack-name myapplicationstack --region ap-south-1 --template-body file://aws_resource.yml ## Check if stack creation completed aws cloudformation list-stack-resources --stack-name myapplicationstack --region ap-south-1 ## Once the stack creation is completed, get the resource id's aws cloudformation list-exports --region ap-south-1 |
Or you can directly copy resources ID’s from AWS CloudFormation console.
Next, we are going to edit the .config file under .ebextensions directory with resource ID’s
Step 4: Edit the .config file with vpc, subnets id’s.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
## Edit the .config file with vpc, subnets id's. vi .ebextensions/aws_configuration.config ------------------------- option_settings: "aws:elasticbeanstalk:container:php:phpini" : document_root : "/MyShoppingListV3" "aws:ec2:vpc" : VPCId : Subnets : ELBSubnets : DBSubnets : ELBScheme : "public" ------------------------- :wq |
Step 5: Create your application environment.
1 2 3 4 5 6 |
## Create an application ## eb init eb init MyShoppingList --platform "PHP 7.2" --region ap-south-1 --tags name=MyShoppingList ## Create environment eb create MyShoppingListENV1 --database --cname MyShoppingList |
Cleanup:
1 2 3 4 5 6 7 8 |
## Terminate your application eb terminate MyShoppingListENV1 --force --all ## Clear application directory cd .. && rm -rf MyShoppingList Elastic-Beanstalk ## Delete your custom VPC aws cloudformation delete-stack --stack-name myapplicationstack --region ap-south-1 |
Hope you have enjoyed this article. With this, we are concluding our introductory series on AWS Elastic Beanstalk. In the next blog post, we will start with a new AWS service.
To get more details on AWS Elastic Beanstalk, please refer below AWS documentation
https://docs.aws.amazon.com/elastic-beanstalk/index.html
Hy not mapping the VPC id in ebextension config file
Dear Pavan,
You have to provide the VPC id that you have created using CloudFormation.
Thanks,
Debjeet