IntelliJ AWS SAM UnicodeDecodeError - java

I'm totally new to AWS and to lambda functions. Currently, I try to start my first "Hello world" program, using IntelliJ and Java. I programmed a Lambda Function in Java using AWS. I want to start it locally with the SAM CLI Builder inside a Maven Container (because I don't have Docker).
After solving some other errors I get this one after hitting start:
SAM Build finished exceptionally: Command did not exist successfully, exit code: 1
Earlier I got a warning, that something is wrong with UTF8 so the code would be somehow platform-dependent, but I have 0 clue honestly.
Java SDK Version
15.0.2
SAM Version
1.18.1
Apache Maven Version
3.6.3
And here is a traceback of the full process:
Traceback (most recent call last):
File "runpy.py", line 194, in _run_module_as_main
File "runpy.py", line 87, in _run_code
File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\__main__.py", line 12, in <module>
cli(prog_name="sam")
File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\click\core.py", line 829, in __call__
return self.main(*args, **kwargs)
File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\click\core.py", line 782, in main
rv = self.invoke(ctx)
File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\click\core.py", line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\click\core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\click\core.py", line 610, in invoke
return callback(*args, **kwargs)
File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\click\decorators.py", line 73, in new_func
return ctx.invoke(f, obj, *args, **kwargs)
File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\click\core.py", line 610, in invoke
return callback(*args, **kwargs)
File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\lib\telemetry\metric.py", line 152, in wrapped
raise exception # pylint: disable=raising-bad-type
File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\lib\telemetry\metric.py", line 121, in wrapped
return_value = func(*args, **kwargs)
File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\lib\utils\version_checker.py", line 42, in wrapped
actual_result = func(*args, **kwargs)
File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\commands\build\command.py", line 160, in cli
do_cli(
File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\commands\build\command.py", line 250, in do_cli
artifacts = builder.build()
File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\lib\build\app_builder.py", line 160, in build
return build_strategy.build()
File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\lib\build\build_strategy.py", line 41, in build
result.update(self._build_functions(self._build_graph))
File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\lib\build\build_strategy.py", line 52, in _build_functions
function_build_results.update(self.build_single_function_definition(build_definition))
File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\lib\build\build_strategy.py", line 116, in build_single_function_definition
result = self._build_function(
File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\lib\build\app_builder.py", line 437, in _build_function
return build_method(config, code_dir, artifacts_dir, scratch_dir, manifest_path, runtime, options)
File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\samcli\lib\build\app_builder.py", line 486, in _build_function_in_process
builder.build(
File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\aws_lambda_builders\builder.py", line 125, in build
return workflow.run()
File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\aws_lambda_builders\workflow.py", line 58, in wrapper
valid_path = binary_checker.validator.validate(executable_path)
File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\aws_lambda_builders\workflows\java_maven\maven_validator.py", line 33, in validate
jvm_mv = self._get_major_version(maven_path)
File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\aws_lambda_builders\workflows\java_maven\maven_validator.py", line 51, in _get_major_version
vs = self._get_jvm_string(maven_path)
File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\aws_lambda_builders\workflows\java_maven\maven_validator.py", line 69, in _get_jvm_string
l_dec = l.decode()
**UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfc in position 25: invalid start byte
SAM Build finished exceptionally: Command did not exist successfully, exit code: 1**

You can write Lambda functions using the Java runtime API and IntelliJ. You can build a JAR file and deploy the Lambda function by using the Lambda console. Once you deploy the Lambda function, you can test it from the Lambda console.
You can log messages to Cloud Watch logs. Using the Lambda java runtime API in the IntelliJ IDE, you will avoid these exceptions. You will code your logic and deploy the JAR file, and it works.
In addition, you can easily use additional AWS Java APIs to invoke AWS services from the Lambda function.
To see a step by step AWS dev article that creates a Lambda function using the Lambda runtime API, see:
Creating scheduled events to invoke Lambda functions

Related

how to avoid IO error while running jar as service

I want to run a customer script which inturn runs multiple python files from my java programs. the script runs fine when in run the jar manually from the terminal using java -jar jarfilename.jar but fails when i run the same jar as service in background.
I'm getting the following error in my debug log
Site:16 RESULT:False:Vmin test completed with no report! Something went wrong.#_#
Site:9 Traceback (most recent call last):
Site:9   File "pexpect/spawnbase.py", line 166, in read_nonblocking
Site:9 OSError: [Errno 5] Input/output error
Site:9 
Site:9 During handling of the above exception, another exception occurred:
Site:9 
Site:9 Traceback (most recent call last):
Site:9   File "pexpect/expect.py", line 111, in expect_loop
Site:9   File "pexpect/pty_spawn.py", line 485, in read_nonblocking
Site:9   File "pexpect/spawnbase.py", line 171, in read_nonblocking
Site:9 pexpect.exceptions.EOF: End Of File (EOF). Exception style platform.
Site:9 
Site:9 During handling of the above exception, another exception occurred:
Site:9 
Site:9 Traceback (most recent call last):`
The above error shows OSError: [Errno 5] Input/output error which from this link linki found it might be due to the print stattement in py file but unfortunately i don't have access to any customer file how can we handle it from java side or any thing when writing a service might fix it?

Learning how to use Jython - ResourceWarning error

I'm trying to learn how to use Jython for a Java Swing project,
I've managed to import the dependency on IntelliJ and I want to test Jython by running some very simple code:
import org.python.util.PythonInterpreter;
public class test {
public static void main(String[] args) {
System.out.println("Java Hello");
PythonInterpreter pi = new PythonInterpreter();
pi.exec("print('Python Hello')");
}
}
The console output is:
Java Hello
Exception in thread "test" Traceback (most recent call last):
File "/Users/a/Desktop/python/Lib/site.py", line 68, in <module>
import os
File "/Users/a/Desktop/python/Lib/os.py", line 50, in <module>
import posixpath as path
File "/Users/a/Desktop/python/Lib/posixpath.py", line 17, in <module>
import warnings
File "warnings.py", line 395, in <module>
File "warnings.py", line 395, in <module>
File "__pyclasspath__/_warnings.py", line 106, in <module>
NameError: name 'ResourceWarning' is not defined
I have not managed to find anything to guide me online,
Any help would be greatly appreciated!
It's an error throws by Python that it can't find Jython library.
As a workaround, add the Jython lib folder to the python classpath.
Then assign python interpreter to your project with updated classpath.
On the pom file, change the scope to: provided.
Cheers,
Mark

Unable to deploy my code to google cloud app engine via gitlab

I am trying to create a gitlab pipeline for deploying my spring boot app to google app engine and i am able to authenticate app engine to gitlab but at deployment step of ci file i am getting Java 7+ JRE must be installed and on your system PATH though i have installed it with gcloud install components app-engine-java command in gitlab ci file. Please suggest what needs to be done to resolve this error.
.gitlab-ci.yml file:
image: google/cloud-sdk:alpine
deploy_production:
stage: deploy
environment: Production
only:
- master
script:
- echo $DEPLOY_KEY_FILE_PRODUCTION > /tmp/$CI_PIPELINE_ID.json
- gcloud -q components install app-engine-java
- gcloud auth activate-service-account --key-file /tmp/$CI_PIPELINE_ID.json
- gcloud --quiet --project $PROJECT_ID_PRODUCTION --verbosity debug app deploy src/main/webapp/WEB-INF/appengine-web.xml
Below are the debug details which mentions that to use the local staging for java, java JRE must be installed though i have installed it using gcloud install components google-app-engine cmd:
$ gcloud --quiet --project $PROJECT_ID_PRODUCTION --verbosity debug app deploy src/main/webapp/WEB-INF/appengine-web.xml
DEBUG: Running [gcloud.app.deploy] with arguments: [--project: "playground-kg", --quiet: "True", --verbosity: "debug", DEPLOYABLES:1: "[u'src/main/webapp/WEB-INF/appengine-web.xml']"]
**DEBUG: (gcloud.app.deploy) To use the local staging for java, a Java 7+ JRE must be installed and on your system PATH**
Traceback (most recent call last):
File "/google-cloud-sdk/lib/googlecloudsdk/calliope/cli.py", line 983, in Execute
resources = calliope_command.Run(cli=self, args=args)
File "/google-cloud-sdk/lib/googlecloudsdk/calliope/backend.py", line 784, in Run
resources = command_instance.Run(args)
File "/google-cloud-sdk/lib/surface/app/deploy.py", line 90, in Run
parallel_build=False)
File "/google-cloud-sdk/lib/googlecloudsdk/command_lib/app/deploy_util.py", line 573, in RunDeploy
args.deployables, stager, deployables.GetPathMatchers())
File "/google-cloud-sdk/lib/googlecloudsdk/command_lib/app/deployables.py", line 334, in GetDeployables
service = Service.FromPath(path, stager, path_matchers)
File "/google-cloud-sdk/lib/googlecloudsdk/command_lib/app/deployables.py", line 102, in FromPath
service = matcher(path, stager)
File "/google-cloud-sdk/lib/googlecloudsdk/command_lib/app/deployables.py", line 175, in AppengineWebMatcher
staging_dir = stager.Stage(descriptor, app_dir, 'java-xml', env.STANDARD)
File "/google-cloud-sdk/lib/googlecloudsdk/command_lib/app/staging.py", line 387, in Stage
return command.Run(self.staging_area, descriptor, app_dir)
File "/google-cloud-sdk/lib/googlecloudsdk/command_lib/app/staging.py", line 171, in Run
args = self.GetArgs(descriptor, app_dir, staging_dir)
File "/google-cloud-sdk/lib/googlecloudsdk/command_lib/app/staging.py", line 251, in GetArgs
return self._mapper(self.GetPath(), descriptor, app_dir, staging_dir)
File "/google-cloud-sdk/lib/googlecloudsdk/command_lib/app/staging.py", line 107, in _JavaStagingMapper
java_bin = java.RequireJavaInstalled('local staging for java')
File "/google-cloud-sdk/lib/googlecloudsdk/command_lib/util/java.py", line 54, in RequireJavaInstalled
v=min_version))
JavaError: To use the local staging for java, a Java 7+ JRE must be installed and on your system PATH
ERROR: (gcloud.app.deploy) To use the local staging for java, a Java 7+ JRE must be installed and on your system PATH
Some people got the same issue trying to create the same workflow, but with github. The same issue can be viewed here. This guy solved this by adding openjdk-8 to Docker.
You can see here how to add openjdk-8 to Docker.
If this did not work for you, please let me know and I will try to edit my answer.
You may also want to check Jenkins for Google Cloud and using Jenkins with Gitlab for CI. It may be helpful for your CI workflow.

Add Java/JRE/JVM in Python Flask Cloud Foundry/IBM Cloud application

I am running a python flask application in a Cloud Foundry/IBM Cloud-environment. In my application I try to connect to DB2 Warehouse on Cloud with the IBMDBPY-package. This packages needs a package called jaydebeapi to be able to run. For the jaydebeapi to work I think I need JRE/JVM somehow installed on the server. I tried adding the Server JRE for Linux based OS, but it didn't work either. My error I got before I tried to upload the Sever JRE was this:
idadb = IdaDataBase(dsn=jdbc) #Establish a connection to our DB2-service
1/20/2018 12:05:45 PM ERR undefined File "/home/vcap/deps/0/python./lib/python2.7/site-packages/ibmdbpy/base.py", line 282, in __init__
1/20/2018 12:05:45 PM ERR undefined jpype.startJVM(jpype.getDefaultJVMPath(), '-Djava.class.path=%s' % jarpath)
1/20/2018 12:05:45 PM ERR undefined File "/home/vcap/deps/0/python/lib/python2.7/site-packages/jpype/_core.py", line 114, in get_default_jvm_path
1/20/2018 12:05:45 PM ERR undefined return finder.get_jvm_path()
1/20/2018 12:05:45 PM ERR undefined File "/home/vcap/deps/0/python/lib/python2.7/site-packages/jpype/_jvmfinder.py", line 121, in get_jvm_path
1/20/2018 12:05:45 PM ERR undefined jvm = method()
1/20/2018 12:05:45 PM ERR undefined File "/home/vcap/deps/0/python/lib/python2.7/site-packages/jpype/_jvmfinder.py", line 164, in _get_from_known_locations
1/20/2018 12:05:45 PM ERR undefined for home in self.find_possible_homes(self._locations):
1/20/2018 12:05:45 PM ERR undefined File "/home/vcap/deps/0/python/lib/python2.7/site-packages/jpype/_jvmfinder.py", line 95, in find_possible_homes
1/20/2018 12:05:45 PM ERR undefined for childname in sorted(os.listdir(parent)):
1/20/2018 12:05:45 PM ERR undefined OSError: [Errno 2] No such file or directory: '/usr/lib/jvm'
1/20/2018 12:05:46 PM OUT undefined Exit status 1
Does anyone know how I can solve this?
After much trial and error, the solution that worked for me was a multi build pack deployment as described below:
cf push -b https://github.com/cloudfoundry/multi-buildpack
and in the root of your project include a multi-buildpack.yml with the following
buildpacks:
- https://github.com/cloudfoundry/apt-buildpack
- https://github.com/cloudfoundry/python-buildpack
and an apt.yml with the following:
---
packages: openjdk-8-jre
repos: deb http://ppa.launchpad.net/openjdk-r/ppa/ubuntu trusty main
keys: https://keyserver.ubuntu.com/pks/lookup?op=get&search=0xEB9B1D8886F44E2A
In the runtime.txt file, (also in the root of the project) is the version of python
python-3.6.6
Unfortunately, JAVA gets installed in your home deps directory and as such you'll have to create a JAVA_HOME environment variable in the manifest.yml.
JAVA_HOME: /home/vcap/deps/0/apt/usr/lib/jvm/java-8-openjdk-amd64/jre/
I also tried to add jre/bin to the path this way
PATH: /bin:/usr/bin:/home/vcap/deps/0/apt/usr/lib/jvm/java-8-openjdk-amd64/jre/bin
However the push wipes this out and installs only the default path /bin;/usr/bin fortunately for me, JAVA_HOME was enough to get jaydebapi to work with the database driver for the jar files I had. If you need this environment variable,
maybe try using the python os package to issue a command to modify the path as part of the startup.
To include a more modern answer. As I write this, most (all worth using) versions of Cloud Foundry you encounter will support multiple buildpacks out-of-the-box. Thus you don't need the multi-buildpack buildpack anymore.
Instead, you can simply cf push and specify multiple buildpacks.
https://docs.cloudfoundry.org/buildpacks/use-multiple-buildpacks.html
This can either be done by setting multiple -b flags to cf push or by using a manifest.yml file and doing something like this:
...
buildpacks:
- buildpack_1
- buildpack_2
...
In either case, the execute in the order you list them.
https://docs.cloudfoundry.org/devguide/deploy-apps/manifest-attributes.html#buildpack
The rest of the answer remains the same as #lamonaki's answer.
Invoke both the apt-buildpack and the Python buildpack, in that order.
Add the apt.yml file and in it, indicate the Java package you'd like to install.
Ex from #lamonaki's answer:
---
packages: openjdk-8-jre
repos: deb http://ppa.launchpad.net/openjdk-r/ppa/ubuntu trusty main
keys: https://keyserver.ubuntu.com/pks/lookup?op=get&search=0xEB9B1D8886F44E2A
Add runtime.txt to set the Python version you'd like to install
Add a .profile file to the root of the project, just like apt.yml and runtime.txt. In that, add lines for export JAVA_HOME=/home/vcap/deps/0/apt/usr/lib/jvm/java-8-openjdk-amd64/jre/ and export PATH=$PATH:$JAVA_HOME/bin and potentially LD_LIBRARY_PATH if you need to reference any custom shared libraries in your Java or Python code.
You might be thinking, why apt-buildpack instead of the Java buildpack. Unfortunately, the Java buildpack in its current incarnation only supports running as a final buildpack (i.e. the last buildpack in the list of buildpacks). That rules it out as a good candidate here since you want the Python buildpack to be last. The Java Cloud Native Buildpacks will resolve the issue, but as I write this, there are no Cloud Native Buildpacks which run natively on CF.

I need help to set up Reddit API

I tried to set up the Reddit API in Linux Mint 16, so that I can use it in IntelliJ 13.1.2/Java.
So I used this guide: https://github.com/reddit/reddit/wiki/Install-guide
and started with
$ git clone https://github.com/reddit/reddit.git
next I went to r2
$ cd reddit/r2
, but when I tried to do the next 3 lines:
$ python setup.py build
$ sudo python setup.py develop
$ make
my console gave out
Traceback (most recent call last):
File "setup.py", line 35, in <module>
pkg_resources.require("distribute>=0.6.16")
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 696, in require
needed = self.resolve(parse_requirements(requirements))
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 594, in resolve
raise DistributionNotFound(req)
pkg_resources.DistributionNotFound: distribute>=0.6.16
respectively
Traceback (most recent call last):
File "setup.py", line 35, in <module>
pkg_resources.require("distribute>=0.6.16")
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 696, in require
needed = self.resolve(parse_requirements(requirements))
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 594, in resolve
raise DistributionNotFound(req)
pkg_resources.DistributionNotFound: distribute>=0.6.16
and in the end at the "make" command the console gave out:
Traceback (most recent call last):
File "Makefile.py", line 24, in <module>
from r2.lib.translation import I18N_PATH
File "/home/fox/reddit/r2/r2/lib/translation.py", line 28, in <module>
from babel.messages.extract import extract_javascript
ImportError: No module named babel.messages.extract
Makefile:41: *** Makefile.py failed. aborting. Schluss.
And now I don't have any clue what I should do. It's the first time I use a API and have zero experience with this. It would be very nice if someone of you could help me.
I had the same problem and solved it by running :
easy_install distribute

Categories

Resources