CI CD for Lambda function (Serverless app) with Gitlab

Gitlab CI integration with AWS Lambda for auto deployments.

39

Votes

image: docker:latest

before_script:
  - apt-get update -y  # Updating the Ubuntu Docker instance.
  - python -V  # Print out python version for debugging.
  - apt install -y zip jq
  - pip install awscli --upgrade --user
  - export PATH=~/.local/bin:$PATH # Required for awscli.
  - aws --version  # Print out aws cli version for debugging.

stages:
  - test
  - deploy

test:
  - pip install -r requirements.txt -t ./ # We must download all dependencies in current directory.
  - python setup.py test

variables:
  LAMBDA_NAME: 'AwsLambdaFunctionName'   # Replace it with the name of aws lambda function you want.
  S3_BUCKET: 'S3BucketName'   # Replace it with the name of Bucket that will hold the zip code.
  LAMBDA_RUNTIME: 'python2.7'
  LAMBDA_ARN: 'arn:aws:iam::XXXXX:role/XXXXXX' # ARN associated with this lambda function.
  LAMBDA_HANDLER: 'lambda_function.lambda_handler'  # This is default lambda handler.

deploy:
  stage: deploy

  only:
  - master   # We will run the CD only when something is going to change in master branch.

  script:
  - zip -r code.zip . -x \*.pyc *.git*   # Archive the code repository.
  - aws s3 cp code.zip s3://$S3_BUCKET/code.zip # Upload archive into s3.
  - aws lambda update-function-code --function-name $LAMBDA_NAME --zip-file fileb://code.zip || aws lambda create-function --function-name $LAMBDA_NAME --runtime $LAMBDA_RUNTIME --role $LAMBDA_ARN --handler $LAMBDA_HANDLER --code S3Bucket=$S3_BUCKET,S3Key=code.zip --memory-size 1024

  environment:
    name: master

Vote Here

You must earn at least 1 vote on your snippets to be allowed to vote

Terms Of Use

Privacy Policy

Featured snippets are MIT license

Gears & Masters

Advertise

DevOpsnipp.com © 2020

medium.png