【问题标题】:aws sam publish/deploy flowaws sam 发布/部署流程
【发布时间】:2019-06-01 14:22:07
【问题描述】:

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

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

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

要解决这个问题,我必须

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

这真是令人讨厌。

我错过了什么?

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

【问题讨论】:

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


    【解决方案1】:

    您可以使用此工作流程来部署 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 作为参数传递。

    【讨论】:

    【解决方案2】:

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

    类似:

    
    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
    
    
    

    【讨论】:

    • 感谢您将我的评论作为答案!
    猜你喜欢
    • 2021-11-03
    • 2021-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    • 2021-11-15
    • 2020-12-20
    • 1970-01-01
    相关资源
    最近更新 更多