How to create 2 AWS lambdas on a single CloudFormation stack? - amazon-web-services

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

Related

How to deploy to multiple envorionments (develop, release, production) with AWS SAM?

I have an AWS SAM template where I have a Lambda with an API Gateway and I want to be able to deploy that lambda to different environments such as develop, release, and production as any other project so that everything can be tested well before reaching the final end-user.
So far I have tried creating a parameter in my template which is used to change the environment on deploy and the first time lambda got deployed with its name-develop but when I tried to deploy to release it just substituted name-develop with name-release instead of having both lambdas coexisting.
Here is my template:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
ocr-app
Sample SAM Template for ocr-app
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Timeout: 15
Parameters:
Env:
Type: String
AllowedValues:
- develop
- release
- product
# Default: develop
Description: Environment in which the application will be deployed. Allowed values [develop, release, product]
Resources:
OCRFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
Environment:
Variables:
ENV: !Ref Env
CodeUri: ocr/
Handler: app.lambdaHandler
FunctionName: !Sub OCRFunction_${Env}
Runtime: nodejs14.x
Policies:
- S3ReadPolicy: # Managed Policy
BucketName: "*"
- AWSLambdaExecute # Managed Policy
Architectures:
- x86_64
Events:
OCR:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /ocr
Method: get
Outputs:
# ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
# Find out more about other implicit resources you can reference within SAM
# https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
OCRApi:
Description: "API Gateway endpoint URL for Prod stage for Hello World function"
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
OCRFunction:
Description: "Hello World Lambda Function ARN"
Value: !GetAtt OCRFunction.Arn
OCRFunctionIamRole:
Description: "Implicit IAM Role created for Hello World function"
Value: !GetAtt OCRFunctionRole.Arn
How do I deploy to multiple environments?
One way I found out so far is creating another stack - so a stack per environment. I am not sure if this is the only/best way so I am open for other opinions.

Aws cloud formation yaml with multiple java projects

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

How to read aws sam parameter in .NET lambda function

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.

AWS Codepipeline Example

I am trying the example of Codepipeline for Lambda Function given by AWS. On executing the pipeline its getting failed during cloudformation execution with error saying "Template configuration is not valid"
Below is the template I copied from the example but its failing any suggestions?
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Outputs the time
Resources:
TimeFunction:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs6.10
CodeUri: ./
Events:
MyTimeApi:
Type: Api
Properties:
Path: /TimeResource
Method: GET
Also please let me know where could I see the detailed error log?

AWS SAM - How to specify the name of your function

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