Template for API gateway creation using CI-CD

Below is the template which can be used for api gateway creation in AWS
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  Application containing RestAPI for First

Globals:
  Function:
    Timeout: 30

Parameters:
  #s3 bucket name
  BucketName:
    Type: String
    Description: Bucket to be used
    Default: lambda-deployment-d1

  EnvironmentName:
    Type: 'AWS::SSM::Parameter::Value<String>'
    Default: /<parameterPath>/EnvironmentName                        
    # /myDemo/d1/EnvironmentName

  StageName:
    Type: 'AWS::SSM::Parameter::Value<String>'
    Default: /<parameterPath>/StageName                             
    # /myDemo/d1/EnvironmentName

  BucketPrefix:
    Type: String
    Description: bucket prefix
    Default: first-project-api                                   
    # /myDemo/d1/EnvironmentName

  CertificateARN:
    Type: 'AWS::SSM::Parameter::Value<String>'
    Default: /<parameterPath>/CertificateARN    
    # /myDemo/d1/CertificateARN

  DomainName:
    Type: 'AWS::SSM::Parameter::Value<String>'
    Default: /<parameterPath>/DomainName        
    # /myDemo/d1/DomainName



Resources:

  LambdaRoleForVPCResources:
    Type: AWS::IAM::Role                                            
    # https://serverless.com/framework/docs/providers/aws/guide/resources/
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
        - Effect: "Allow"
          Principal:
            Service:
            - "lambda.amazonaws.com"
          Action:
          - "sts:AssumeRole"
      Policies:
      #policy name
      - PolicyName: "LambdaVpcPolicy"
        PolicyDocument:
          Version: "2012-10-17"
          Statement:
          - Effect: "Allow"
            Action:
            - "logs:CreateLogGroup"
            - "logs:CreateLogStream"
            - "logs:PutLogEvents"
            - "ec2:CreateNetworkInterface"
            - "ec2:DescribeNetworkInterfaces"
            - "ec2:DeleteNetworkInterface"
            - "dynamodb:*"
            - "apigateway:*"
            Resource: "*"
  #creates api gateway using swagger.yml
    
    
  FirstRestAPI:
    Type: AWS::Serverless::Api                 
    # https://docs.aws.amazon.com/serverless-alication-model/latest/developerguide/serverless-sam-template.html
    Properties:
      Name: !Join ['', [' first-rest-api-', !Ref EnvironmentName]]
      StageName: !Ref StageName
      # https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-transform.html
      DefinitionBody:
        'Fn::Transform':        
         # Fn::Transform specifies a macro to perform custom processing on part of a stack template. 
          Name: 'AWS::Include'
          # Replace <bucket> with your bucket name
          Parameters:
            Location: !Join ['', [ 's3://',!Ref BucketName, '/',!Ref BucketPrefix, '/swagger.yaml'  ] ]

  FirstRestAPIGetByItem:
    Type: AWS::Serverless::Function 
    # https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-transform.html
    FunctionName: !Join ['', [ 'FirstRestAPIGetByItem-', !Ref EnvironmentName ] ]
    Properties:
      #python code base containg lambdas which process request
      CodeUri: First-rest-api/build/
      #lambda name and its method
      Handler: ItemByName.lambda_handler
      Runtime: python3.7
      #role of lambda
      Role: !GetAtt LambdaRoleForVPCResources.Arn
      #these are environment variable which are required for lambda
      Environment:
        Variables:
          First_TABLE:  !Join ['', [ 'Item-table-', !Ref EnvironmentName ] ]
         
      Events:
        #listner of lambda fucntion ...this can be api kinesis or any aws service
        SubscribeToEvents:
        #type of service to subscribe
          Type: Api
          Properties:
            #name of service this is declared above
            RestApiId: !Ref FirstRestAPI
            #path to access api this is also maed to swagger.yml
            Path: /item
            #method type
            Method: post



    #done for static name creation --create name of api
  ApiDomainName:
    Type: 'AWS::ApiGateway::DomainName'
    Properties:
      CertificateArn: !Ref CertificateARN
      DomainName: !Join ['', ['first-api-', !Ref EnvironmentName, '.', !Ref DomainName]]
   
   #done for static name creation -- create maing for api
  APIMaing:    
    Type: 'AWS::ApiGateway::BasePathMapping'
    Properties:
      BasePath: v1
      DomainName: !Ref ApiDomainName
      RestApiId: !Ref FirstRestAPI
      Stage: !Ref StageName


#generates output url for the generated services      
Outputs:

  ItemByNameApiUrl:
    Description: URL of API endpoint
    Value: !Join
    - ''
    - - https://
      - !Ref FirstRestAPI
      - '.execute-api.'
      - !Ref 'AWS::Region'
      - '.amazonaws.com/'
      - !Ref StageName
      - '/item'



   #shows details of all created api's
  FirstRestAPIGetByItem:
    Description: " Rest API Function ARN"
    Value: !GetAtt FirstRestAPIGetByItem.Arn



313 total views, 1 views today