0
votes

I tried deploying a serverless python app through the azure devops pipeline.

serverless.yml

service: ws-serverless
provider:
  name: aws
  runtime: python3.7
  websocketApiName: ws-serverless-api
  websocketApiRouteSelectionExpression: $request.body.action

  stage: dev
  region: ap-northeast-2

  iamRoleStatements:

    - Effect: "Allow"
      Action:
        - "execute-api:ManageConnections"
      Resource: 
        - "arn:aws:execute-api:*:*:**/@connections/*"

    - Effect: "Allow"
      Action:
        - "dynamodb:PutItem"
        - "dynamodb:GetItem"
        - "dynamodb:UpdateItem"
        - "dynamodb:DeleteItem"
        - "dynamodb:BatchGetItem"
        - "dynamodb:BatchWriteItem"
        - "dynamodb:Scan"
        - "dynamodb:Query"
      Resource: 
        - "arn:aws:dynamodb:ap-northeast-2:*:*"

functions:
  connectionManager:
    handler: handler.connection_manager
    events:
        - websocket:
            route: $connect
        - websocket:
            route: $disconnect
  defaultMessage:
    handler: handler.default_message
    events:
        - websocket:
            route: $default
  getRecentMessages:
    handler: handler.get_recent_messages
    events:
        - websocket:
            route: getRecentMessages
  sendMessage:
    handler: handler.send_message
    events:
        - websocket:
            route: sendMessage
  ping:
    handler: handler.ping
    events:
        - http:
            path: ping
            method: get

plugins:
  - serverless-python-requirements

custom:
  pythonRequirements:
      dockerizePip: true
      noDeploy: []

azure-pipelines.yml

trigger:
- test

pool:
  vmImage: 'ubuntu-latest'

steps:
- task: UsePythonVersion@0
  inputs:
    versionSpec: '3.7'
    addToPath: true
  displayName: Install Python

- task: NodeTool@0
  inputs:
    versionSpec: '10.x'
  displayName: 'Install Node.js'

- script: |
    npm install --no-cache
  displayName: 'npm install'

- task: AWSShellScript@1
  inputs:
    awsCredentials: aws
    regionName: 'ap-northeast-2'
    scriptType: 'inline'
    inlineScript: |
      ./node_modules/.bin/serverless package --stage dev --region ap-northeast-2 --package /tmp/dev_artifacts/
  displayName: Package for Dev Environment

- task: PublishBuildArtifacts@1
  inputs:
    pathToPublish: /tmp/dev_artifacts
    artifactName: dev_artifacts
  displayName: Export Dev Artifacts

It failed on Package for Dev Environment

I got this error message

Error: ENOENT: no such file or directory, open '/home/vsts/work/1/s/venv/bin/python'

So i tried installing Python in that path using Virtualenv, but it also failed.

FileExistsError: [Errno 17] File exists: '/opt/hostedtoolcache/Python/3.7.7/x64/bin/python' -> '/home/vsts/work/1/s/venv/bin/python'

1

1 Answers

1
votes

I did it in a lsiglty different way. I used serverless package installed globally. But all went ok. (almost all, working directory didn't work for me, so I had to navigate to folder directly). I hope it helps you:

trigger:
- test

pool:
  vmImage: 'ubuntu-latest'

steps:
- task: UsePythonVersion@0
  inputs:
    versionSpec: '3.7'
    addToPath: true
  displayName: Install Python

- task: NodeTool@0
  inputs:
    versionSpec: '10.x'
  displayName: 'Install Node.js'

- script: |
    npm install
  displayName: 'npm install dependency'

- script: |
    npm install serverless -g
  displayName: 'npm install serverless'

- task: AWSShellScript@1
  inputs:
    awsCredentials: aws
    workingDirectory: $(Build.SourcesDirectory)/stackoverflow/31-serverless-aws
    regionName: 'ap-northeast-2'
    scriptType: 'inline'
    inlineScript: |
      cd $(Build.SourcesDirectory)/stackoverflow/31-serverless-aws
      ls
      serverless package --stage dev --region ap-northeast-2 --package $(Build.ArtifactStagingDirectory)
  displayName: Package for Dev Environment

- task: PublishBuildArtifacts@1
  inputs:
    pathToPublish: $(Build.ArtifactStagingDirectory)
    artifactName: dev_artifacts
  displayName: Export Dev Artifacts