Dan
Dan

Reputation: 987

How to deploy Serverless Framework to AWS from Azure Pipeline?

I'm wanting to get familiar with Azure DevOps and setting up CI/CD through it. For the backend services, I want to use Serverless Framework (SF). I'm getting stuck trying to add credentials to the CI pipeline for SF.

I'm following the instructions in this documentation page but I'm getting this error in the deploy step.

AWS provider credentials not found. Learn how to set up AWS provider credentials in our docs here:

These are the steps to reproduce:

sls create -t aws-nodejs-typescript -p api-hello-service
cd api-hello-service
git init
git add .
git commit -m 'initial commit'
git remote add origin <origin>
# azure-pipelines.yml
trigger:
- main

pool:
  vmImage: ubuntu-latest

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

- script: |
    npm install
    npm install -g serverless
    serverless deploy
  displayName: 'npm install and build'

Deploy step fails:

Starting: npm install and build
==============================================================================
Task         : Command line
Description  : Run a command line script using Bash on Linux and macOS and cmd.exe on Windows
Version      : 2.201.1
Author       : Microsoft Corporation
Help         : https://learn.microsoft.com/azure/devops/pipelines/tasks/utility/command-line
==============================================================================
Generating script.
========================== Starting Command Output ===========================
/usr/bin/bash --noprofile --norc /home/vsts/work/_temp/dec6f028-dba1-4c47-8718-cb65524f31a1.sh
npm WARN deprecated [email protected]: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.
npm WARN deprecated [email protected]: Please downgrade to v7.1.5 if you need IE/ActiveXObject support OR upgrade to v8.0.0 as we no longer support IE and published an incorrect patch version (see https://github.com/visionmedia/superagent/issues/1731)
npm WARN deprecated [email protected]: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.
npm WARN deprecated [email protected]: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.

added 474 packages, and audited 475 packages in 16s

88 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
npm WARN deprecated [email protected]: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.
npm WARN deprecated [email protected]: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.
npm WARN deprecated [email protected]: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.
npm WARN deprecated [email protected]: Please downgrade to v7.1.5 if you need IE/ActiveXObject support OR upgrade to v8.0.0 as we no longer support IE and published an incorrect patch version (see https://github.com/visionmedia/superagent/issues/1731)

added 425 packages, and audited 426 packages in 6s

87 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
Running "serverless" from node_modules

Deploying api-hello-service to stage dev (us-east-1)

× Stack api-hello-service-dev failed to deploy (0s)
Environment: linux, node 16.17.0, framework 3.22.0 (local) 3.22.0v (global), plugin 6.2.2, SDK 4.3.2
Docs:        docs.serverless.com
Support:     forum.serverless.com
Bugs:        github.com/serverless/serverless/issues

Error:
AWS provider credentials not found. Learn how to set up AWS provider credentials in our docs here: <http://slss.io/aws-creds-setup>.
##[error]Bash exited with code '1'.
Finishing: npm install and build

Update:

Default generated serverless.ts

import type { AWS } from '@serverless/typescript';

import hello from '@functions/hello';

const serverlessConfiguration: AWS = {
  service: 'api-hello-service',
  frameworkVersion: '3',
  plugins: ['serverless-esbuild'],
  provider: {
    name: 'aws',
    runtime: 'nodejs14.x',
    apiGateway: {
      minimumCompressionSize: 1024,
      shouldStartNameWithService: true,
    },
    environment: {
      AWS_NODEJS_CONNECTION_REUSE_ENABLED: '1',
      NODE_OPTIONS: '--enable-source-maps --stack-trace-limit=1000',
    },
  },
  // import the function via paths
  functions: { hello },
  package: { individually: true },
  custom: {
    esbuild: {
      bundle: true,
      minify: false,
      sourcemap: true,
      exclude: ['aws-sdk'],
      target: 'node14',
      define: { 'require.resolve': undefined },
      platform: 'node',
      concurrency: 10,
    },
  },
};

module.exports = serverlessConfiguration;

Update: 2

Looks like I was missing app & org in my serverless config.

Now I'm receiving Error: You are not currently logged in. To log in, use: $ serverless login.

Am I setting the API key incorrectly?

azure pipeline

Upvotes: 1

Views: 1014

Answers (1)

pgrzesik
pgrzesik

Reputation: 2427

if you want to use credentials configured as a part of Serverless Dashboard, you need to set corresponding app and org in your configuration, and then expose SERVERLESS_ACCESS_KEY as env var in case of CI/CD setup. So, in your case, adding configured org and app to config should resolve the problem.

Upvotes: 1

Related Questions