标签: serverless-framework

Serverless.yml 拒绝 cloudformation “Ref”函数

这是我的 serverless.yml 文件中的一个片段:

Resources:
  LogGroupInfo:
    Type: 'AWS::Logs::LogGroup'
    Properties:
      RetentionInDays: 3
  FirehoseInstance:
     Properties:
      DeliveryStreamName: ${opt:stage}-analytics
      DeliveryStreamType: DirectPut
      RedshiftDestinationConfiguration:
        CloudWatchLoggingOptions:
          Enabled: true
          LogGroupName: !Ref LogGroupInfo
Run Code Online (Sandbox Code Playgroud)

这是我收到的错误:

  unknown tag !<!Ref> in "/Users/code/Project1/serverless.yml" at line 56, column 42:
     ...  LogGroupName: !Ref LogGroupInfo
Run Code Online (Sandbox Code Playgroud)

当在 cloudformation 中用于创建堆栈时,此模板运行良好。为什么 !Ref 被 serverless.yml 拒绝?

serverless-framework serverless

2
推荐指数
1
解决办法
2413
查看次数

fs.readFileSync cannot find file when deploying with lambda

In my code I am calling a query from my lambda function

let featured_json_data = JSON.parse(fs.readFileSync('data/jsons/featured.json'))
Run Code Online (Sandbox Code Playgroud)

This works locally because my featured.json is in the directory that I am reading from. However when I deploy with serverless, the zip it generates doesn't have those files, I get a

ENOENT: no such file directory, open...

I tried packaging by adding

package: 
include: 
 - data/jsons/featured.json
Run Code Online (Sandbox Code Playgroud)

but it just doesn't work. The only way I get this to work is manually adding the …

json amazon-web-services node.js aws-lambda serverless-framework

2
推荐指数
1
解决办法
3761
查看次数

elasticache redis - python - 连接超时

我有一个相当简单的测试应用程序:

import redis
import os
import logging

log = logging.getLogger()
log.setLevel(logging.DEBUG)

def test_redis(event, context):
    redis_endpoint = None
    if "REDIS" in os.environ:
        redis_endpoint = os.environ["REDIS"]
        log.debug("redis: " + redis_endpoint)
    else:
        log.debug("cannot read REDIS config environment variable")
        return {
            'statusCode': 500
        }

    redis_conn = None
    try:
        redis_conn = redis.StrictRedis(host=redis_endpoint, port=6379, db=0)
        redis_conn.set("foo", "boo")
        redis_conn.get("foo")
    except:
        log.debug("failed to connect to redis")
        return {
            'statusCode': 500
        }
    finally:
        del redis_conn
        return {
            'statusCode': 200
        }
Run Code Online (Sandbox Code Playgroud)

我已将其部署为无服务器的 HTTP 端点

#
# For full config options, …
Run Code Online (Sandbox Code Playgroud)

python redis amazon-elasticache serverless-framework

2
推荐指数
1
解决办法
3154
查看次数

无服务器复制用户池而不是按名称重用

我正在使用无服务器来部署 AWS CloudFormation 模板和一些功能,这是我的 serverless.yml 文件的一部分:

resources:
  Resources:
    MyUserPool: #An inline comment
      Type: "AWS::Cognito::UserPool"
      Properties:
        UserPoolName: "MyUserPool"
        Policies:
          PasswordPolicy: 
            MinimumLength: 7
            RequireLowercase: false
            RequireNumbers: true
            RequireSymbols: false
            RequireUppercase: false
functions:
  preSignUp:
    handler: presignup.validate
    events:
      - cognitoUserPool:
          pool: "MyUserPool"
          trigger: PreSignUp
Run Code Online (Sandbox Code Playgroud)

如您所见,两个用户池名称相同,但是当我运行无服务器部署时,会创建 2 个具有相同名称的用户池。

在此处输入图片说明

这是一个错误还是我错过了什么?

amazon-web-services aws-cloudformation serverless-framework serverless

2
推荐指数
2
解决办法
805
查看次数

dynamodb 如何在 serverless.yml 中定义无键模式?

我尝试在我的无服务器 aws lambda 中应用 dynamodb。我的文件是这样的:

resources:
  Resources:
    StoreDynamoDbTable:
      Type: 'AWS::DynamoDB::Table'
      DeletionPolicy: Retain
      Properties:
        AttributeDefinitions:
          - AttributeName: id
            AttributeType: S
          - AttributeName: lat
            AttributeType: N 
          - AttributeName: lng
            AttributeType: N
        KeySchema:
          - AttributeName: id
            KeyType: HASH
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1
        TableName: ${self:provider.environment.TableStore}
Run Code Online (Sandbox Code Playgroud)

我尝试应用 lat 和 lng 作为 storeTable 的属性,只是属性不是 key Schema,但每个 store 元素都应该具有这些属性。

但是有错误:

发生错误:StoreDynamoDbTable - Property AttributeDefinitions 与表的 KeySchema 和二级索引不一致。

如何使 lat 和 lng 只是桅杆属性,而不是索引的关键元素?

amazon-dynamodb serverless-framework serverless

2
推荐指数
1
解决办法
1578
查看次数

使用 AWS Cognito 的无服务器框架生成 CORS 错误

我从 Angular 前端收到此错误消息,但我无权接触我的 lambda 代码:

`Access to fetch at 'https://testapicd.***.***.com/localization/v1/role' from origin 'https://localization.test.***.***.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.`
Run Code Online (Sandbox Code Playgroud)

我到处找,我的代码中似乎没有错误。我的无服务器代码是

  getrole:
    handler: v1/handler_get_role.get_role
    name: get_role
    events:
      - http:
          path: v1/role
          method: get
          cors: true
          authorizer:
            name: CognitoCSAuthorizer
            type: COGNITO_USER_POOLS
            arn: ${file(config.${self:provider.stage}.json):userpoolarn}
Run Code Online (Sandbox Code Playgroud)

我已经三重检查了所有设置,一切似乎都正确。有什么建议该怎么做?该功能在开发环境中有效,但在我将其部署到测试环境时无效。

如果我直接针对 API 尝试令牌,那么它也不起作用(但在开发中运行良好)。我什至不再相信这是 CORS 问题。我认为 jwt 令牌是错误的。

def get_role(event, …
Run Code Online (Sandbox Code Playgroud)

python cors amazon-cognito aws-lambda serverless-framework

2
推荐指数
1
解决办法
1977
查看次数

向离线运行的 lambda 发出请求时如何使用计算机 IP 地址?

嗨,我正在尝试使用我计算机的本地 IP 地址访问我的 lambda,但我反复收到Connection refused错误消息。

但是,如果我使用:

http://127.0.0.1:3000/my-path

端点工作正常。

有没有人对可能出现的问题有任何建议?

谢谢!

ip-address amazon-web-services serverless-framework serverless-offline

2
推荐指数
1
解决办法
499
查看次数

如何删除无服务器仪表板?

我决定尝试新的无服务器框架仪表板,现在我无法摆脱它。它正在获取我的 CloudWatch 日志并要求我登录才能进行部署,这太烦人了!

有谁知道如何删除它?我只能在他们的文档中找到如何安装它...

serverless-framework

2
推荐指数
1
解决办法
585
查看次数

AWS 无服务器、CloudFormation:错误,尝试将非字符串值填充到变量的字符串中

我正在使用无服务器框架在 AWS 云上部署我的应用程序。

https://serverless.com/
Run Code Online (Sandbox Code Playgroud)

我想在 serverless.yml 文件中使用 AWS 账户 ID 的值,并且我想将账户 ID 作为环境变量导出,以便可以从 Lambda 函数访问它。

基于这个 lambda 函数的值,我想创建一些资源(比如 IAM 角色等),这些资源引用这个 accountId 变量。

但是当我尝试部署堆栈时,出现以下错误,

尝试将非字符串值填充到变量 ${self:custom.accountId} 的字符串中。请确保该属性的值是一个字符串。

我的 Serverless.yml 文件如下

custom:
  accountId : !Ref "AWS::AccountId"


provider:
  name: aws
  runtime: go1.x
  stage: dev

  region: us-east-1

  environment:
     ACCOUNT_ID : ${self:custom.accountId}       
     myRoleArn: arn:aws:iam::${self:custom.accountId}:role/xxxxxxxx
Run Code Online (Sandbox Code Playgroud)

有什么办法可以引用 serverless.yml 文件中的 Account Id 的值吗?

amazon-web-services aws-cloudformation serverless-framework aws-serverless

2
推荐指数
1
解决办法
3781
查看次数

如何从 AWS Lambda 函数访问 YAML 文件中的变量?

我正在使用无服务器框架来测试我的 lamdba 函数,但它要求我访问我的 yaml 文件以获取特定变量。为了清楚起见,我将添加文件和下面的代码:

我需要访问的 YAML 变量是 tableName

environment:
  tableName: notes_project
Run Code Online (Sandbox Code Playgroud)

这是 lambda 函数:

import json
import boto3
import uuid

dynamodb = boto3.resource('dynamodb')

table = dynamodb.Table('notes_project')

def lambda_handler(event, context):

    data = json.loads(event['body'])

    item = {
        'user_id': event['requestContext']['identity']['cognitoIdentityId'],
        'note_id': str(uuid.uuid1()),
        'note_text': data
    }

    table.put_item( Item = item)
Run Code Online (Sandbox Code Playgroud)

目前,您可以看到表名是硬编码的,但我想避免这种情况。

到目前为止我尝试过的:

我已经查看了在 python获取 yaml 键值和其他涉及 os.environ 的建议,但如果我要将代码部署到 AWS,我不确定这些是否是正确的选项。我对 lambda 代码在部署后如何访问这些 yaml 变量几乎一无所知。一个解释将不胜感激。

python aws-lambda serverless-framework

2
推荐指数
1
解决办法
1584
查看次数