Search code examples
aws-lambdaaws-cloudformationaws-serverlessaws-samaws-sam-cli

API Name is always the Stack Name in SAM


I am using the AWS CLI to deploy a SAM template.

The AWS Api Name is being set to the same as the CloudFormation Stack Name. I expected the Api to be called "Users" based on this template content below.

Is it possible to set the API name?

SAM template:

  MyApi:
    Type: AWS::Serverless::Api
    Properties:
      Name: Users
      StageName: default

Update with additional information (complete template and AWS CLI commands used to deploy):

Template:

AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31

Resources:
  HelloFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: main
      Runtime: go1.x
      Events:
        GetEvent:
          Type: Api
          Properties:
            Path: /
            Method: post
            #RestApiId: !Ref ApiGateway1

  LambdaInvokePermission:
    Type: AWS::Lambda::Permission
    Properties:
      FunctionName: !GetAtt
        - HelloFunction
        - Arn
      Action: 'lambda:InvokeFunction'
      Principal: apigateway.amazonaws.com
      SourceAccount: !Ref 'AWS::AccountId'

  MyApi:
    Type: AWS::Serverless::Api
    Properties:
      Name: Users
      StageName: default
      EndpointConfiguration: REGIONAL
      DefinitionBody:
        swagger: "2.0"
        info:
          title: "TestAPI"
        paths:
          /:
            post:
              x-amazon-apigateway-integration:
                uri:
                  Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${HelloFunction.Arn}/invocations
                responses: {}
                httpMethod: "POST"
                type: "aws_proxy"
Outputs:
  FunctioArn:
    Value: !GetAtt  HelloFunction.Arn
    Export:
      Name: HelloFunctionArn

CLI commands:

aws cloudformation package --template-file template.yml 
--output-template-file samtemplate.yaml --s3-bucket (bucketname) 

aws cloudformation deploy --template-file samtemplate.yaml 
--stack-name apisample-stack  --capabilities CAPABILITY_IAM

Solution

  • Your use of the Name property is the correct way to set the API Name.

    However, I think you are confused about what this template is doing. As you have written it, this template creates two APIs - the "implicit API", and then the API named "Users" that you declared explicitly.

    Note that:

    • The implicit API takes its name from the Stack Name. (Which seems to be what you are observing.)

    • The explicit API takes its name from the Name property.

    If you don't want to create the implicit API but rather define it explicitly yourself - which appears to be the case - then you just need to refer to it:

      Events:
        GetEvent:
          Type: Api
          Properties:
            Path: /
            Method: post
            RestApiId: !Ref MyApi # Add this line
    

    That will result in a single API being created and it will have the name "Users".