How to make a HTTP rest call in AWS lambda using python? - aws-lambda

To make an http call using python my way was to use requests.
But requests is not installed in lambda context. Using import requests resulted in module is not found error.
The other way is to use the provided lib from botocore.vendored import requests. But this lib is deprecated by AWS.
I want to avoid to package dependencies within my lambda zip file.
What is the smartest solution to make a REST call in python based lambda?

Solution 1)
Since from botocore.vendored import requests is deprecated the recomended way is to install your dependencies.
$ pip install requests
import requests
response = requests.get('https://...')
See also. https://aws.amazon.com/de/blogs/developer/removing-the-vendored-version-of-requests-from-botocore/
But you have to take care for packaging the dependencies within your lambda zip.
Solution 2)
My preferred solution is to use urllib. It's within your lambda execution context.
https://repl.it/#SmaMa/DutifulChocolateApplicationprogrammer
import urllib.request
import json
res = urllib.request.urlopen(urllib.request.Request(
url='http://asdfast.beobit.net/api/',
headers={'Accept': 'application/json'},
method='GET'),
timeout=5)
print(res.status)
print(res.reason)
print(json.loads(res.read()))
Solution 3)
Using http.client, it's also within your lambda execution context.
https://repl.it/#SmaMa/ExoticUnsightlyAstrophysics
import http.client
connection = http.client.HTTPSConnection('fakerestapi.azurewebsites.net')
connection.request('GET', '/api/Books')
response = connection.getresponse()
print(response.read().decode())

Related

What's the best way to reload python query module during development?

I'm working on my first query module for Memgraph. I'm trying to find the best approach.
I have a query module that depends on a local submodule (firstone):
import firstone
import mgp
import importlib
#mgp.read_proc
def procedure(context...):
importlib.reload(firstone)
firstone.call()
If I get it correctly, if firstone module changes, the procedure is still using the previous code. How can I reload the Python query module during development?
You can use the use mg.load to load or reload the given module. The correct syntax is CALL mg.load("py_example");. After loading the module, all dependent Python's submodules that are imported will also be reloaded. The official documentation can be found on Memgraph site.

How do I stub a dependency within Cypress/Sinon without actually importing the file?

Within my Cypress tests I would like to use the endpoints that I generate from our swagger documentation. However, these endpoints use a custom Axios client, that uses Redux and a ton of other dependencies. I can mock this client, but only by using:
import * as CustomAxios from "libs/custom";
cy.stub(CustomAxios, "customAxios");
However, within this file "custom.ts", I have the following code:
export const customAxios = createCustomAxiosClient()
It seems I cannot mock this file, without importing the file, and calling this method (which creates errors). Does anyone have an idea how I can mock this customAxios client without having the createCustomAxiosClient() method being called (which I cannot remove).
I tried reading the documentation and other questions, but I could not find any answers to my specific case.

Cannot import package in AWS lambda with Nodejs14.x ES module

I have a layer where the path of node_modules is nodejs/node14/node_modules.
Using that layer, and I try to import a package in a Lambda function, say 'aws-cloudfront-sign', like this:
import cfsign from 'aws-cloudfront-sign'
I got error message
Cannot find package 'aws-cloudfront-sign' imported from /var/task/signer.js\nDid you mean to import aws-cloudfront-sign/lib/cloudfrontUtil.js?
But if I import the package like this:
import cfsign from '/opt/nodejs/node14/node_modules/aws-cloudfront-sign/lib/cloudfrontUtil.js'
It succeeds.
Do you know why? How could I import the package correctly?
This appears to be a bug. It is occurring with layers and the SDK. There are are a number of similar open issues on Github:
Nodejs Lambda: Cannot find package 'aws-sdk'
Cannot find package when using ES Module and Lambda Layer
ES6 imports don't work in #aws-sdk/client-iotsitewise
As you have worked out, the only workaround at present seems to be the use of absolute paths. E.g.:
import { DynamoDB } from 'aws-sdk;'
fails, whereas
import AWS from '/var/runtime/node_modules/aws-sdk/lib/aws.js';
const { DynamoDB } = AWS;
will work.
I suggest you add your voice to an existing open issue to help ensure it gets attention.

How to create proxy for golang package sources

Let's say I have all source code for module hosted on github.com/example/my-module. But I want to create a proxy hosted on example.com/my-module to redirect all requests to github.com/example/my-module and use my custom domain name in imports:
package main
import "example.com/my-module"
instead of:
package main
import "github.com/example/my-module"
According to docs, I can host git repository on example.com/my-module, but it would be overhead for me.
How can I set up package source code proxy for Go on custom domain? Can I just use HTTP reverse proxy for that or it doesn't work?
If you just need it for yourself, or your package's users are open to an extra step to including the package in the go.mod, you could use the "replace" function of the mod file: https://thewebivore.com/using-replace-in-go-mod-to-point-to-your-local-module/

import path issue when a package and the main use a common third package

I have several microservices written in golang, and wanted to provide some common services. In this example, I have a common go-restful filter that does authorization of API calls. The compilation error indicates that the auth package and the main package are importing the underlying go-restful from different places.
Is there a nice way to resolve this without collapsing the code into one repository?
./main.go:104: cannot use AUTHZ.AuthorizeFilter()
(type "github.com/Solinea/gsk-common/auth/vendor/github.com/emicklei/go-restful".FilterFunction) as type
"github.com/Solinea/gsk-analytics/vendor/github.com/emicklei/go-restful".FilterFunction
in argument to "github.com/Solinea/gsk-analytics/vendor/github.com/emicklei/go-restful".Filter

Resources