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
Related
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.
I am currently learning AWS Serverless Application Model (SAM).
With that objective, I created a simple template that is deploying a Python Lambda function and an API which calls this function. My template is the following :
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
a test with AWS SAM
Globals:
Function:
Timeout: 10
Api:
OpenApiVersion: 3.0.1
Resources:
HelloAPI:
Type: AWS::Serverless::Api
Properties:
StageName: dev
Auth:
ApiKeyRequired: true
UsagePlan:
CreateUsagePlan: PER_API
Description: Usage plan for this API
HelloFunction:
Properties:
CodeUri: some/path/to/hello_function
Handler: hello.lambda_handler
Runtime: python3.8
Events:
Hello:
Type: Api
Properties:
RestApiId: !Ref HelloAPI
Path: /
Method: post
Outputs:
ProdDataEndpoint:
Description: "API dev stage endpoint"
Value: !Sub "https://${HelloAPI}.execute-api.${AWS::Region}.amazonaws.com/dev/"
So the template creates a UsagePlan with an associated API key. I checked within AWS, the ressources are indeed created correcly.
I can test the API locally with :
sam local start-api
sam local generate-event apigateway aws-proxy > local-event.json
curl -d "#local-event.json" -X POST http://127.0.0.1:3000/
And this works. But I don't know how to test it with the API key to check if the access is indeed protected by the key. Does anyone know how to do that?
Thanks !
I have an AWS Serverless cloudformation template creating a lambda function and a rest api. I would like some guidance or pointers on the steps to take in order to create a custom API domain name so that my api have the URL:
api.search.mydomainname.com
How does this work with my existing Serverless function code and the Events?
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
MyFunction:
...
Events:
ProxyResource:
Type: Api
Properties:
RestApiId: !Ref RestApi
Path: /{proxy+}
Method: ANY
RootResource:
Type: Api
Properties:
RestApiId: !Ref RestApi
Path: /
Method: ANY
Found a good solution regarding the problem at:
https://github.com/awslabs/serverless-application-model/issues/40
I am trying to create a lambda function using SAM, however I can't work out how to add a custom domain to it. Do I need to add a whole ApiGateway to my CloudFormation template just to change the domain or is there is an easier way?
My domain is in Route53 and I have a certificate for it in ACM.
My template is currently as follows:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: An AWS Serverless Application that uses the ASP.NET Core framework running in Amazon Lambda.
Resources:
ExampleWebApi:
Type: AWS::Serverless::Function
Properties:
Handler: Example.WebApi::Example.WebApi.LambdaEntryPoint::FunctionHandlerAsync
Runtime: dotnetcore2.1
CodeUri: ''
MemorySize: 128
Timeout: 10
Role: null
Policies:
- AWSLambdaFullAccess
Environment:
Variables: {}
Events:
PutResource:
Type: Api
Properties:
Path: "/{proxy+}"
Method: ANY
Yes, you need to use API Gateway in order to define a custom domain for a lambda 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