3

Perhaps this is more than one question. Tried to sign up to the SAM Slack channel, but no success.

I am trying out SAM to build a serverless app. I am used to having a Cloudformation template to describe all the resources needed. Now I am confused as to why SAM's cli asks me to pass s3 bucket where to upload lambda function code. I would normally expect the creation of s3 bucket (with a random name) to be part of the Cloudformation template execution. Is SAM an extension over Cloudformation or is it not?

In my template.yaml I have something like this:

Resources:

  SrcBucket:
    Type: AWS::S3::Bucket

  MyFunction:
    Type: AWS::Serverless::Function
    Properties:
      Timeout: 3
      Runtime: python3.7
      Handler: my.lambda_handler
      CodeUri: my/
      Events:
        ShopifyInstall:
          Type: Api
          Properties:
            Path: /
            Method: get

How do I reference the SrcBucket in CodeUri?

1 Answer 1

4

Well unfortunately no.

The deployment of the SAM template is in two parts one is the package command which basically constructs the zip file and needs an s3 bucket to upload this to. And the deploy command which simply deploys your packaged application just like what cloudformation would do.

I usually have a small bash script with multiple cloudformation stacks one is the helper stack which creates this bucket (And also adds the name in the outputs) and then fetch the name and pass it along to all the other stacks

#Create the Helper stack
echo "---------Create Helper stack ---------"
aws cloudformation deploy  --profile ${profile} --stack-name $helperStack -- 
region ${region} --template-file deployment-helper.yaml

serverlessCodeBucketName="$(aws cloudformation --region ${region} --profile 
${profile} describe-stacks --stack-name $helperStack --query 
'Stacks[0].Outputs[?OutputKey==`CodeBucketName`].OutputValue' --output text)"

aws cloudformation package --profile ${profile}  --region ${region} -- 
template-file template.yaml  --output - 
template-file serverless-output.yaml --s3-bucket 
${serverlessCodeBucketName}

aws cloudformation deploy --profile ${profile}  --stack-name 
${applicationStack} --region ${region} --template-file 
serverless-output.yaml --capabilities 
CAPABILITY_IAM 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I ended up doing it with ansible.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.