aws-sam-cli
aws-sam-cli copied to clipboard
Passing CodeUri as a parameter to an application
Hello,
My team has recently started trying to use SAM, specifically with Applications but we've hit a problem and I can't find anyone else talk about it anywhere else. There are a few issues with passing a CodeUri to a function as a !Ref, which is ok - however our issue is slightly different.
We are wondering if it's possible to pass a local CodeUri (hello-world/
) as a parameter into a local application, which then uses that parameter inside a lambda created in that application?
We have tried so far to do exactly that, however when we run sam package
, the CodeUri does not get updated to a valid S3 uri as it does with a function outside an application.
So basically running sam package
will make the CodeUri inside the generated application template become CodeUri
instead of the local path we passed in.
Is there any way to achieve this dynamic application lambda code idea without having to zip and upload our functions before running sam package
?
The code below shows what we are currently trying to do:
template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
sam-app
Sample SAM Template for sam-app
Globals:
Function:
Timeout: 3
Resources:
hello-world:
Type: AWS::Serverless::Application
Properties:
Location: ./application.yaml
Parameters:
CodeUri: hello-world/
application.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
RC-sam-app
RC base SAM App
Globals:
Function:
Timeout: 3
Parameters:
EventName:
Type: String
Default: DefaultEvent
Handler:
Type: String
Default: app.lambdaHandler
CodeUri:
Type: String
BatchSize:
Type: Number
Default: 1
Resources:
hello-world:
Type: AWS::Serverless::Function
Properties:
CodeUri: !Ref CodeUri
Handler: !Ref Handler
Runtime: nodejs8.10
Role: <rolearn>
packaged.yaml (generated after sam package
)
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: 'sam-app2
Sample SAM Template for sam-app
'
Globals:
Function:
Timeout: 3
Resources:
hello-world:
Type: AWS::Serverless::Application
Properties:
Location: https://s3.amazonaws.com/<locationPath>.template
Parameters:
CodeUri: hello-world/
application.template (pushed up to s3)
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: 'RC-sam-app
RC base SAM App
'
Globals:
Function:
Timeout: 3
Parameters:
EventName:
Type: String
Default: DefaultEvent
Handler:
Type: String
Default: app.lambdaHandler
CodeUri:
Type: String
BatchSize:
Type: Number
Default: 1
Resources:
hello-world:
Type: AWS::Serverless::Function
Properties:
CodeUri:
// So this cost hasn't been zipped and pushed to S3
Ref: CodeUri
Handler:
Ref: Handler
Runtime: nodejs8.10
Role: <roleArn>
Environment:
Variables:
eventName:
Ref: EventName
Thank you
We have tried so far to do exactly that, however when we run
sam package
, the CodeUri does not get updated to a valid S3 uri as it does with a function outside an application.So basically running
sam package
will make the CodeUri inside the generated application template becomeCodeUri
instead of the local path we passed in.
We are experiencing exactly this, our functions are uploaded to s3, but we are thus getting an invalid packaged.yml
file. As a workaround, we run a sam build
first, and presumably the new template.yaml
file it creates in the .aws-sam/build/
directory triggers sam package
to do the right thing.
The functions now get CodeUri's pointing to the build directory output so I guess that makes it work.
SAM CLI does not replace parameters during sam package
. Looks like there might be a case for sam build
but typically we do not.
So sam build
can be used but would typically not recommend doing this kind of thing for local directories and instead provide where the code lives directly within the template. As this is how SAM CLI is designed.