Home > Back-end >  How should i run this file in gitlab.ci.yml script to clean?
How should i run this file in gitlab.ci.yml script to clean?

Time:07-02

So This is the Python file i have with me

clean_old_lambda_versions.py

from __future__ import absolute_import, print_function, unicode_literals
import boto3

# This script removes all versions except $LATEST and the newest version
# If this script tries to delete a version any alias is using,
# boto3 will throw an exception and the script will exit

def clean_old_lambda_versions():
    client = boto3.client('lambda')
    functions = client.list_functions()['Functions']
    for function in functions:
        while True:
          versions = client.list_versions_by_function(FunctionName=function['FunctionArn'])['Versions']
          numVersions = len(versions)
          if numVersions <= 2:
              print('{}: done'.format(function['FunctionName']))
              break
          for version in versions:
              if version['Version'] != function['Version'] and numVersions > 2: # $LATEST
                  arn = version['FunctionArn']
                  print('delete_function(FunctionName={})'.format(arn))
                  # client.delete_function(FunctionName=arn)  # uncomment me once you've checked
                  numVersions -= 1


if __name__ == '__main__':
    clean_old_lambda_versions()

And this is my gitlab.ci.yml file to execute this file

delete-lambda-versions-nonprod:
  stage: import
  image: "someimage/confidential"
  <<: *nonprod
  variables:
    ENV_TAG: "alpha1"
  script:
     - . ./clean_old_lambda_versions.py
  when: manual

This is the error i am getting when running this pipeline in my gitlab

$ . ./clean_old_lambda_versions.py /bin/sh: ./clean_old_lambda_versions.py: line 1: import: not found

Can someone help me with this. How can i execute this script and clean all my older versions in aws lambda ? or is there any other work around to clean the older version using jobs in gitlab ? Thanks in advance

CodePudding user response:

It's a python script. You have to run python.

- python3 ./clean_old_lambda_versions.py

CodePudding user response:

I would think either your image should come with boto3 preinstalled or you have to pip install that in your script block in your ci.yml file in order for your python script to work.

  • Related