Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to run locally with SAM #16

Open
alteredtech opened this issue Dec 5, 2024 · 2 comments
Open

How to run locally with SAM #16

alteredtech opened this issue Dec 5, 2024 · 2 comments

Comments

@alteredtech
Copy link

I have been experimenting with contract validation with OpenAPI and looking to try it against some lambda functions. However when I attempt to run locally with DOCKER_HOST=unix://$HOME/.docker/run/docker.sock sam local start-api --template template.yml I only get this output

Mounting /Users/alteredtech/Coding/xcode-apps/OpenAPI-Foundation as /var/task:ro,delegated, inside runtime container
START RequestId: 53869222-a0c3-44b5-a6a1-a18c447286b6 Version: $LATEST
05 Dec 2024 16:54:20,524 [ERROR] (rapid) Init failed InvokeID= error=fork/exec /var/task/bootstrap: no such file or directory
05 Dec 2024 16:54:20,527 [ERROR] (rapid) Invoke failed error=fork/exec /var/task/bootstrap: no such file or directory InvokeID=f47285a1-48ed-4ec3-9e23-69d48b620dd4
05 Dec 2024 16:54:20,527 [ERROR] (rapid) Invoke DONE failed: Runtime.InvalidEntrypoint

Invalid lambda response received: Lambda response must be valid json
2024-12-05 09:54:21 127.0.0.1 - - [05/Dec/2024 09:54:21] "GET /pet/1 HTTP/1.1" 502 -

I was initially thinking it was this error with SAM but with that bootstrap error, it has me thinking its an issue with how I am deploying locally. I took at look at the swift lambda deployment but they do it slightly differently and do have some dockerfiles that add the bootstrap file to the docker image.

I can get this working using the local method in the docs LOCAL_LAMBDA_SERVER_ENABLED=true swift run but I am trying to mimic it being deployed so I can only use normal curl 127.0.0.1:3000/pet/1 without all the extra json in the Quote example.

I am using a slimmed down pet store openapi doc

openapi: 3.0.3
info:
  title: Swagger Petstore - OpenAPI 3.0
  description: |-
    This is a sample Pet Store Server based on the OpenAPI 3.0 specification.  You can find out more about
    Swagger at [https://swagger.io](https://swagger.io). In the third iteration of the pet store, we've switched to the design first approach!
    You can now help us improve the API whether it's by making changes to the definition itself or to the code.
    That way, with time, we can improve the API in general, and expose some of the new features in OAS3.

    Some useful links:
    - [The Pet Store repository](https://github.com/swagger-api/swagger-petstore)
    - [The source API definition for the Pet Store](https://github.com/swagger-api/swagger-petstore/blob/master/src/main/resources/openapi.yaml)

  termsOfService: http://swagger.io/terms/
  contact:
    email: [email protected]
  license:
    name: Apache 2.0
    url: http://www.apache.org/licenses/LICENSE-2.0.html
  version: 1.0.11
externalDocs:
  description: Find out more about Swagger
  url: http://swagger.io
servers:
  - url: https://127.0.0.1:7000/invoke
tags:
  - name: pet
    description: Everything about your Pets
    externalDocs:
      description: Find out more
      url: http://swagger.io
paths:
  /pet:
    put:
      tags:
        - pet
      summary: Update an existing pet
      description: Update an existing pet by Id
      operationId: updatePet
      requestBody:
        description: Update an existent pet in the store
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Pet'
        required: true
      responses:
        '200':
          description: Successful operation
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Pet'
        '400':
          description: Invalid ID supplied
        '404':
          description: Pet not found
        '422':
          description: Validation exception
    post:
      tags:
        - pet
      summary: Add a new pet to the store
      description: Add a new pet to the store
      operationId: addPet
      requestBody:
        description: Create a new pet in the store
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Pet'
        required: true
      responses:
        '200':
          description: Successful operation
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Pet'
        '400':
          description: Invalid input
        '422':
          description: Validation exception
  /pet/findByStatus:
    get:
      tags:
        - pet
      summary: Finds Pets by status
      description: Multiple status values can be provided with comma separated strings
      operationId: findPetsByStatus
      parameters:
        - name: status
          in: query
          description: Status values that need to be considered for filter
          required: false
          explode: true
          schema:
            type: string
            default: available
            enum:
              - available
              - pending
              - sold
      responses:
        '200':
          description: successful operation
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Pet'
        '400':
          description: Invalid status value
  /pet/findByTags:
    get:
      tags:
        - pet
      summary: Finds Pets by tags
      description: Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
      operationId: findPetsByTags
      parameters:
        - name: tags
          in: query
          description: Tags to filter by
          required: false
          explode: true
          schema:
            type: array
            items:
              type: string
      responses:
        '200':
          description: successful operation
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Pet'
        '400':
          description: Invalid tag value
  /pet/{petId}:
    get:
      tags:
        - pet
      summary: Find pet by ID
      description: Returns a single pet
      operationId: getPetById
      parameters:
        - name: petId
          in: path
          description: ID of pet to return
          required: true
          schema:
            type: integer
            format: int64
      responses:
        '200':
          description: successful operation
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Pet'
        '400':
          description: Invalid ID supplied
        '404':
          description: Pet not found
    delete:
      tags:
        - pet
      summary: Deletes a pet
      description: delete a pet
      operationId: deletePet
      parameters:
        - name: api_key
          in: header
          description: ''
          required: false
          schema:
            type: string
        - name: petId
          in: path
          description: Pet id to delete
          required: true
          schema:
            type: integer
            format: int64
      responses:
        '200':
          description: Pet removed
        '400':
          description: Invalid pet value
  /pet/{petId}/uploadImage:
    post:
      tags:
        - pet
      summary: uploads an image
      description: ''
      operationId: uploadFile
      parameters:
        - name: petId
          in: path
          description: ID of pet to update
          required: true
          schema:
            type: integer
            format: int64
        - name: additionalMetadata
          in: query
          description: Additional Metadata
          required: false
          schema:
            type: string
      requestBody:
        content:
          application/octet-stream:
            schema:
              type: string
              format: binary
      responses:
        '200':
          description: successful operation
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiResponse'
components:
  schemas:
    Category:
      type: object
      properties:
        id:
          type: integer
          format: int64
          example: 1
        name:
          type: string
          example: Dogs
    Tag:
      type: object
      properties:
        id:
          type: integer
          format: int64
        name:
          type: string
    Pet:
      required:
        - name
        - photoUrls
      type: object
      properties:
        id:
          type: integer
          format: int64
          example: 10
        name:
          type: string
          example: doggie
        category:
          $ref: '#/components/schemas/Category'
        photoUrls:
          type: array
          items:
            type: string
        tags:
          type: array
          items:
            $ref: '#/components/schemas/Tag'
        status:
          type: string
          description: pet status in the store
          enum:
            - available
            - pending
            - sold
    ApiResponse:
      type: object
      properties:
        code:
          type: integer
          format: int32
        type:
          type: string
        message:
          type: string
  requestBodies:
    Pet:
      description: Pet object that needs to be added to the store
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Pet'
@alteredtech
Copy link
Author

Oh I think I solved it. I had to update this line in the template

    CodeUri: .

to

    CodeUri: .aws-sam/build/PetStore

This might have been obvious to others but not I as I have not used SAM before.

@alteredtech
Copy link
Author

alteredtech commented Dec 5, 2024

hmmm so little issue I am discovering now. removed a path in the openapi doc. Now when I try to build with the solution I found I get this error.

sam build
Building codeuri: /Users/alteredtech/Coding/xcode-apps/OpenAPI-Foundation/.aws-sam/build/PetStore
runtime: provided.al2 architecture: arm64 functions: PetStore
PetStore: Running CustomMakeBuilder:CopySource
Skipping copy operation since source
/Users/alteredtech/Coding/xcode-apps/OpenAPI-Foundation/.aws-sam/build/PetStore does not exist
PetStore: Running CustomMakeBuilder:MakeBuild

Build Failed
Error: CustomMakeBuilder:MakeBuild - Makefile not found at /Users/alteredtech/Coding/xcode-apps/OpenAPI-Foundation/.aws-sam/build/PetStore/Makefile

The only way to get around this is by flip flopping between .aws-sam/build/PetStore and . for the CodeUri.

@alteredtech alteredtech reopened this Dec 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant