5

I am trying my hands on SAM templates. Here I am trying to figure out how can I add custom names within the template. As when I package and Deploy the Template it creates the lambda function with an added alphanumeric value. API gateway name will be the stack name and it will be deployed in "Prod" and "Stage" Stage with in the API gateway.

Is there a way to have my own custom names.

Here is my sample code for the SAM template:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: An AWS Serverless Specification template describing your function.
Resources:
  GetAllUser:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: code/
      Handler: getAllUser.lambda_handler
      Timeout: 5
      Runtime: python3.8
      Role: lambda_execution
      MemorySize: 128
      Events:
        GetAllUser:
          Type: Api
          Properties:
            Path: /get-all-user
            Method: get
            

could any one help me with this?

sumanth shetty
  • 1,851
  • 5
  • 24
  • 57

1 Answers1

5

For creating a name for your lambda you can specify directly AWS::Serverless::Function

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: An AWS Serverless Specification template describing your function.
Resources:
  GetAllUser:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: "MyFunctionName"
      CodeUri: code/
      Handler: getAllUser.lambda_handler
      Timeout: 5
      Runtime: python3.8
      Role: lambda_execution
      MemorySize: 128
      Events:
        GetAllUser:
          Type: Api
          Properties:
            Path: /get-all-user
            Method: get

As for other names like StageName and ApiGateway Name, you need to use AWS::Serverless::Api

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: AWS SAM template with a simple API definition
Resources:
  ApiGatewayApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: prod
      Name: myapi
  ApiFunction: # Adds a GET api endpoint at "/" to the ApiGatewayApi via an Api event
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: myfunction
      Events:
        ApiEvent:
          Type: Api
          Properties:
            Path: /
            Method: get
            RestApiId:
              Ref: ApiGatewayApi
      Runtime: python3.7
      Handler: index.handler
      InlineCode: |
        def handler(event, context):
            return {'body': 'Hello World!', 'statusCode': 200}
samtoddler
  • 8,463
  • 2
  • 26
  • 21
  • but how is API gateway or stage being associated with the lambda function? – sumanth shetty Feb 05 '21 at 09:52
  • 1
    @sumanthshetty you can reference the API Id in under `Events` [Api](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-function-api.html#sam-property-function-api--examples) – samtoddler Feb 05 '21 at 09:55
  • 1
    @sumanthshetty if this works for you, can you mark the question as solved. This helps the community and anyone in the future who stumbles upon this question. – samtoddler Feb 06 '21 at 17:02
  • hey @samtoddler. i am working on this. what should i refer in the function event? apiGateway or apiGatewayDeployment or ApiGatewayStage. Also i came across this doc https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-api.html which has Type: AWS::Serverless::Api – sumanth shetty Feb 08 '21 at 06:27
  • Not able to implement this in SAM currently – sumanth shetty Feb 08 '21 at 07:57
  • 1
    @sumanthshetty due to this [AWS CloudFormation compatibility: This property is unique to AWS SAM and doesn't have an AWS CloudFormation equivalent.](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-function-api.html) and `Identifier of a RestApi resource, which must contain an operation with the given path and method. Typically, this is set to reference an AWS::Serverless::Api resource defined in this template.`. So can't be defined via `CloudFormation`. I have updated my answer. – samtoddler Feb 08 '21 at 15:11