我正在尝试使用IBM OpenWhisk.它有自己的CLI,但我想用一个简单的"echo"样本curl.
curl -d-data '{"message": "hellow world"}' https://user:password@openwhisk.ng.bluemix.net:443/api/v1/namespaces/whisk.system/action/samples/echo
curl: (6) Couldn't resolve host '"message": "hello world"'
{
"error": "HTTP method not allowed, supported methods: GET",
"code": 81107
}
Run Code Online (Sandbox Code Playgroud)
如何通过curl或类似工具调用OpenWhisk,如何进行身份验证?
curl serverless-framework openwhisk ibm-cloud-functions ibm-cloud
我想使用API Gateway + Lambda做一个csv下载链接。但是存在一个问题,lambda总是返回JSON.stringify。有办法解决吗?
s-function.json
"responses": {
"default": {
"statusCode": "200",
"responseParameters": {
"method.response.header.Content-disposition": "'attachment; filename=testing.csv'"
},
"responseTemplates": {
"text/csv": ""
}
}
}
Run Code Online (Sandbox Code Playgroud)
handler.js
var json2csv = require('json2csv');
module.exports.handler = function(event, context, cb) {
var fields = ['car', 'price', 'color'];
var myCars = [
{
"car": "Audi",
"price": 40000,
"color": "blue"
}, {
"car": "BMW",
"price": 35000,
"color": "black"
}, {
"car": "Porsche",
"price": 60000,
"color": "green"
}
];
var csv = json2csv({ data: myCars, fields: fields });
return …Run Code Online (Sandbox Code Playgroud) 我使用无服务器来实现Lambda和Api网关。当我实现错误处理时,下面的代码总是会得到502错误的网关。
handler.js
module.exports.hello = (event, context, callback) => {
const response = {
statusCode: 400,
headers: {
"Content-Type" : "application/json"
},
body: JSON.stringify({
"status": "error",
"message": "Missing Params"
})
};
callback(response);
};
Run Code Online (Sandbox Code Playgroud)
CloudWatch会记录错误。
{
"errorMessage": "[object Object]"
}
Run Code Online (Sandbox Code Playgroud)
我通过遵循AWS博客下面的“自定义错误对象序列化”方法来进行这种编码。 参考
lambda amazon-web-services aws-api-gateway serverless-framework
我有一个用户表和一个请求表。一个用户的许多请求。我想在用户表中有一个请求列表。但我不确定如何编写云形成调用来构建它。目前我只有一组平面属性:
resources:
Resources:
DynamoDbTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: Employee
AttributeDefinitions:
- AttributeName: employeeid
AttributeType: S
- AttributeName: name
AttributeType: S
- AttributeName: requests
AttributeType: S
KeySchema:
- AttributeName: employeeid
KeyType: HASH
Run Code Online (Sandbox Code Playgroud)
我希望请求是用户的请求 ID 列表,而不是字符串值,所以没有 S 类型,所以我可以循环浏览它们并调用我想要的。让我知道我的架构是否正常。提前致谢。
我正在尝试为使用AWS Lambda的应用程序编写无服务器后端,并且遇到标题中的错误。使用API Gateway代理集成进行测试时会发生错误,但是在Lambda控制台中进行测试时,该功能可以正常工作。
这是错误:
{
"errorMessage":"string indices must be integers",
"errorType":"TypeError",
"stackTrace":[
[
"/var/task/auth_login.py",
17,
"lambda_handler",
"response = get_user(payload)"
],
[
"/var/task/shifty_utils/__init__.py",
22,
"get_user",
"table = dynamo.Table(user['company'] + '_users')"
]
]
}
Run Code Online (Sandbox Code Playgroud)
这是发生情况的上下文:
def lambda_handler(event, context):
payload = event['body']
response = get_user(payload)
def get_user(user):
try:
table = dynamo.Table(user['company'] + '_users')
response = table.get_item(
Key={
'userId': user['userId'],
'position': user['position']
}
)
except ClientError as e:
print(e.response['Error']['Message'])
return {'message': e.response['Error']['Message']}
else:
return response
Run Code Online (Sandbox Code Playgroud)
基本上,代理集成似乎是以JSON格式的字符串而不是dict的形式读取事件对象,但是如果我为此调整代码,就会发生以下情况:
{
"errorMessage":"the JSON object must be str, bytes …Run Code Online (Sandbox Code Playgroud) 我的凭证文件中有多个AWS配置文件设置
[dev]
aws_key = xx
aws_secret = yy
[qa]
aws_key = aa
aws_secret = bb
[prod]
aws_key = mm
aws_secret = qq
Run Code Online (Sandbox Code Playgroud)
无论如何,在部署时是否会通知无服务器框架要使用哪些凭据?
例如:
serverless deploy --profile prod
Run Code Online (Sandbox Code Playgroud) 我正在使用无服务器框架来设置下表:
provider:
name: aws
runtime: nodejs6.10
stage: dev
region: ap-southeast-1
environment:
DYNAMODB_TABLE: "Itinerary-${self:provider.stage}"
DYNAMODB_COUNTRY_INDEX: country_index
iamRoleStatements:
- Effect: Allow
Action:
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
Resource: "arn:aws:dynamodb:${opt:region, self:provider.region}:*:table/${self:provider.environment.DYNAMODB_TABLE}"
- Effect: Allow
Action:
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
Resource: "arn:aws:dynamodb:${opt:region, self:provider.region}:*:table/${self:provider.environment.DYNAMODB_TABLE}/index/${self:provider.environment.DYNAMODB_COUNTRY_INDEX}"
resources:
Resources:
ItineraryTable:
Type: 'AWS::DynamoDB::Table'
DeletionPolicy: Retain
Properties:
AttributeDefinitions:
-
AttributeName: ID
AttributeType: S
-
AttributeName: Country
AttributeType: S
KeySchema:
-
AttributeName: ID
KeyType: HASH
GlobalSecondaryIndexes: …Run Code Online (Sandbox Code Playgroud) 所以我们目前在最近的项目中使用无服务器,我们的客户端为我们提供了aws iam用户,因为我们将部署到他们的aws资源中.问题是他们不会向用户提供create s3策略.s3存储桶的创建将由他们手动完成.那么在serverless.yml中我可以添加他们创建的s3存储桶并且无服务器将其用作部署存储桶而不是创建新存储桶?
我正在编写供内部使用的API,并且这是我第一次使用无服务器框架。我正在Node.js中编写Lambda函数,并使用AWS API Gateway进行连接。
在某些情况下,我想返回一个自定义错误消息,而我试图编写一个允许我执行此操作的函数。现在,每当Lambda进程失败时,我都会从API收到一条标准消息。在代码中,如果我尝试使用杀死进程process.exit(1),即使我已经使用callback()以下命令返回了错误,也会收到一般错误:
{
"message": "Internal server error"
}
Run Code Online (Sandbox Code Playgroud)
如果不使用process.exit(1),我会callback()在日志中看到通过错误返回的错误,但是该过程继续进行,最终超时:
{
"message": "Endpoint request timed out"
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试了几种使用该callback()方法返回错误的方法,但是到目前为止,我还没有成功。我试过这种方法:
async function return_error(callback, context, error, returnCode){
console.error("FATAL ERROR: ", error);
let ErrorObj = {
errorType : "InternalServerError",
httpStatus : 500,
requestId : context.awsRequestId,
errorMessage : error
}
callback(JSON.stringify(ErrorObj));
process.exit(1);
}
Run Code Online (Sandbox Code Playgroud)
还有这个:
async function return_error(callback, error, returnCode){
console.error("FATAL ERROR: ", error);
callback({
isBase64Encoded: false,
statusCode: returnCode,
headers: { 'Content-Type': 'application/json' …Run Code Online (Sandbox Code Playgroud) node.js aws-lambda aws-api-gateway serverless-framework serverless
有没有人在无服务器框架的认知用户池中设置用户组?我知道我可以在 .yml 文件的资源部分中创建用户池和用户组,但我面临的问题是用户组需要用户池 ID(直到用户池已创建)。我总是可以在没有用户组的情况下进行部署,然后在获得用户池 ID 后使用用户组进行第二次部署,但我很好奇是否有人对如何在一次部署中执行此操作有任何经验或想法。我相信你们中的许多人都遇到过这个问题,但作为参考,我在下面添加了有关资源的无服务器文档以及有关添加用户池和用户组资源的 AWS 文档。谢谢大家的帮助!!
无服务器文档:https : //serverless.com/framework/docs/providers/aws/guide/resources/
用户池文档:https : //docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cognito-userpool.html
用户组文档:https : //docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cognito-userpoolgroup.html
aws-lambda ×4
serverless ×2
csv ×1
curl ×1
ibm-cloud ×1
lambda ×1
node.js ×1
openwhisk ×1
python ×1