7

I'm not fully grasping the flow with publishing/deploying with sam. My biggest hiccup is that my sam template declares a AWS::Serverless::Function and the CodeUri parameter forces me to put in a s3 bucket url.

I've seen examples where the CodeUri is just the path to the code resources on your computer. When I try this sam complains

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

To get around this I have to

  • change my CodeUri for my functions to the root folder of my code in my template
  • go into the AWS console, delete the resources in my s3 bucket, otherwise sam package will not upload
  • run sam package to upload my updated code resources
  • copy the new s3 resource key
  • go back into my template and replace the CodeUri with the new s3 bucket uri
  • run sam deploy

This is painstakingly obnoxious.

What am I missing?

{ 
    "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"]
            }
        }
    }
  }
TemporaryFix
  • 2,008
  • 3
  • 30
  • 54

2 Answers2

3

TemporaryFix's comment is the right answer to this. AWS SAM is correctly uploading artifacts to s3 and then produces the updated template file. You need to specify the --template-output-path packaged.yaml when running sam package, this command will then generate the file with a reference to s3 bucket for your function. You then have to specify --template-file packaged.yaml when running the deploy command

something like:


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


purple
  • 136
  • 1
  • 7
1

You can use this workflow to deploy lambda function

  1. Generate a random number. (similar to java uuid) Or it could be your git commit number

  2. Upload the artifact to s3://your_lmabda_code_bucket_name/uuid

  3. In your sam, make codeuri configurable like this

    CodeUri:
        Bucket: your_lmabda_code_bucket_name
        Key: !Sub '${uuid}/main.zip'
    
  4. Pass the uri as a parameter during deployment.

KyleMit
  • 30,350
  • 66
  • 462
  • 664
best wishes
  • 5,789
  • 1
  • 34
  • 59
  • 2
    I think I actually figured it out. There's an undocumented cli argument for package that specifies the output file when running sam package. If you run sam deploy on the output template sam package generates it's good to go. Link to documented params - https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-cli-command-reference-sam-package.html Link to third param not listed in documentation - https://github.com/awslabs/aws-sam-cli/blob/80314a418d9959c74c1e9f8871cdd4f2668702c8/docs/deploying_serverless_applications.md – TemporaryFix Jan 07 '19 at 15:39