是否可以在没有API网关的情况下使用AWS SAM在Lambda函数前启动基础架构(如SQS队列)?
我只看到sam local invoke "Lambda" -e event.json
和sam local start-api
当我运行我试图从消息队列中读取消息的lambda时,它没有找到下面引用的Message Queue URL:
NotificationFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
Handler: index.handler
Runtime: nodejs8.10
Role: !Sub ${ConsumerLambdaRole.Arn}
Timeout: 10
Environment: # More info about Env Vars: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#environment-object
Variables:
NODE_ENV: 'dev'
MANDRILL_API_KEY: 'PUyIU3DAxJzIlt7KbBE5ow'
SQS_URL: !Ref MessageQueue
MessageQueue:
Type: AWS::SQS::Queue
Properties:
VisibilityTimeout: 60
RedrivePolicy:
deadLetterTargetArn: !Sub ${DeadLetterQueue.Arn}
maxReceiveCount: 10
# this is where any failed messages will go to
DeadLetterQueue:
Type: AWS::SQS::Queue
答案 0 :(得分:0)
正如评论所建议的那样,sam本地调用只会调用模板中您的lambda资源引用的代码-本身不会创建任何资源。
但是,您可以做的是启动模板,然后使用CLI或通过sam本地调用来调用lambda函数(尽管您将需要执行STS角色假设来获取本地调用函数的正确角色)。但是,我假设您要尝试的是本地测试。在这种情况下,您可以执行以下操作:
我个人更喜欢避免本地测试,并且倾向于启动模板并通过假定的凭据在本地调用以进行调试。我倾向于拥有一个集成测试套件,该套件将针对沙盒AWS环境执行以进行测试。就是说,要进行设置需要花费大量的精力。
[1] https://github.com/localstack/localstack
[2] https://github.com/billthelizard/aws-sam-localstack-example
[3] http://www.piotrnowicki.com/python/aws/2018/11/16/aws-local-lambda-invocation/