0

I deployed API (AWS::Serverless::Function) on AWS using SAM.

I am able to change API Endpoint Type to Private in API setting.

But I want to find a way to do it programatically.

I found that I need to add "AWS::EC2::VPCEndpoint" and "AWS::Serverless::Api".

This way I can reference AWS::Serverless::Api inside AWS::Serverless::Function RestApiId:

!Ref ServerlessApi

AWS::Serverless::Api has EndpointType in its properties:

Properties:
  StageName: !Ref "Environment"
  EndpointConfiguration:
    Type: "PRIVATE"
    VPCEndpointIds:
      - !Ref APIGatewayVpcEndpoint

But when I deploy API, it is not being modified, the EndpointType is not changed to PRIVATE.

I don't where I am making a mistake and why it is not being referenced properly. I wish I at least received some kind of error.

I would appreciate anyone's help, thank you.

Resources:
  HealthFunction:
    Type: AWS::Serverless::Function 

    Properties:
      CodeUri: healthcheck
      Handler: healthcheck
      Runtime: go1.x
      EndpointConfiguration:
        Types:
          - PRIVATE
      Architectures:
        - x86_64
      Events:
        CatchAll:
          Type: Api 
          Properties:
            Path: /healthcheck
            Method: GET
            RestApiId: !Ref ServerlessApi

      Environment:
        Variables:
          DEFAULT_NAME: Ok

  #VPC_ENDPOINT

  APIGatewayVpcEndpoint:
    Type: "AWS::EC2::VPCEndpoint"
    Properties:
      SubnetIds:
        - !Ref tgwSubnetA
        - !Ref tgwSubnetB
        - !Ref tgwSubnetC

      SecurityGroupIds:
        - !Ref osSecurityGroup
      ServiceName: !Sub com.amazonaws.${AWS::Region}.execute-api
      VpcId: !Ref vpc
      VpcEndpointType: "Interface"
      PrivateDnsEnabled: false
      PolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: "Allow"
            Principal: "*"
            Action:
              - "execute-api:Invoke"
            Resource:
              - !Sub "arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:*******/*"


  # Api gateway
  ServerlessApi:
    Type: "AWS::Serverless::Api"
    Properties:
      StageName: !Ref "Environment"
      EndpointConfiguration:
        Type: "PRIVATE"
        VPCEndpointIds:
          - !Ref APIGatewayVpcEndpoint
Alina
  • 21
  • 3

1 Answers1

0

It's solved.

The problem was is that SAM was deploying the wrong template.yml file (this file didn't have my changes).

In the sam deploy command I specified the path for the correct template.yml file.

Also need to make sure that CodeUri goes to the correct folder where main.go (lambda function) is located.

And CloudFormation template should be deleted before every deployment in order to add changes from the template (only Cloudformation for API, Lmabda, etc, not the Bucket cloudformation)

Alina
  • 21
  • 3
  • It would be good if you posted updated code/config samples to make it easier for others to follow your changes – Jan Oct 21 '22 at 11:20