Search code examples
aws-lambdaaws-serverlessaws-sam-cli

aws sam publish/deploy flow


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

Solution

  • 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