'serverless invoke -f hello' gives KeyError - serverless-framework

I am following a tutorial in order to learn how to work with the serverless framework. The goal is to deploy a Django application. This tutorial suggests putting the necessary environment variables in a separate yml-file. Unfortunately, following the tutorial gets me a KeyError.
I have a serverless.yml, variables.yml and a handler.py. I will incert all code underneath, together with the given error.
serverless.yml:
service: serverless-django
custom: ${file(./variables.yml)}
provider:
name: aws
runtime: python3.8
functions:
hello:
environment:
- THE_ANSWER: ${self:custom.THE_ANSWER}
handler: handler.hello
variables.yml:
THE_ANSWER: 42
handler.py:
import os
def hello(event, context):
return {
"statusCode": 200,
"body": "The answer is: " + os.environ["THE_ANSWER"]
}
The error in my terminal:
{
"errorMessage": "'THE_ANSWER'",
"errorType": "KeyError",
"stackTrace": [
" File \"/var/task/handler.py\", line 7, in hello\n \"body\": \"The answer is: \" + os.environ[\"THE_ANSWER\"]\n",
" File \"/var/lang/lib/python3.8/os.py\", line 675, in __getitem__\n raise KeyError(key) from None\n"
]
}
Error --------------------------------------------------
Error: Invoked function failed
at AwsInvoke.log (/snapshot/serverless/lib/plugins/aws/invoke/index.js:105:31)
at AwsInvoke.tryCatcher (/snapshot/serverless/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/snapshot/serverless/node_modules/bluebird/js/release/promise.js:547:31)
at Promise._settlePromise (/snapshot/serverless/node_modules/bluebird/js/release/promise.js:604:18)
at Promise._settlePromise0 (/snapshot/serverless/node_modules/bluebird/js/release/promise.js:649:10)
at Promise._settlePromises (/snapshot/serverless/node_modules/bluebird/js/release/promise.js:729:18)
at _drainQueueStep (/snapshot/serverless/node_modules/bluebird/js/release/async.js:93:12)
at _drainQueue (/snapshot/serverless/node_modules/bluebird/js/release/async.js:86:9)
at Async._drainQueues (/snapshot/serverless/node_modules/bluebird/js/release/async.js:102:5)
at Immediate._onImmediate (/snapshot/serverless/node_modules/bluebird/js/release/async.js:15:14)
at processImmediate (internal/timers.js:456:21)
at process.topLevelDomainCallback (domain.js:137:15)
For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.
Get Support --------------------------------------------
Docs: docs.serverless.com
Bugs: github.com/serverless/serverless/issues
Issues: forum.serverless.com
Your Environment Information ---------------------------
Operating System: linux
Node Version: 12.18.1
Framework Version: 2.0.0 (standalone)
Plugin Version: 4.0.2
SDK Version: 2.3.1
Components Version: 3.1.2
The command i'm trying is 'sls invoke -f hello'. The command 'sls deploy' has been already executed succesfully.
I am new to serverless, so please let me know how to fix this, or if any more information is needed.

First of all, there is an error in yml script:
Serverless Error ---------------------------------------
Invalid characters in environment variable 0
The error is defining environment variables as an array instead of key-value pairs
And then after deployment, everything works smoothly (sls deploy -v)
sls invoke --f hello
{
"statusCode": 200,
"body": "The answer is: 42"
}
serverless.yml
service: sls-example
custom: ${file(./variables.yml)}
provider:
name: aws
runtime: python3.8
functions:
hello:
environment:
THE_ANSWER: ${self:custom.THE_ANSWER}
handler: handler.hello
variables.yml
THE_ANSWER: 42

Related

Serverless deploy fails - Error: Write EPIPE

I have used Serverless Framework previously but lately I am having an error I cannot resolve. Serverless Framework version 3.8.0. was installed using npm. Node version 17.7.1. Running on Linux(Manjaro)
Running sls deploy
✔ Service deployed to stack test-api-dev (0s)
But nothing actually changes on AWS, nothing is actually deployed.
Running sls deploy | echo outputs
Running "serverless" from node_modules
✖ Uncaught exception
/home/mascs/api/node_modules/serverless/scripts/serverless.js:49
throw error;
^
Error: write EPIPE
at afterWriteDispatched (node:internal/stream_base_commons:160:15)
at writeGeneric (node:internal/stream_base_commons:151:3)
at Socket._writeGeneric (node:net:817:11)
at Socket._write (node:net:829:8)
at writeOrBuffer (node:internal/streams/writable:390:12)
at _write (node:internal/streams/writable:331:10)
at Socket.Writable.write (node:internal/streams/writable:335:10)
at Object.<anonymous> (/home/mascs/node_modules/#serverless/utils/log-reporters/node.js:50:39)
at Object.emit (/home/mascs/api/node_modules/event-emitter/index.js:97:9)
at /home/mascs/api/node_modules/#serverless/utils/lib/log/get-output-reporter.js:17:27
at module.exports (/home/mascs/api/node_modules/serverless/lib/cli/handle-error.js:81:3)
at finalize (/home/mascs/api/node_modules/serverless/scripts/serverless.js:57:41)
at /home/mascs/api/node_modules/serverless/scripts/serverless.js:772:11
Emitted 'error' event on Socket instance at:
at emitErrorNT (node:internal/streams/destroy:164:8)
at emitErrorCloseNT (node:internal/streams/destroy:129:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
errno: -32,
code: 'EPIPE',
syscall: 'write'
}
Node.js v17.7.1
Running as sudo doesn't change things, so it doesn't seem to be a permissions issue.
Here is the serverless.yml I am using. Can't see any syntax errors.
service: test-api
frameworkVersion: ">=3.0.0 <4.0.0"
provider:
name: aws
runtime: python3.7
region: eu-west-1
stage: dev
environment:
DYN_TABLE: ${self:provider.stage}-test
iamRoleStatements:
- Effect: Allow
Action:
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:BatchGetItem
- dynamodb:UpdateItem
- dynamodb:PutItem
Resource: "arn:aws:dynamodb:${opt:region, self:provider.region}:*:table/${self:provider.environment.DYN_TABLE}"
plugins:
- serverless-python-requirements
- serverless-ignore
package:
individually: false
custom:
pythonRequirements:
dockerizePip: true
slim: true
functions:
lambda-test:
handler: api/main.get
memorySize: 256
timeout: 6
events:
- http:
path: v1/
method: get
cors: true
I am at a bit of a loss here, would appreciate if anyone has any ideas.
Have tried uninstalling and then reinstalling Serverless Framework also but it has made no difference.

How to run a Lambda Docker with serverless offline

I would like to run serverless offline using a Lambda function that points to a Docker image.
When I try to run serverless offline, I am just receiving:
Offline [http for lambda] listening on http://localhost:3002
Function names exposed for local invocation by aws-sdk:
* hello-function: sample-app3-dev-hello-function
If I try to access http://localhost:3002/hello, a 404 error is returned
serverless.yml
service: sample-app3
frameworkVersion: '3'
plugins:
- serverless-offline
provider:
name: aws
ecr:
images:
sampleapp3image:
path: ./app/
platform: linux/amd64
functions:
hello-function:
image:
name: sampleapp3image
events:
- httpApi:
path: /hello
method: GET
app/myfunction.py
def lambda_handler(event, context):
return {
'statusCode': 200,
'body': 'Hello World!'
}
app/Dockerfile
FROM public.ecr.aws/lambda/python:3.9
COPY myfunction.py ./
CMD ["myfunction.lambda_handler"]
at the moment such functionality is not supported in serverless-offline plugin. There's an issue open where the discussion started around supporting this use case: https://github.com/dherault/serverless-offline/issues/1324

I can't manage files from s3 using ansible aws_s3 module

Running the following task on my playbook:
- name: "Check if hold file exists..."
amazon.aws.aws_s3:
region: <region>
bucket: <bucket>
prefix: "hold_jobs_{{ env }}"
mode: list
register: s3_files
I Got the following error:
{
"exception": "Traceback (most recent call last):\n File \"/tmp/ansible_amazon.aws.aws_s3_payload_b92r42e3/ansible_amazon.aws.aws_s3_payload.zip/ansible_collections/amazon/aws/plugins/modules/aws_s3.py\", line 427, in bucket_check\n File \"/var/lib/awx/venv/ansible/lib/python3.6/site-packages/botocore/client.py\", line 357, in _api_call\n return self._make_api_call(operation_name, kwargs)\n File \"/var/lib/awx/venv/ansible/lib/python3.6/site-packages/botocore/client.py\", line 661, in _make_api_call\n raise error_class(parsed_response, operation_name)\nbotocore.exceptions.ClientError: An error occurred (400) when calling the HeadBucket operation: Bad Request\n",
"boto3_version": "1.9.223",
"botocore_version": "1.12.253",
"error": {
"code": "400",
"message": "Bad Request"
}
I am using this configuration:
ansible [core 2.11.7]
config file = /etc/ansible/ansible.cfg
configured module search path = ['/var/lib/awx/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.6/site-packages/ansible
ansible collection location = /var/lib/awx/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/local/bin/ansible
python version = 3.6.8 (default, Apr 16 2020, 01:36:27) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]
jinja version = 2.10.1
libyaml = True
I need help to solve this, thanks in advance. For this moment I am using a paliative solution, with shell module.

How to eliminate serverless framework error Template format error

Testing, learning serverless framework. I'm trying to deploy simple/basic state machine with two simple lambda functions.
Serverless definition as follows:
frameworkVersion: '2'
app: state-machine
org: macdrorepo
service: state-machine
plugins:
- serverless-python-requirements
- serverless-iam-roles-per-function
- serverless-step-functions
- serverless-pseudo-parameters
custom:
pythonRequirements:
dockerizePip: non-linux
slim: true
zip: true
provider:
name: aws
runtime: python3.8
region: eu-central-1
stage: ${opt:stage, 'testing'}
timeout: 30
package:
individually: true
exclude:
- node_modules/**
- .git/**
- .venv/**
functions:
processpurchase:
module: state-machine
memorySize: 128
stages:
- testing
- dev
handler: ProcessPurchase.process_purchase
processrefund:
module: state-machine
memorySize: 128
stages:
- testing
- dev
handler: ProcessRefund.process_refund
stepFunctions:
validate: true
stateMachines:
TransactionChoiceMachine:
name: ChoiceMachineTest-${self:provider.stage}
dependsOn: CustomIamRole
definition:
Comment: "Purchase refund choice"
StartAt: ProcessTransaction
States:
ProcessTransaction:
Type: Choice
Choices:
- Variable: "$.TransactionType"
StringEquals: PURCHASE
Next: PurchaseState
- Variable: "$.TransactionType"
StringEquals: REFUND
Next: RefundState
PurchaseState:
Type: Task
Resource:
Fn::GetAtt: [processpurchase, Arn]
End: true
RefundState:
Type: Task
Resource:
Fn::GetAtt: [processrefund, Arn]
End: true
During deploy, sls is saying my state machine definition is ok: State machine "TransactionChoiceMachine" definition is valid
My environment information:
Your Environment Information ---------------------------
Operating System: linux
Node Version: 12.20.0
Framework Version: 2.14.0
Plugin Version: 4.1.2
SDK Version: 2.3.2
Components Version: 3.4.3
Setup SLS_DEBUG=* is not helping me much as I do not know js unfortunately.
After serverless deploy command, I'm getting error:
Error: The CloudFormation template is invalid: Template format error: Unresolved resource dependencies [CustomIamRole] in the Resources block of the template
Looks like you are referencing something called CustomIamRole in your state machine creation but I cannot see it being created anywhere in the yaml file. Either create the role and use it in creation or remove the depends on part.

phantom JS configuration when used with InternJS

I want to use phantomJS with Intern for functional testing.
I Installed phantomJS and ran the command "phantomjs --webdriver 4444", it ran successfully.
Then tried to run intern-runner with the command "intern-runner config=tests/intern" -file tests/intern is shown at the end of the post-.
but i keep getting the error
intern-runner config=tests/intern
Listening on 0.0.0.0:9000
Tunnel started
Suite phantomjs on any platform FAILED
UnknownError: [POST http: //localhost:4444/wd/hub/session / {"desiredCapabilities":{"browserName":"phantomjs","name":"tests/intern","idle-timeout":60}}] An unknown server-side error occurred while processing the command.
on intern.
and the error
[ERROR - 2015-10-14T10:14:35.611Z] RouterReqHand - _handle.error - {"message":"{
\"headers\":{\"Connection\":\"close\",\"Host\":\"localhost:4444\",\"accept\":\"a
pplication/json,text/plain;q=0.9\",\"content-length\":\"91\",\"content-type\":\"
application/json;charset=UTF-8\",\"user-agent\":\"dojo/2.0.0-dev Node.js/4.1.2\"
},\"httpVersion\":\"1.1\",\"method\":\"POST\",\"url\":\"/session\",\"urlParsed\"
:{\"anchor\":\"\",\"query\":\"\",\"file\":\"session\",\"directory\":\"/\",\"path
\":\"/session\",\"relative\":\"/session\",\"port\":\"\",\"host\":\"\",\"password
\":\"\",\"user\":\"\",\"userInfo\":\"\",\"authority\":\"\",\"protocol\":\"\",\"s
ource\":\"/session\",\"queryKey\":{},\"chunks\":[\"session\"]}}","name":"Missing
Command Parameter","line":85,"sourceId":65775352,"sourceURL":":/ghostdriver/req
uest_handlers/session_manager_request_handler.js","stack":"Missing Command Param
eter: {\"headers\":{\"Connection\":\"close\",\"Host\":\"localhost:4444\",\"accep
t\":\"application/json,text/plain;q=0.9\",\"content-length\":\"91\",\"content-ty
pe\":\"application/json;charset=UTF-8\",\"user-agent\":\"dojo/2.0.0-dev Node.js/
4.1.2\"},\"httpVersion\":\"1.1\",\"method\":\"POST\",\"url\":\"/session\",\"urlP
arsed\":{\"anchor\":\"\",\"query\":\"\",\"file\":\"session\",\"directory\":\"/\"
,\"path\":\"/session\",\"relative\":\"/session\",\"port\":\"\",\"host\":\"\",\"p
assword\":\"\",\"user\":\"\",\"userInfo\":\"\",\"authority\":\"\",\"protocol\":\
"\",\"source\":\"/session\",\"queryKey\":{},\"chunks\":[\"session\"]}}\n at :
/ghostdriver/request_handlers/session_manager_request_handler.js:85\n at :/gh
ostdriver/request_handlers/session_manager_request_handler.js:44\n at :/ghost
driver/request_handlers/router_request_handler.js:70","stackArray":[{"sourceURL"
:":/ghostdriver/request_handlers/session_manager_request_handler.js","line":85},
{"sourceURL":":/ghostdriver/request_handlers/session_manager_request_handler.js"
,"line":44},{"sourceURL":":/ghostdriver/request_handlers/router_request_handler.
js","line":70}]}
on phantomjs - this error appears after running the intern-runner-.
does anyone know what the problem is?.
p.s. I'm using phantomJS v.1.9.8
tests/intern :
define({
proxyPort: 9000,
proxyUrl: 'http://localhost:9000/',
environments: [
{ browserName: 'phantomjs' }
],
maxConcurrency: 3,
useSauceConnect: false,
loaders: {
'host-browser': 'node_modules/requirejs/require.js'
},
tunnel: 'NullTunnel',
functionalSuites: [ 'tests/functional/modetest' ],
});
PhantomJS 1 is ancient (it uses a version of WebKit from 2012) and not supported. You must use PhantomJS 2+ with Intern.