Azure Devops: Build Aws Lamdba

Durante lo scorso week-end ho deciso di provare un nuovo esperimento con le AWS Lambda. Di cosa si tratta? Ho deciso di sperimentare il build tramite Azure DevOPS di un progetto template presente su GitHub.

Prima di cominciare avrete bisogno di …

  • account su Azure DEVOps
  • account console per AWS
  • una Lambda gia’ creata e pronta da riempire

Siete pronti a cominciare?

Vi informo che la mia prova e di conseguenza l’articolo si basera’ del codice NET Core.

Azure DEVOps : Build AWS Lamdba

AWS Toolkit

Prima di potere svolgere qualsiasi passaggio successivo avete la necessita’ di installare AWS Toolkit for Azure DevOps nella vostra organizzazione. Senza di esso non vi sara’ possibile sfruttare i passaggi successivi per raggiungere l’obiettivo.

The AWS Toolkit for Azure DevOps adds tasks to easily enable build and release pipelines in Azure DevOps (formerly VSTS) and Azure DevOps Server (previously known as Team Foundation Server (TFS)) to work with AWS services including Amazon S3, AWS Elastic Beanstalk, AWS CodeDeploy, AWS Lambda, AWS CloudFormation, Amazon Simple Queue Service and Amazon Simple Notification Service, and run commands using the AWS Tools for Windows PowerShell module and the AWS CLI.

The AWS Toolkit for Azure DevOps is available from the Visual Studio Marketplace.

This is an open source project because we want you to be involved. We love issues, feature requests, code reviews, pull requests or any positive contribution. Please see the the CONTRIBUTING guide for how to help, including how to build your own extension.

Per quanto riguarda le features offerte nell’ultima release ( 2021-11-177, 01:06:25) possiamo trovare:

  • AWSCLI - Interact with the AWSCLI (Windows hosts only)
  • AWS Powershell Module - Interact with AWS through powershell (Windows hosts only)
  • Beanstalk - Deploy ElasticBeanstalk applications
  • CodeDeploy - Deploy with CodeDeploy
  • CloudFormation - Create/Delete/Update CloudFormation stacks
  • ECR - Push an image to an ECR repository
  • Lambda - Deploy from S3, .net core applications, or any other language that builds on Azure DevOps
  • S3 - Upload/Download to/from S3 buckets
  • Secrets Manager - Create and retrieve secrets
  • SQS - Send SQS messages
  • SNS - Send SNS messages
  • Systems manager - Get/set parameters and run commands

Sicuramente seguiranno aggiornamenti in futuro.

AWS Service connections

Il primo passaggio da fare sara’ la creazione di una Service connections e per farlo i passaggi da fare sono davvero semplici:

  • Cliccare su Project Settings in basso a sinistra
  • Dal menu presente a sinistra selezionare Pipeline e poi Service connections
  • Fare click su New Service Connection
  • Selezionare AWS (Se non avete installato il toolkit, questa voce non sara’ presente)

A questo punto dovrete compilare i seguenti tre punti obbligatori (ma nulla vi vieta di compilare anche i restanti)

  • Access Key ID: The AWS access key ID for signing programmatic requests. Example: AKIAIOSFODNN7EXAMPLE
  • Secret Access Key: The AWS secret access key for signing programmatic requests. Example: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
  • Details > Service connection name

Molto probabilmente la domanda nella vostra mente in questo momento e’ la seguente:

Scusa, dove posso trovare Access Key ID e Secret Access Key?

La risposta e’ molto semplice e quasi spontanea. Dalla vostra AWS Console dovrete andare nella sezione Identity and Access Management (IAM) e creare un utente dedicato per accedere alle AWS Lamdba.

Importante: Se volete salvare queste informazioni non fatelo usando il notepad, ma usate strumenti criptati per garantirvi una sicurezza ulteriore.

Benissimo, ora siamo pronti per creare la pipeline di build! Ora non dobbiamo fare altro che creare la pipeline di build per aggiornare la nostra AWS Lambda direttamente su Cloud …

Pipelines di Build - Classic Editor

Se volete creare la pipeline di build sfruttando il classic editor per prendere confidenza coi diversi passaggio ecco quello come dovete fare.

L’azione da fare eseguire al nostro agente sara’ la seguente: AWS Lambda .NET Core.

Una volta aggiunta dovrete fornire le seguenti informazioni:

  • AWS Credentials: La connessione creata nel punto iniziale
  • AWS Region: selezionare la region in cui si trovera’ la Lambda (ex. Europe (Frankfurt) [eu-central-1])
  • Deployment Type: Function
  • Path to Lambda Project: selezionare il csproj in cui e’ contenuta la lambda

Ora passiamo alla sezione Lambda Function Properties con le seguenti voci:

  • Function Name: The name for the deployed Lambda function. You can also specify the Amazon Resource name (ARN) of the function when deploying to an existing function.
  • Function Role: The name of the AWS Identity and Access Management (IAM) Role that provides access to AWS services to the deployed Lambda function.
  • Function Handler: The function within your code that Lambda calls to begin execution. Format is ::<namespace.type-name>::.
  • Function Memory (MB): Memory allocated to the Lambda function. The value must be in multiples of 64.
  • Function Timout (Seconds): The function execution time at which Lambda should terminate the function.

Tutte queste informazioni -personalmente- le ho reperite dal file aws-lambda-tools-defaults.json ottenuto con la creazione della stessa in Visual Studio.

Ora -se tutto e’ stato configurato per il meglio- vi bastera’ selezionare Save & Run per vedere la vostra lambda aggiornata con l’ultima versione del codice sorgente. Semplice, vero?

Pipelines di Build - YAML

La corrispondenza dello step precedente in YAML e’ il seguente

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
steps:
- task: AmazonWebServices.aws-vsts-tools.LambdaNETCoreDeploy.LambdaNETCoreDeploy@1
  displayName: 'Deploy .NET Core to Lambda: devLamdbaTest '
  inputs:
    awsCredentials: 'AWS - Lambda Connection'
    regionName: 'eu-central-1'
    lambdaProjectPath: devLamdbaTest/devLamdbaTest.csproj
    functionName: devLamdbaTest
    functionRole: 'Hide ...'
    functionHandler: 'devLamdbaTest::devLamdbaTest.Function::FunctionHandler'
    functionMemory: 128
    functionTimeout: 30

Ora ditemi, come vi sentite ad avere messo una prima AWS Lambda sotto CI/CD con Azure DEVOps?

Che dite, mi merito una pizza degna ti tale nome dopo questo articolo praticamente infinito?