use elastic-apm jar java agent with aws elastic beanstalk - amazon-web-services

as of now elastic beanstalk allows to upload a single jar file for deployment.
i want to run custom script as per the below command for starting the application.
sudo java -Dlogging.file=/home/ec2-user/server.log -Xmx8192m -javaagent:/home/ec2-user/server/elastic-apm-agent-1.12.0.jar
-Delastic.apm.service_name=service-name
-Delastic.apm.application_packages=com.demo
-Delastic.apm.server_urls=xxx
-Delastic.apm.secret_token=XXX
-jar $server_name-0.0.1-SNAPSHOT.jar
--spring.profiles.active=prod
not sure if we can do it with some config file to specify the path of the java agent if we know the path of the java agent jar

In EB configuration > environment properties, you'd have to set JAVA_TOOL_OPTIONS=-javaagent:/home/ec2-user/server/elastic-apm-agent-1.12.0.jar and the rest of your -D and -Xmx properties in _JAVA_OPTIONS.
You will also need to download the agent using ebextensions config:
files:
"/home/ec2-user/server/elastic-apm-agent-1.12.0.jar":
mode: "000444"
owner: root
group: root
source: "https://<host>/elastic-apm-agent-1.12.0.jar"

Related

eb-cli define docker-compose file when uploading complete source code to elastic beanstalk

is there a way to define a docker-compose file with a different name than docker-compose.yml when deploying a docker application (with full source code) to elastic beanstalk with eb-cli?
Details:
We are currently deploying the test stage of an application to elastic beanstalk by using the eb-cli. This is working without any problem as long as we provide a docker-compose.yml. In that case elastic beanstalk gets the complete source code and builds the images during the deployment. However, since the CI/CD Pipeline of our production stage is also using the docker-compose.yml, we need to rename the file to docker-compose.test.yml. Is there a way to upload the complete source code AND define a docker-compose file when using the eb-cli?
If you are on Amazon Linux 2, you can use the prebuild hook to rename your docker compose file:
# .platform/hooks/prebuild/docker_compose_cp.sh
#!/bin/bash
cp docker-compose.test.yml docker-compose.yml
You should see output in the eb-engine.log during deployment indicating that the script has run:
[INFO] Executing platform hooks in .platform/hooks/prebuild/
[INFO] Following scripts will be executed in order: [docker_compose_cp.sh]

Elastic BeanStalk app deploy post hook not executing my command

I recently was able to get my Laravel app deployed using codepipeline on Elastic Beanstalk but ran into a problem. I noticed that my routes where failing because of php.conf Nginx configuration. I had to add a few lines of code to EB's nginx php.conf file to get it to work.
My problem now was that after every deployment, the instance of the application I modified the php.conf file was destroyed and recreated fresh. I wanted a way to dynamically update the file after every successful deployment. I had a version of the file I wanted versioned with my application and so wanted to create a symlink to that file after deployment.
After loads of research, I stumbled on appDeploy Hooks on Elastic Beanstalk that runs post scripts after deployment so did this
files:
"/opt/elasticbeanstalk/hooks/appdeploy/post/91_post_deploy_script.sh":
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env bash
sudo mkdir /var/testing1
sudo ln -sfn /var/www/html/php.conf.example /etc/nginx/conf.d/elasticbeanstalk/php.conf
sudo mkdir /var/testing
sudo nginx -s reload
And this for some reason does not work. The symlink is not created so my routes are still not working..
I even added some mkdir so am sure the commands in that script runs, none of those commands ran because none of those directories where created.
Please note that if I ssh into the ec2 instance and run the commands there it works. That bash script also exists in the post directory and if I manually run in on the server it works too.
Any pointers to how I could fix this would be helpful. Maybe I am doing something wrong too.
Now I have gotten my scripts to run by following this. However, the script is not running. I am getting an error
2020/06/28 08:22:13.653339 [INFO] Following platform hooks will be executed in order: [01_myconf.config]
2020/06/28 08:22:13.653344 [INFO] Running platform hook: .platform/hooks/postdeploy/01_myconf.config
2020/06/28 08:22:13.653516 [ERROR] An error occurred during execution of command [app-deploy] - [RunPostDeployHooks]. Stop running the command. Error: Command .platform/hooks/postdeploy/01_myconf.config failed with error fork/exec .platform/hooks/postdeploy/01_myconf.config: permission denied
I tried to follow this forum post here to make my file executable by adding to my container command a new command like so:
01_chmod1:
command: "chmod +x .platform/hooks/postdeploy/91_post_deploy_script.sh"
I am still running into the same issue. Permission denied
Sadly, the hooks you are describing (i.e. /opt/elasticbeanstalk/hooks/appdeploy) are for Amazon Linux 1.
Since you are using Amazon Linux 2, as clarified in the comments, the hooks you are trying to use do not apply. Thus they are not being executed.
In Amazon Linux 2, there are new hooks as described here and they are:
prebuild – Files here run after the Elastic Beanstalk platform engine downloads and extracts the application source bundle, and before it sets up and configures the application and web server.
predeploy – Files here run after the Elastic Beanstalk platform engine sets up and configures the application and web server, and before it deploys them to their final runtime location.
postdeploy – Files here run after the Elastic Beanstalk platform engine deploys the application and proxy server.
The use of these new hooks is different than in Amazon Linux 1. Thus you have to either move back to Amazon Linux 1 or migrate your application to Amazon Linux 2.
General migration steps from Amazon Linux 1 to Amazon Linux 2 in EB are described here
Create a folder called .platform in your project root folder and create a file with name 00_myconf.config inside the .platform folder.
.platform/
00_myconf.config
Open 00_myconf.config and add the scripts
files:
"/opt/elasticbeanstalk/hooks/appdeploy/post/91_post_deploy_script.sh":
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env bash
sudo mkdir /var/testing1
sudo ln -sfn /var/www/html/php.conf.example /etc/nginx/conf.d/elasticbeanstalk/php.conf
sudo mkdir /var/testing
sudo nginx -s reload
Commit your changes or reupload the project. This .platform folder will be considered in each new instance creation and your application will deploy properly in all the new instances Amazon Elastic beanstalk creates.
If you access the documentation here and scroll to the section with the title "Application example with extensions" you can see an example of the folder structure of your .platform folder so it adds your custom configuration to NGINX conf on every deploy.
You can either replace the entire nginx.conf file with your file or add additional configuration files to the conf.d directory
Replace conf file with your file on app deploy:
.platform/nginx/nginx.conf
Add configuration files to nginx.conf:
.platform/nginx/conf.d/custom.conf

.war file is not deployed in the Elastic Beanstalk environment

I'm deploying .war file with .ebextensions in EB environment using Jenkins pipeline. The deployment is succeeded and all the commands are executed in at the deployment stage, but when I check the /usr/share/tomcat8/webapps/ROOT/ I only see my .war file is there as it is, without extracting.
What would be the reason for this? and any idea about how to resolve that issue ? Please find my code snippet below.
zip -r app-${BUILD_NUMBER}.zip myapp.war .ebextensions
aws s3 cp myapp.war s3://inc-eb-deployments/inc-batch/myapp.war
Try deploying it to the webapps folder, not webapps/ROOT. Then tomcat should decompress to webapps/myapp.
If you want to deploy your application in the root directory you need to name your build output ROOT.war and place it in the webapps folder.
Once the ebextesnions are setup with .war file, which is being coupled with ebextensions(.zip format). In this case, .war file should be unzipped through the .ebextension file manually. Following is the sample code snippet.
fix_path:
command: "unzip <app-name>.war 2>&1 > /var/log/my_last_deploy.log"
This solution worked on my environment.

Why will this config file which works on an EC2 instance not work on elastic beanstalk?

So I have the below kue.config file that works (ie runs fine) on my EC2 instance, not work when I try to use it as an .ebextension on Elastic Beanstalk?
description "start kue server"
start on filesystem and started networking
stop on shutdown
script
touch /var/log/forever.log
touch /var/log/stat_out.log
touch /var/log/stat_err.log
rm /var/log/forever.log
rm /var/log/stat_out.log
rm /var/log/stat_err.log
NODE_ENV=production forever start --spinSleepTime 10000 -l /var/log/forever.log -o /var/log/stat_out.log -e /var/log/stat_err.log /home/ec2-user/mykue/server.js
/bin/echo 'Server should be started we ran etc/init/kue.config' >> ../home/ec2-user/wearego
end script
I get the following ERROR:
Top level element in configuration file burrokue/.ebextensions/kue.config in application version burrokue3 must be a map. Update the top level element in the configuration file.
You are confusing the .config files up.
The kue.config file you're using is a config file for configuring the Kue service: https://github.com/Automattic/kue
However, the .config files in an .ebextensions folder is not the same. They are for a different purpose: to configure the Elastic Beanstalk service.
https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/ebextensions.html
The file formats are different.
While they both have names ending in .config, the are not the same thing.
In your case, assuming that you already have Kue installed on your Elastic Beanstalk EC2 instances, you can create a .ebextensions file that will instruct Elastic Beanstalk to create your kue.config file.

eb logs display binary data

I have deployed an application using Amazon Elastic Beanstalk and I am using the EB CLI to access my logs.
Until recently when I did eb logs I was able to tail my logs. But now when I issue the same command, eb prints some binary data, for example:
-------------------------------------
/var/log/nginx/access.log-20150906.gz
-------------------------------------
^_<8B>^H^#<90><A5><EB>U^#^C<ED><9D><DF>s<E3><C8>q<C7><DF><F3>W<A8>
.......
As you can see the log file is gzipped. How can I make eb return the logs as text so I can read them?
Can you try eb logs --all as documented here. This will save the logs to a directory.
This problem is caused by elasticbeanstalk default configuration.
At least in 64bit Amazon Linux 2015.09 v2.0.4 running Ruby 2.2 (Puma) platform.
If you login to your instance with eb ssh and run cat /opt/elasticbeanstalk/tasks/taillogs.d/nginx.conf
You'll find out that by default eb logs tails every file in /var/log/nginx/ directory.
So we need to change /var/log/nginx/* to /var/log/nginx/*.log
It can be done by creating .ebextensions/0001_eb_logs.config file in root directory of your project.
File's content:
files:
"/opt/elasticbeanstalk/tasks/taillogs.d/nginx.conf" :
mode: "000644"
owner: root
group: root
content: |
/var/log/nginx/*.log
commands:
rm_old_conf:
command: "rm nginx.conf.bak"
cwd: "/opt/elasticbeanstalk/tasks/taillogs.d/"
ignoreErrors: true