Customize Elastic Beanstalk Environment
Occasionally, you will need to customize environment for applications running in Elastic Beanstalk.
Some of the use cases can be:
Create folder .ebextensions at root of source code. Create file 'downloadPropertiesFile.config' in folder .ebextensions with following content in yaml format:
Deploy war file to elastic beanstalk environment and file from S3 should be downloaded. Make sure that ec2-instace has associated Role (Instance Profile) which has access to S3 bucket.
It is recommended to use Roles/Instance Profiles to access AWS resources from ec2-instance. However, if you cannot assign role to ec2-instance then you can download files using environment variables. Change content of 'downloadPropertiesFile.config' to:
This example also demonstrates on how to access environment variables in .ebextensions files. Elastic beanstalk executes container commands after extracting source code, but before deploying it. It is important to note that Environment Variables are available for container commands but they are not available for other .ebextensions keys such as Files and Commands.
For Further Reading: Container Commands
- Download properties/configuration files, used by application, from S3
- Run customized scripts which make use of environment variables
- Create or modify files/directories
Create folder .ebextensions at root of source code. Create file 'downloadPropertiesFile.config' in folder .ebextensions with following content in yaml format:
container_commands: 010-downloadfile: command: sudo /usr/bin/aws s3 --region eu-west-1 cp s3://${Bucket_Path_To_File_In_S3} /destination_file_pathIf using gradle, add following content in build.gralde to make this .ebextensions part of war file.
war { from('.ebextensions') { into('.ebextensions') } }Add Environment Variable Bucket_Path_To_File_In_S3 to Elastic Beanstalk environment.
Deploy war file to elastic beanstalk environment and file from S3 should be downloaded. Make sure that ec2-instace has associated Role (Instance Profile) which has access to S3 bucket.
It is recommended to use Roles/Instance Profiles to access AWS resources from ec2-instance. However, if you cannot assign role to ec2-instance then you can download files using environment variables. Change content of 'downloadPropertiesFile.config' to:
container_commands: 010-export-accesskey: command: export AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID} 020-export-secretkey: command: export AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY} 030-downloadfile: command: sudo /usr/bin/aws s3 --region eu-west-1 cp s3://${Bucket_Path_To_File_In_S3} /destination_file_pathAnd add following environment variables to Elastic Beanstalk application. These environment variables are not available in shell and this is why we have to export them first in order to download file from S3.
AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY Bucket_Path_To_File_In_S3
This example also demonstrates on how to access environment variables in .ebextensions files. Elastic beanstalk executes container commands after extracting source code, but before deploying it. It is important to note that Environment Variables are available for container commands but they are not available for other .ebextensions keys such as Files and Commands.
For Further Reading: Container Commands
Comments
Post a comment