我对 sam locals SNS 支持感到困惑。
只能将 Lambda 指向具有 sam 模板的现有 SNS 主题,或者 sam 也可以为我创建该主题吗?
如果可能的话,我非常想做后者。
我可以调用本地 lambda http 端点(sam local start-lambda),但是如何使用(模拟)AWS 事件(s3 等)触发该 lambda 端点,就像使用 Dockerized lambda 环境(sam local invoke)一样-e event.json myLambdaFunction)?
我想在本地运行的 lambda 端点上触发 s3 事件,就像在部署到 AWS 的 lambda 上一样。
我正在使用 AWS CLI 部署 SAM 模板。
AWS Api 名称设置为与 CloudFormation 堆栈名称相同。我希望根据下面的模板内容将 Api 称为“用户”。
可以设置API名称吗?
SAM 模板:
MyApi:
Type: AWS::Serverless::Api
Properties:
Name: Users
StageName: default
Run Code Online (Sandbox Code Playgroud)
使用其他信息进行更新(用于部署的完整模板和 AWS CLI 命令):
模板:
AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Resources:
HelloFunction:
Type: AWS::Serverless::Function
Properties:
Handler: main
Runtime: go1.x
Events:
GetEvent:
Type: Api
Properties:
Path: /
Method: post
#RestApiId: !Ref ApiGateway1
LambdaInvokePermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !GetAtt
- HelloFunction
- Arn
Action: 'lambda:InvokeFunction'
Principal: apigateway.amazonaws.com
SourceAccount: !Ref 'AWS::AccountId'
MyApi:
Type: AWS::Serverless::Api
Properties:
Name: Users
StageName: default
EndpointConfiguration: …Run Code Online (Sandbox Code Playgroud) aws-cloudformation aws-lambda aws-sam-cli aws-serverless aws-sam
我已经部署了一个使用 pg8000 的 lambda (python3.7)
import pg8000
..
def get_connection():
"""
Method to establish the connection.
"""
try:
print ("Connecting to database")
# Create a low-level client with the service name for rds
client = boto3.client("rds")
# Read the environment variables to get DB EndPoint
DBEndPoint = os.environ.get("DBEndPoint")
# Read the environment variables to get the Database name
DatabaseName = os.environ.get("DatabaseName")
# Read the environment variables to get the Database username which has access to database.
DBUserName = os.environ.get("DBUserName")
# …Run Code Online (Sandbox Code Playgroud) 我正在使用aws-sam-cli构建一个 AWS lambda 。在函数中,我想访问某个 DynamoDB 表。我的问题是,当我使用以下sam local invoke命令在本地调用该函数时,该函数会返回此错误:ResourceNotFoundException: Requested resource not found
const axios = require('axios')
const AWS = require('aws-sdk')
AWS.config.update({region: <MY REGION>})
const dynamo = new AWS.DynamoDB.DocumentClient()
exports.handler = async (event) => {
const scanParams = {
TableName: 'example-table'
}
const scanResult = await dynamo.scan(scanParams).promise().catch((error) => {
console.log(`Scan error: ${error}`)
// => Scan error: ResourceNotFoundException: Requested resource not found
})
console.log(scanResult)
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我真的将sam deploy它发送到 AWS 并在实际的 Lambda 控制台中对其进行测试,它会正确记录表信息。
{
Items: <TABLE ITEMS>, …Run Code Online (Sandbox Code Playgroud) 我知道如何调用我已命名且已作为 Lambda 函数存在的 lambda 函数,但是我如何让 FunctionA 调用我在 AWS SAM 模板中一起定义的 FunctionB,并且事先不知道名称,也就是动态的。
有没有办法在创建之前将 FunctionB 的名称作为 SAM 模板的一部分传递,以便模板在创建 FunctionA 之前知道 FunctionB 的全名的名称?
我看到很多关于仅在本地测试的问题
amazon-web-services aws-cloudformation boto3 aws-lambda aws-sam-cli
我正在尝试按照Hello World 示例部署 AWS 无服务器应用程序,但在使用sam deploy --guided. 我在每个提示下按 Enter 以接受教程中的默认值。
对我来说,奇怪的一点是,如果我使用 AWS Toolkit 扩展来部署 VS Code 应用程序,它可以正常工作,所以我认为这与我的 IAM 权限配置没有任何关系,但我是新手对此,我不排除它。
我使用 sam cli 来创建一个项目。当我打包并部署时,它会默认创建 lambda 以及具有阶段和生产阶段、策略、角色等的 api 网关,而无需在 cloudformation 模板中显式定义(请参阅下面的代码)。由于它自动生成 api 网关,如果我想为下面模板生成的 api 添加 api 密钥或某种授权,我该如何添加/附加?
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
simple-node-api
Sample SAM Template for simple-node-api
Globals:
Function:
Timeout: 3
Resources:
ServerlessHttpApi:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
Auth:
ApiKeyRequired: true # sets for all methods
DefinitionBody:
swagger:2.0
paths:
"/myresource":
post:
x-amazon-apigateway-integration
httpMethod: post
type: aws_proxy
uri: ...
ApiKey:
Type: AWS::ApiGateway::ApiKey
Properties:
Name: !Join ["", [{"Ref": "AWS::StackName"}, "-apikey"]]
Description: "CloudFormation API Key V1"
Enabled: true
GenerateDistinctId: false
Value: abcdefg123456 …Run Code Online (Sandbox Code Playgroud) 我正在使用带有 Spring Boot 应用程序的 aws 无服务器架构。当我在 intellij 中使用 sam build 构建项目时,出现以下错误。
Building codeuri: . runtime: java11 metadata: {} functions: ['MedisproutApiFunction']
Running JavaMavenWorkflow:CopySource
Running JavaMavenWorkflow:MavenBuild
Build Failed
Error: JavaMavenWorkflow:MavenBuild - 'utf-8' codec can't decode byte 0xbb in position 150889: invalid start byte
Run Code Online (Sandbox Code Playgroud)
它没有显示任何其他错误详细信息。如果我删除新进行的代码更改(即使它没有任何编码代码),则不会出现此错误。请帮我解决这个问题。
已经检查过此链接,但没有找到答案。
我想对我的无服务器项目执行自动集成测试。为此,我需要以某种方式获取api端点。已经有用于Serverless框架的插件serverless-stack-output服务。但是我想知道在部署应用程序后如何通过AWS SAM实现类似的目标?
同时,如果我能以某种方式获取我的api的基本URL以及各个端点,那么我就可以连接它们并针对它们执行测试。