Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can SAM create the s3 bucket to store the lambda function code?

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?

like image 890
jbasko Avatar asked Sep 18 '26 03:09

jbasko


1 Answers

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 
like image 81
Gaurav Lanjekar Avatar answered Sep 21 '26 11:09

Gaurav Lanjekar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!