aws sam 发布/部署流程

Tem*_*Fix 9 aws-lambda aws-sam-cli aws-serverless

我没有完全掌握使用 sam 发布/部署的流程。我最大的问题是我的 sam 模板声明了 aAWS::Serverless::Function并且 CodeUri 参数强制我输入 s3 存储桶 url。

我见过一些示例,其中 CodeUri 只是计算机上代码资源的路径。当我尝试这个时,萨姆抱怨道

“CodeUri”不是具有可选 versionId 查询参数的“s3://bucket/key”形式的有效 S3 Uri。

为了解决这个问题,我必须

  • 将函数的 CodeUri 更改为模板中代码的根文件夹
  • 进入AWS控制台,删除我的s3存储桶中的资源,否则sam包不会上传
  • 运行 sam package 上传我更新的代码资源
  • 复制新的 s3 资源密钥
  • 返回我的模板并将 CodeUri 替换为新的 s3 存储桶 uri
  • 运行 sam 部署

这是非常令人讨厌的。

我缺少什么?

{ 
    "Description" : "Serverless backend",
    "Transform" : "AWS::Serverless-2016-10-31",
    "Globals" : {
    },
    "Resources" : {
        "db" : {
            "Type": "AWS::RDS::DBInstance",
            "Properties" : {
                "AllocatedStorage": "20",
                "DBInstanceClass": "db.t2.micro",
                "DBName": "nameforthedb",
                "DeleteAutomatedBackups": true,
                "Engine": "postgres",
                "MasterUsername": "masterUserName",
                "MasterUserPassword": "******",
                "PubliclyAccessible": true
            }
        },
        "signIn" : {
            "Type": "AWS::Serverless::Function",
            "Properties": {
                "Handler": "index.signIn",
                "Runtime": "nodejs8.10",
                "CodeUri": "src", <--- complains when this is set to this. Code lives in the src folder. this is fine when I run sam package, but has to be changed to the s3 bucket when running sam deploy
                "FunctionName": "signIn",
                "Events": {
                    "SignIn" : {
                        "Type": "Api",
                        "Properties" : {
                            "Path" : "/signIn",
                            "Method" : "post"
                        }
                    }
                }
            }
        },
        "Auth" : {
            "Type" : "AWS::Cognito::UserPool",
            "Properties": {
                "Schema" : [
                    {
                        "AttributeDataType": "String",
                        "Name": "email",
                        "Mutable": true,
                        "Required": true
                    },
                    {
                        "AttributeDataType": "String",
                        "Name": "family_name",
                        "Mutable": true,
                        "Required": true
                    },
                    {
                        "AttributeDataType": "String",
                        "Name": "given_name",
                        "Mutable": true,
                        "Required": true
                    },
                    {
                        "AttributeDataType": "String",
                        "Name": "houseId",
                        "Mutable": true
                    },
                    {
                        "AttributeDataType": "Boolean",
                        "Name": "owner",
                        "Mutable": true
                    }
                ],
                "UsernameAttributes": ["email"]
            }
        }
    }
  }
Run Code Online (Sandbox Code Playgroud)

小智 4

TemporaryFix 的评论是对此的正确答案。AWS SAM 正确地将构件上传到 s3,然后生成更新的模板文件。您需要--template-output-path packaged.yaml在运行时指定sam package,然后此命令将为您的函数生成包含对 s3 存储桶的引用的文件。然后您必须--template-file packaged.yaml在运行时指定deploy command

就像是:


sam build

sam package --s3-bucket your-bucket --output-template-file packaged.yaml

sam deploy --template-file packaged.yaml \
--region eu-west-1 \
--capabilities CAPABILITY_IAM \
--stack-name your-stack


Run Code Online (Sandbox Code Playgroud)