TemporaryFix
TemporaryFix

Reputation: 2186

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

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

Upvotes: 9

Views: 10627

Answers (2)

best wishes
best wishes

Reputation: 6624

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.

Upvotes: 1

purple
purple

Reputation: 146

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


Upvotes: 4

Related Questions