I'm trying the Create Your Own Serverless Application and I see that the name of the function is specified in the YAML template but when it gets deployed it creates a lambda with a composite name based on:
CloudFormation stack + Lambda function + Some Id.
My questions is: Is there a way to override the name of the function when using AWS SAM?
Thanks
Yes there is, take a look at https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
You need the FunctionName parameter in your YAML.
Similar to the following:
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Resources:
samPocFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: samPoc
Description: This is my SAM POC function
Runtime: python2.7
CodeUri: ./functions/mycode
Handler: handler.handler
MemorySize: 128
Timeout: 3
Related
I'm becoming crazy with aws cloud formation and its documentation.
What I'm trying to do is:
create multiple java lambda functions projects, for example getUsers and insertUser, with their own template yaml;
create a root template yaml to deploy all the stack of single inner projects (i.e. single lambda functions).
For the first question the yaml is something like this:
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Resources:
getcounties:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./
Description: Lambda functions that retrieves users
FunctionName: GetUsersTest
Handler: "GetUsersHandler.handleRequest"
MemorySize: 128
ReservedConcurrentExecutions: 5
Role: LambdaBasicExecutionRole
Runtime: java11
Timeout: 30
But I'm not able to perform the second task. Any idea?
Thanks
I have a SAM template that takes parameters in the .yml file, and I want to access the parameter value within the lambda function written in c#
here is the SAM template
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
Globals:
Function:
Timeout: 60
Runtime: dotnetcore2.1
Parameters:
ApiBaseUri:
Type: String
Default: https://postb.in/1584332032935-8906962152104
Description: The API URL where the received events will be pushed by the lambda function
Resources:
PushUpdates:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./src/PushAPIs/
Handler: Localgov.Microservices::LambPaymentTracker.UpdateHandler::PushBurtonUpdates
Runtime: dotnetcore2.1
Policies: AWSLambdaDynamoDBExecutionRole
Events:
Stream:
Type: DynamoDB
Properties:
Stream: !GetAtt UpdatesTable.StreamArn
BatchSize: 100
StartingPosition: TRIM_HORIZON
The common way of passing cloudformation (or sam) variables to lambda would be through Environment Variables.
In your template you specify the variables using Environment property.
In your code, you then use your programming language specific functionality to read them.
I currently have 2 lambda functions, and I'm trying to make a CI/CD process to them. So I have trying with 2 approaches:
two separate steps on my CI. I have tried to make CloudFormation package and then deploy each lambda, with each of those having their own SAM template and template. but the result is that the only one that will remain on the stack is the last one deployed. i understand that deploy is an smart way that AWS CLI create to do not use create/update stack actions. but it keeps overwriting between them (yes they have different resource name).
having a single sam template and one step in a single repo: I also attempt this in a single repo having both lambdas and a single sam file, but I have duplicate code on my lambdas the difference is that each of them have different set up for which handler to be used.
My goal is to have 2 lambdas in a single stack.
im going to answer my own question because I notice on the sam template was the key.
initially i was doing the sam template like this:
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: An AWS Serverless Specification template describing your function.
Resources:
certainty:
Type: 'AWS::Serverless::Function'
Properties:
Handler: ./myfunc/index.handler
Runtime: nodejs8.10
CodeUri: .
Description: >-
here goes
my description
MemorySize: 128
Timeout: 20
Role: 'arn:aws:iam::116738426468:role/rolename'
Events:
Schedule1:
Type: Schedule
Properties:
Schedule: rate(1 day)
certaintyauxiliar:
Type: 'AWS::Serverless::Function'
Properties:
Handler: my-other-func/index.handler
Runtime: nodejs8.10
CodeUri: .
Description: >-
blabla
blabla.
MemorySize: 1152
Timeout: 300
Role: 'arn:aws:iam::116738426468:role/roleanme'
Events:
Api1:
Type: Api
Properties:
Path: /show-all
Method: POST
what was causing here a "duplicate of the code" was that the lambdas code uri was indicating that should grab all the stuff in the folder that cotained both repos. and telling to go deeper in directories to find the the handler.
so I changed the code uri and the handler and now the lambdas are grabbing just what should be in each lambda. now my sam template looks like this:
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: An AWS Serverless Specification template describing your function.
Resources:
certainty:
Type: 'AWS::Serverless::Function'
Properties:
Handler: index.handler
Runtime: nodejs8.10
CodeUri: ./my-func
Description: >-
here goes
my description
MemorySize: 128
Timeout: 20
Role: 'arn:aws:iam::116738426468:role/roleName'
Events:
Schedule1:
Type: Schedule
Properties:
Schedule: rate(1 day)
certaintyauxiliar:
Type: 'AWS::Serverless::Function'
Properties:
Handler: index.handler
Runtime: nodejs8.10
CodeUri: ./my-other-func
Description: >-
bla bla
bla bla
MemorySize: 1152
Timeout: 300
Role: 'arn:aws:iam::116738426468:role/rolename'
Events:
Api1:
Type: Api
Properties:
Path: /path
Method: POST
sorry, now i can see that at the question I was not providing enough info, but i answer to my own question hoping I can help some as lost as I was. Serverless is a nice approach but it does have quiet a learning curves.
Regards, Daniel
I am new to AWS and SAM. I am developing a dummy backend using AWS services. For that, I am using SAM application to write the code locally. I defined the structure of APIs and Lambda in that as
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
sam-app
Sample SAM Template for sam-app
Globals:
Function:
Timeout: 300
Api:
Cors:
AllowHeaders: "'content-type, authorization'"
AllowOrigin: "'*'"
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: hello_world
Handler: app.lambda_handler
Runtime: nodejs8.10
Environment:
Variables:
PARAM1: VALUE
Events:
HelloWorld:
Type: Api
Properties:
Path: /hello2
Method: get
Outputs:
HelloWorldFunction:
Description: "Hello World Lambda Function ARN"
Value: !GetAtt HelloWorldFunction.Arn
This creates a small dummy application. But, I want to know how to use other utilities of AWS like Body Mapping, defining model etc. Please help me know these.
Thank you...
You can define models, etc using an API Gateway Swagger definition. This can be embedded in the SAM template or hosted in S3 and referenced by the SAM template
Basic example looks like:
RestApi:
Type: AWS::Serverless::Api
Properties:
DefinitionBody:
<add Swagger definition here>
See https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessapi for what the SAM API Gateway configuration options are.
Some sample SAM + API Gateway + Swagger examples are at:
https://github.com/awslabs/serverless-application-model/blob/master/examples/2016-10-31/inline_swagger/template.yaml
https://github.com/awslabs/serverless-application-model/blob/master/examples/2016-10-31/api_swagger_cors/template.yaml
https://docs.aws.amazon.com/apigateway/latest/developerguide/api-as-lambda-proxy-export-swagger-with-extensions.html
I'm trying to deploy AWS Lambda function by using SAM.
What I want to do is to update exsiting lambda function by deploying local source code.
In order to do that, I specified the existing lambda function name as 'FunctionName' in template.yaml like below.
However, 'FunctionName' does only support for creating new function, not updating to existing function.
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-function.html#cfn-lambda-function-functionname
Are there any ways to specify Function Name in SAM in order to update lambda function?
[template.yaml]
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
HelloWorld:
Type: 'AWS::Serverless::Function'
Properties:
FunctionName: 'hello_world'
Description: ''
Handler: index.handler
MemorySize: 256
Role: 'arn:aws:iam::368834739507:role/lambda_basic_execution'
Runtime: nodejs6.10
Timeout: 120
Using SAM (and/or CloudFormation), you cannot update existing resources.
SAM (and CloudFormation) create and manage their own resources. All resources specified in the template are going to be created when the stack is created. They cannot be "taken over".
Instead, you should allow SAM (or CloudFormation) to create the Lambda function for you, then update users to reference the new function. After that, you can update your code using SAM.