AWS SAM发布/部署流程

时间:2019-01-06 20:21:56

标签: aws-lambda aws-serverless aws-sam-cli

我无法通过sam发布/部署完全掌握流程。我最大的麻烦是我的sam模板声明了AWS::Serverless::Function,而CodeUri参数则迫使我输入s3存储桶url。

我看过一些示例,其中CodeUri只是计算机上代码资源的路径。当我尝试此sam抱怨

  

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

要解决这个问题,我必须

  • 将我的功能的CodeUri更改为模板中代码的根文件夹
  • 进入AWS控制台,删除我的s3存储桶中的资源,否则sam软件包将不会上传
  • 运行sam包以上传我更新的代码资源
  • 复制新的s3资源密钥
  • 回到我的模板中,用新的s3 bucket uri替换CodeUri
  • 运行sam deploy

这是令人讨厌的。

我想念什么?

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

2 个答案:

答案 0 :(得分:1)

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


答案 1 :(得分:0)

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

  1. 生成一个随机数。 (类似于java uuid),也可能是您的git commit号

  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作为参数传递。

相关问题