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

2 Answers 2

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


1
  • Thanks for formulating my comment as an answer! Sep 19, 2019 at 19:52
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.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.