aws sam publish/deploy 流

aws sam publish/deploy flow

我没有完全掌握与 sam publishing/deploying 的流程。我最大的问题是我的 sam 模板声明了一个 AWS::Serverless::Function 并且 CodeUri 参数迫使我放入一个 s3 存储桶 url.

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

'CodeUri' is not a valid S3 Uri of the form "s3://bucket/key" with optional versionId query parameter.

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

这真是令人讨厌。

我错过了什么?

{ 
    "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"]
            }
        }
    }
  }

您可以使用此工作流程部署 lambda 函数

  1. 生成一个随机数。 (类似于 java uuid)或者它可能是您的 git 提交编号

  2. 上传工件到s3://your_lmabda_code_bucket_name/uuid

  3. 在你的 sam 中,像这样配置 codeuri

    CodeUri:
        Bucket: your_lmabda_code_bucket_name
        Key: !Sub '${uuid}/main.zip'
    
  4. 在部署期间将 uri 作为参数传递。

TemporaryFix 的评论是对此的正确答案。 AWS SAM 正确地将工件上传到 s3,然后生成更新的模板文件。当 运行 sam package 时,您 需要 指定 --template-output-path packaged.yaml,然后此命令将为您的函数生成包含对 s3 存储桶的引用的文件。然后你必须指定 --template-file packaged.yaml when 运行 the 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