我尝试sam build使用 AWS SAM CLI 运行,但遇到问题。我收到以下错误:
Build Failed
Error: Building image for DefaultFunction requires Docker. is Docker running?
Run Code Online (Sandbox Code Playgroud)
看起来docker虽然安装了,却找不到。当我运行时docker ps,我看到 Docker 正在运行的确认信息。奇怪的是,该build命令实际上对我在同一项目中的同事有效,他们也使用带有 Intel 芯片的 MacOS。
我已经SAM CLI, version 1.60.0安装了
我已经Docker version 20.10.20安装并运行
我正在运行它MacOS Montery 12.5
我已经寻找这个问题的答案有一段时间了,但找不到解决方案。有谁知道如何解决这一问题?我已遵循本文档中的所有步骤。
我正在尝试为无服务器架构进行本地开发设置。亚马逊为此提供了SAM Local Beta。但是在我的项目中,我们正在使用mysql数据库。我试图建立到我的本地mysql服务器的连接,但失败了。
module initialization error:
(2003, "Can't connect to MySQL server on 'localhost'
([Errno 99] Cannot assign requested address)")
Run Code Online (Sandbox Code Playgroud)
以下是我的python代码:
import json
import pymysql
def lambda_sandbox_down_handler(event, context):
rds_host = 'localhost'
name = 'localhost'
password = 'localhost'
db_name = 'localhost'
conn = pymysql.connect(rds_host,port=3306, user=name, passwd=password,
db=db_name,charset='utf8mb4',connect_timeout=5)
return conn
Run Code Online (Sandbox Code Playgroud)
我能够使用命令行以及pycharm建立连接。
我的问题是这可能在SAM本地中。
我已经安装了mysql的docker镜像并启动了它。在此之后,我仍然遇到相同的错误。
我已经成功地使用以下命令在我的机器上的端口 3000 上运行 AWS SAM Local:
sam local start-api
Run Code Online (Sandbox Code Playgroud)
文档说有一个标志-d可以在不同的端口上以调试模式运行服务器。我需要在端口 8080 上运行它。所以我尝试了这个:
sam local start-api -d 8080
Run Code Online (Sandbox Code Playgroud)
并且服务器仍然在 3000 上启动:
Mounting api.get_account (python3.6) at http://127.0.0.1:3000/account/{account_num} [GET]
Run Code Online (Sandbox Code Playgroud)
有谁知道我在这里做错了什么?谢谢!
我正在使用 aws sam local 来开发一些 lambda/nanoservices
我使用以下命令启动 lambda 容器:
sudo sam local start-api --docker-network db-pros
Run Code Online (Sandbox Code Playgroud)
其中 db-pros 是数据库容器
当我转到 127.0.0.1:3000/ lambda-name时一切正常,但是当我尝试从测试设备(连接到 WiFi 的 Android 手机)进行连接时,我不能。
尝试使用 IP(以太网和 docker0),ifconfig但仍然无法连接。
我不知道是否可能,如果可能的话如何。
我需要扫描 dynamodb 数据库,但我不断收到此错误:
"errorMessage": "调用扫描操作时发生错误 (AccessDeniedException):用户:arn:aws:sts::747857903140:assumed-role/test_role/TestFunction 未被授权执行:dynamodb:Scan on resource: arn:aws :dynamodb:us-east-1:747857903140:table/HelpBot"
这是我的 Lambda 代码(index.py):
import json
import boto3
client = boto3.resource('dynamodb')
table = client.Table('HelpBot')
def handler(event, context):
table.scan()
return {
"statusCode": 200,
"body": json.dumps('Hello from Lambda!')
}
Run Code Online (Sandbox Code Playgroud)
这是我的 SAM 模板 (template.yml):
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Resources:
MyFunction:
Type: 'AWS::Serverless::Function'
Properties:
Handler: index.handler
Runtime: python3.6
Policies:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- dynamodb:Scan
Resource: arn:aws:dynamodb:us-east-1:747857903140:table/HelpBot
Run Code Online (Sandbox Code Playgroud) 我正在使用SAM CLI v0.8.1。我正在尝试将环境变量MY_TABLE_VAR设置为资源(MyTableResource)中表的名称。但是,在本地运行我的应用程序时,未定义MY_TABLE_VAR。您能告诉我模板中有什么问题吗,如何正确设置呢?以下是我的SAM模板:
Globals:
Function:
Timeout: 30
Runtime: nodejs8.10
Environment:
Variables:
MY_TABLE_VAR: !Ref MyTableResource
Resources:
MyTableResource:
Type: AWS::Serverless::SimpleTable
Properties:
TableName: table1
PrimaryKey:
Name: id
Type: String
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 5
Run Code Online (Sandbox Code Playgroud) 我正在尝试将 python lambda 部署到 aws。当给定存储桶名称和文件路径时,此 lambda 只会从 s3 存储桶读取文件。如果我运行以下命令,它可以在本地机器上正常工作:
sam build && sam local invoke --event testfile.json GetFileFromBucketFunction
Run Code Online (Sandbox Code Playgroud)
文件中的数据打印到控制台。接下来,如果我运行以下命令,lambda 将被打包并发送到 my-bucket。
sam build && sam package --s3-bucket my-bucket --template-file .aws-sam\build\template.yaml --output-template-file packaged.yaml
Run Code Online (Sandbox Code Playgroud)
下一步是在 prod 中部署,所以我尝试以下命令:
sam deploy --template-file packaged.yaml --stack-name getfilefrombucket --capabilities CAPABILITY_IAM --region my-region
Run Code Online (Sandbox Code Playgroud)
现在可以在 lambda 控制台中看到 lambda,我可以运行它但不返回任何内容,如果我手动将服务角色更改为允许 s3 get/put 的服务角色,则 lambda 可以工作。然而,这破坏了使用 aws sam cli 的全部意义。
我想我需要在 template.yaml 文件中添加一个策略。此链接在这里似乎是说,我要补充的策略,如一个显示在这里。所以,我补充说:
Policies: S3CrudPolicy
Run Code Online (Sandbox Code Playgroud)
在“Resources:GetFileFromBucketFunction:Properties:”下,我重建应用程序并重新部署,部署失败并在 cloudformation 中出现以下错误:
1 validation error detected: Value 'S3CrudPolicy' at 'policyArn' failed to …Run Code Online (Sandbox Code Playgroud) 我的 sam 函数中有以下模板:
Resources:
TagChangedFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: tag_changed_function
Handler: tag_changed/app.lambda_handler
Runtime: python3.8
Policies:
- VPCAccessPolicy: {}
- Statement:
- Sid: EC2DescribeInstancesPolicy
Effect: "Allow"
Action:
- ec2:DescribeInstances
Resource: '*'
VpcConfig:
SubnetIds:
- sg-061328bxxxxx
SecurityGroupIds:
- subnet-03afd77xxxxx
Events:
TagChanged:
Type: CloudWatchEvent
Properties:
Pattern:
source:
- aws.tag
detail-type:
- Tag Change on Resource
Run Code Online (Sandbox Code Playgroud)
(我用 xxxxx 屏蔽了模板中的SubnetIds和)。SecurityGroupIds
但是当我构建并尝试将代码上传到 aws 时,我收到以下错误消息:
2 validation errors detected: Value
'[subnet-061328bxxxxx]' at
'vpcConfig.securityGroupIds' failed to satisfy
constraint: Member must satisfy constraint: [Member must
have …Run Code Online (Sandbox Code Playgroud) 我已按照 AWS 文档中的步骤在本地设置和运行 AWS Step Functions:https: //docs.aws.amazon.com/step-functions/latest/dg/sfn-local-lambda.html。
一切都运行良好,但在步骤 #5 中,它表示您必须创建一个状态机,并且当定义包含大量任务时,使用命令行执行此操作可能会很痛苦。
有没有办法开始执行本地 .asl 文件中定义的状态机?
这是我在本地定义的状态机的示例(从模板):
{
"Comment": "A state machine that does mock stock trading.",
"StartAt": "Check Stock Value",
"States": {
"Check Stock Value": {
"Type": "Task",
"Resource": "${StockCheckerFunctionArn}",
"Retry": [
{
"ErrorEquals": [
"States.TaskFailed"
],
"IntervalSeconds": 15,
"MaxAttempts": 5,
"BackoffRate": 1.5
}
],
"Next": "Buy or Sell?"
},
"Buy or Sell?": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.stock_price",
"NumericLessThanEquals": 50,
"Next": "Buy Stock"
}
],
"Default": "Sell Stock" …Run Code Online (Sandbox Code Playgroud) sam --version
SAM CLI, version 1.0.0
aws --version
aws-cli/1.18.79 Python/3.7.6 Darwin/19.6.0 botocore/1.17.7
Run Code Online (Sandbox Code Playgroud)
问题:
$ aws lambda get-account-settings
'str' object has no attribute 'get'
Run Code Online (Sandbox Code Playgroud)
~/.aws/config完整如下图所示:
[default]
gregion=None
output=json
region=us-east-1
s3=
signature_version=s3v4
[profile my_name]
region=us-east-1
output=json
Run Code Online (Sandbox Code Playgroud)
问题:我的 SAM CLI 是否配置不正确?如何正确配置 Lambda 相关默认设置,例如并发限制和显示帐户设置?
aws-sam-cli ×10
aws-lambda ×4
aws-sam ×3
aws-cli ×1
aws-config ×1
docker ×1
lambda ×1
mysql ×1
python ×1
yaml ×1